标签:
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using namespace std; int n,m,num,head[maxn],f[maxn],match[maxn],color[maxn]; struct node { int u,v,pre; }e[maxn*maxn]; void Add(int from,int to) { num++; e[num].u=from; e[num].v=to; e[num].pre=head[from]; head[from]=num; } bool Color(int s) { for(int i=head[s];i;i=e[i].pre) if(!color[e[i].v]) { color[e[i].v]=-1*color[s]; if(!Color(e[i].v))return 0; } else if(color[e[i].v]==color[s])return 0; return 1; } int Dfs(int s) { for(int i=head[s];i;i=e[i].pre) if(f[e[i].v]==0) { f[e[i].v]=1; if(match[e[i].v]==0||Dfs(match[e[i].v]))//XXXXX又是这句错了 下次再写错就XXXX { match[e[i].v]=s; return 1; } } return 0; } int main() { while(~scanf("%d%d",&n,&m)) { num=0; memset(f,0,sizeof(f)); memset(head,0,sizeof(head)); memset(color,0,sizeof(color)); memset(match,0,sizeof(match)); int u,v; for(int i=1;i<=m;i++) { scanf("%d%d",&u,&v); Add(u,v);Add(v,u); } if(n==1) { printf("No\n"); continue; } color[1]=1; if(!Color(1)) { printf("No\n"); continue; } int ans=0; for(int i=1;i<=n;i++) { memset(f,0,sizeof(f)); ans+=Dfs(i); } printf("%d\n",ans/2); } return 0; }
HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
标签:
原文地址:http://www.cnblogs.com/yanlifneg/p/5547434.html