题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2431 Accepted Submission(s): 1158
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 212
using namespace std;
int n;
struct Edge
{
int to,next;
}edge[N*N];
int headList[N];
int vis[N];
int match[N];
int query;
int top;
int color[N];
int add(int a,int b)
{
edge[top].to=b;
edge[top].next = headList[a];
headList[a]=top++;
}
void get_map()
{
int i;
int a,b;
top=1;
memset(headList,-1,sizeof(headList));
for(i=0;i<query;i++)
{
scanf("%d %d",&a,&b);
add(a,b);
add(b,a);
}
}
//判断是否为二分图 ,一边颜色为0,另一边颜色为1
int check()
{
memset(color,-1,sizeof(color));
queue <int> qu;
qu.push(1);
color[1] = 1;
while(!qu.empty())
{
int x = qu.front();
qu.pop();
// printf("x==%d\n",x);
int j;
for(j = headList[x] ; j !=-1 ; j = edge[j].next)
{
int y = edge[j].to;
// printf("y==%d\n",y);
if(color[y]==-1) //该点还没被匹配过,则颜色为x的取反
{
color[y]=!color[x];
qu.push(y);
}
else if(color[x] == color[y])
return 1;
//printf("color:%d %d\n",color[x],color[y]);
}
}
return 0;
}
int dfs(int x)
{
int i;
for(i = headList[x] ; i!=-1 ;i = edge[i].next)
{
int k = edge[i].to;
if(!vis[k])
{
vis[k]=1;
if(match[k] == -1 || dfs(match[k]))
{
match[k] = x;
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d %d",&n,&query)!=EOF)
{
int i;
int result = 0;
get_map();
if(check())
{
printf("No\n");
continue;
}
memset(match,-1,sizeof(match));
for(i=1 ; i <= n ; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
result++;
}
printf("%d\n",result/2);
}
return 0;
}