hdu 2444 The Accomodation of Students (二分图婚配+染色法)
hdu 2444 The Accomodation of Students (二分图匹配+染色法)
Total Submission(s): 2895 Accepted Submission(s): 1351
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2895 Accepted Submission(s): 1351
Problem 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
Source
2008 Asia Harbin Regional Contest Online
Recommend
gaojie | We have carefully selected several similar problems for you: 2438 2443 2442 2441 2440
题意:判断是否是二分图,并输出最大匹配数。
思路:bfs染色法判断二分图,再就是套模板了。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 222 #define MAXN 40005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r typedef long long ll; using namespace std; struct Edge { int to,next; }edge[MAXN]; int n,m,num; bool used[maxn]; int head[maxn],color[maxn],linker[maxn]; void init() { num=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[num].to=v; edge[num].next=head[u]; head[u]=num++; } bool bfs() { int st,now,co,v; memset(color,-1,sizeof(color)); queue<int>Q; Q.push(1); color[1]=1; while(!Q.empty()) { st=Q.front(); Q.pop(); for (int i=head[st];i!=-1;i=edge[i].next) { v=edge[i].to; if (color[v]==-1) { color[v]=1-color[st]; Q.push(v); } else if (color[v]==color[st]) return true; } } return false; } 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 ans=0; memset(linker,-1,sizeof(linker)); for (int u=1;u<=n;u++) { memset(used,false,sizeof(used)); if (dfs(u)) ans++; } return ans; } int main() { int u,v; while (~scanf("%d%d",&n,&m)) { init(); for (int i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); } if (bfs()) { printf("No\n"); continue; } printf("%d\n",hungary()); } return 0; }