HUD(2444)
題目鏈接http://acm.hdu.edu.cn/showproblem.php?pid=2444 就會給你n個人,m個朋友關系,然后讓你判斷是否能把他們分成兩組
然后里面誰也不認識對方
如果能,輸出最大的組的人數? 解法,先判斷二分圖,然后在二分圖匹配
然后里面誰也不認識對方
如果能,輸出最大的組的人數? 解法,先判斷二分圖,然后在二分圖匹配
?
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node {int v,u;int next; }V[1000*1000]; int n,m; int tol; int head[1000]; int col[1000]; int link[1000]; bool vis[1000]; void init() {int tol=0;memset(head,-1,sizeof(head)); } void add(int u,int v) {V[tol].v=v;V[tol].u=u;V[tol].next=head[u];head[u]=tol++;int t=u;u=v;v=t;V[tol].v=v;V[tol].u=u;V[tol].next=head[u];head[u]=tol++; } bool dfs(int u) {vis[u]=1; for(int k=head[u];k!=-1;k=V[k].next){int v=V[k].v;if(!vis[v]){vis[v]=1;if(link[v]==-1||dfs(link[v])){ link[v]=u;return true;}}}return false; } int match() {int ans=0;memset(link,-1,sizeof(link));for(int i=1;i<=n;i++){memset(vis,0,sizeof(vis));if(dfs(i))ans++;}return ans; } bool bldfs(int u,int co) {int i,v;col[u]=co;for(i=head[u];i!=-1;i=V[i].next){v=V[i].v;if(col[v]==co)return false;if(col[v]==-1&&!bldfs(v,co^1))return false;}return true; } int main() {while(scanf("%d %d",&n,&m)!=EOF){init();int u,v;for(int i=0;i<m;i++){scanf("%d %d",&u,&v);add(u,v);add(v,u);}memset(col,-1,sizeof(col));int flag=0;for(int i=1;i<=n;i++){if(col[i]==-1)if(!bldfs(i,1)){flag=1;break;}}if(flag==1)printf("No\n");else{int ans=match(); printf("%d\n",ans/2);}}return 0; }?
轉載于:https://www.cnblogs.com/NaCl/p/9580140.html
總結
- 上一篇: 管理集群中的 crs 管理员
- 下一篇: 【转】 Linux内核中读写文件数据的方