标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
/* **************************************************** #include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int MAXN = 210; struct Edge{ int to,next; }edge[MAXN*MAXN]; int head[MAXN],tol; int uN,vN; int linker[MAXN]; bool used[MAXN]; int col[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[tol].to=v; edge[tol].next=head[u]; head[u]=tol++; } bool dfs(int u) { for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(!used[v]){ used[v]=true; if(linker[v]==-1||dfs(linker[v])){ linker[v]=u; return true; } } } return false; } int Hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=1;u<=uN;u++){ memset(used,false,sizeof(used)); if(dfs(u)) res++; } return res; } bool Color(int u) { if(col[u]==-1) col[u]=0; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(col[v]==-1){ col[v]=col[u]^1; if(!Color(v)) return false; } if(col[v]==col[u]) return false; } return true; } int main() { int n,m; int u,v; while(scanf("%d%d",&n,&m)!=EOF){ init(); uN=vN=n; for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } bool flag=true; memset(col,-1,sizeof(col)); for(int i=1;i<=n;i++) if(col[i]==-1) flag&=Color(i); if(!flag){ printf("No\n"); continue; } else printf("%d\n",Hungary()/2); } return 0; } ************************************************ */ #include<stdio.h> #include<string.h> #include<iostream> #include<vector> using namespace std; const int MAXN = 210; vector<int> G[MAXN]; int uN; int linker[MAXN]; bool used[MAXN]; int col[MAXN]; bool dfs(int u) { for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(!used[v]){ used[v]=true; if(linker[v]==-1||dfs(linker[v])){ linker[v]=u; return true; } } } return false; } int Hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=1;u<=uN;u++){ memset(used,false,sizeof(used)); if(dfs(u)) res++; } return res; } bool Color(int u) { if(col[u]==-1) col[u]=0; for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(col[v]==-1){ col[v]=col[u]^1; if(!Color(v)) return false; } if(col[v]==col[u]) return false; } return true; } int main() { int n,m; int u,v; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++) G[i].clear(); uN=n; for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } bool flag=true; memset(col,-1,sizeof(col)); for(int i=1;i<=n;i++) if(col[i]==-1) flag&=Color(i); if(!flag){ printf("No\n"); continue; } else printf("%d\n",Hungary()/2); } return 0; }
HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)
标签:
原文地址:http://www.cnblogs.com/wangdongkai/p/5635723.html