BZOJ1191 超级英雄Hero

BZOJ1191 超级英雄Hero

题目传送门

题解

裸的二分图匹配,直接跑匈牙利即可。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
const int maxn=1e5+500;
int n,m,tot;
struct edge {
	int to,nxt;
}E[maxn];
int belong[maxn],vis[maxn],head[maxn];
/*==================Define Area================*/
void addedge(int u,int v) {
	E[++tot].to=v;E[tot].nxt=head[u];head[u]=tot;
}

bool Solve(int x) {
	for(int i=head[x];~i;i=E[i].nxt) {
		int to=E[i].to;
		if(vis[to]) continue;
		vis[to]=1;
		if(belong[to]==-1||Solve(belong[to])) 
			return belong[to]=x;
	}
	return 0;
}

int main() {
	memset(head,-1,sizeof head);
	memset(belong,-1,sizeof belong);
	read(n);read(m);
	for(int i=1,x,y;i<=m;i++) {
		read(x);read(y);
		if(x!=y) addedge(i,x);
		addedge(i,y);
	}
	int ans=0;
	for(int i=1;i<=m;i++) {
		memset(vis,0,sizeof vis);
		if(!Solve(i)) break;
		ans++;
	}
	printf("%d\n",ans);
	return 0;
}
posted @ 2018-08-06 19:52  Apocrypha  阅读(135)  评论(0编辑  收藏  举报