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.
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.
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
题目大意:输入有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;
}
分享到:
相关推荐
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases. In each test case, there are two integers k,m(1≤k≤1018,1≤m≤100). Output For each test case, print ...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦
hdu题目分类
HDU图论题目分类