The Accomodation of Students
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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
题目大意:输入有n个人和m对关系,要求把他们分成两组,每组的人中不能有互相认识的,如果不可以分成这样的两组,输出NO,否则输出这两组中最多人的人数。
这个也是二分最大匹配,判断能否分成两组,这个用染色法,黑白染色,如果有矛盾则不能分成两组,否则可以二分匹配,每对关系就是边的关系,然后一个二分匹配算法求最大匹配就是答案了!
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <vector>
using namespace std;
const int N = 205;
vector<int> map[N];
bool flag[N], go[N];
int pre[N], col[N];
int n, m;
bool dfs(int cur, int tar)
{
int i, k;
for(i = 0; i < map[cur].size(); i++)
{
k = map[cur][i];
if(col[k] == -1)
{
go[k] = true;
col[k] = tar^1;
if(!dfs(k, col[k])) return false;
}
else if(col[k] == (tar^1)) go[k] = true;
else if(col[k] == tar) return false;
}
return true;
}
int find(int cur)
{
int i, k;
for(i = 0; i < map[cur].size(); i++)
{
k = map[cur][i];
if(!flag[k])
{
flag[k] = true;
if(pre[k] == -1 || find(pre[k]))
{
pre[k] = cur;
return 1;
}
}
}
return 0;
}
void init()
{
int i;
for(i = 0; i <= n; i++) map[i].clear();
memset(pre, -1, sizeof(pre));
memset(col, -1, sizeof(col));
memset(go, false, sizeof(go));
}
int main()
{
int i, x, y, tar, sum;
while(scanf("%d %d", &n, &m) != EOF)
{
init();
for(i = 0; i < m; i++)
{
scanf("%d %d", &x, &y);
map[x].push_back(y);
}
tar = false;
for(i = 1; i <= n; i++)
{
if(go[i]) continue;
col[i] = 1;
if(!dfs(i, col[i]))
{
tar = true;
break;
}
}
if(tar)
{
printf("No\n");
continue;
}
sum = 0;
for(i = 1; i <= n; i++)
{
memset(flag, false, sizeof(flag));
sum += find(i);
}
printf("%d\n", sum);
}
return 0;
}