The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4306 Accepted Submission(s): 1975
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
题意:首先判断该图能否构成二分图,然后如果是的话就将它进行最大匹配。
这题主要在于如何判断能否构成二分图,我采用的是dfs的方法,将所有的点分成两类,然后一个个遍历看是否出现矛盾。当然,也可以用bfs来查找,只不过代码量会稍稍大一点儿,原理都是一样的,就是判断所有的点分成两类是否矛盾。两种类型我用color数组来表示的,相当于上色的意思,刚开始所有的color都是-1,然后将不同类型的分成0和1两种颜色。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int N=555;
bool tu[N][N];
int from[N];///记录右边的点如果配对好了它来自哪里
bool use[N];///记录右边的点是否已经完成了配对
int color[N];
int n,m;
bool dfs(int x)
{
for(int i=1; i<=m; i++) ///m是右边,所以这里上界是m
if(!use[i]&&tu[x][i])
{
use[i]=1;
if(from[i]==-1||dfs(from[i]))
{
from[i]=x;
return 1;
}
}
return 0;
}
int hungary()
{
int tot=0;
memset(from,-1,sizeof(from));
for(int i=1; i<=n; i++) ///n是左边,所以这里上界是n
{
memset(use,0,sizeof(use));
if(dfs(i))
tot++;
}
return tot;
}
bool judge(int x)
{
for(int i=1;i<=n;i++)
if(i!=x&&tu[x][i])
{
if(color[i]==-1)
{
color[i]=(color[x]+1)%2;
if(!judge(i))
return 0;
}
else if(color[i]==color[x])
return 0;
}
return 1;
}
int main()
{
int k;
while(cin>>n>>k)
{
m=n;
memset(tu,0,sizeof(tu));
memset(color,-1,sizeof(color));
bool flag=0;
for(int i=0; i<k; i++)
{
int x,y;
cin>>x>>y;
tu[x][y]=tu[y][x]=1;
}
if(n==1)
{printf("No\n");continue;}
color[1]=0;
flag=judge(1);
if(!flag)
printf("No\n");
else
printf("%d\n",hungary()/2);
}
return 0;
}