标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4250 Accepted Submission(s): 1946
#include"cstdio" #include"cstring" #include"vector" #include"algorithm" using namespace std; const int MAXN=205; vector<int> G[MAXN]; int V,E; bool bfs(int s)//二分图判断 { int que[MAXN],rear=0,front=0; int color[MAXN]; memset(color,-1,sizeof(color)); color[s]=1;que[rear++]=s; while(rear!=front) { int v=que[front++]; for(int i=0;i<G[v].size();i++) { int u=G[v][i]; if(color[u]==-1) { color[u]=!color[v];//若没有染色则染成相反颜色 que[rear++]=u; } else if(color[u]==color[v]) return false;//若相邻结点颜色相同则不是二分图 } } return true; } int match[MAXN]; int vis[MAXN]; bool dfs(int u)//二分图匹配 { vis[u]=1; for(int i=0;i<G[u].size();i++) { int v=G[u][i],w=match[v]; if(w==0||(!vis[w]&&dfs(w))) { match[u]=v; match[v]=u; return true; } } return false; } int matching() { memset(match,0,sizeof(match)); int ans=0; for(int i=1;i<=V;i++) { if(!match[i]) { memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } } return ans; } int main() { while(scanf("%d%d",&V,&E)!=EOF) { for(int i=1;i<=V;i++) G[i].clear(); for(int i=0;i<E;i++) { int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } if(bfs(1)) printf("%d\n",matching()); else printf("No\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5181135.html