HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)
Posted 季末Despaire
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)相关的知识,希望对你有一定的参考价值。
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Description
There are a group of students. Some of them may know each other, while others don‘t. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don‘t know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don‘t know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
Input
For each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
Output
If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
No
3
题意:有n个学生,m对熟人,问你能否把他们分为2部分,其中在同一部分的人不认识,如果能,再将他们分到寝室,只有相互认识的人才能分到同一个寝室,问最多需要多少个寝室
分析:先判断是否为二分图,不是,就输出No,否则就求最大匹配。保存图用了两种方式。
/* **************************************************** #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 (判断是否是二分图,然后求最大匹配)的主要内容,如果未能解决你的问题,请参考以下文章
hdu 2444 The Accomodation of Students
HDU2444 The Accomodation of Students —— 二分图最大匹配
hdu2444The Accomodation of Students
HDU - 2444 The Accomodation of Studentsp[二分图判定,匈牙利算法]