The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8233 Accepted Submission(s): 3630
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
No
3
一、原题地址
二、大致题意
一群人给出互相认识的关系,然后将他们分作两组,这两组内部的人不能是认识的关系。询问有多少的匹配关系。
三、思路
用BFS判断是否为二分图。若是,二分匹配最大值。
四、代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long int
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
int nn, mm;
int n, m;
vector<int > mmp[205];
int cnt[250];
bool vis[250];
int link[205];
int x[205], y[205];
struct Node
{
int id, color;
}p;
bool BFS()
{
queue<Node>q;
bool v[250];
memset(v, false, sizeof(v));
memset(cnt, -1, sizeof(cnt));
for (int i = 1; i <= nn; i++)
{
if (!v[i])
{
p.color = 1; p.id = i;
cnt[i] = 1;
q.push(p);
v[p.id] = true;
while (!q.empty())
{
Node t = q.front();
q.pop();
int size = mmp[t.id].size();
for (int i = 0; i < size; i++)
{
int next = mmp[t.id][i];
if (v[next])
{
if (t.color == cnt[next])
return false;
else continue;
}
else
{
Node tt;
tt.id = next;
if (t.color == 1)tt.color = 0;
else tt.color = 1;
q.push(tt);
cnt[tt.id] = tt.color;
v[tt.id] = true;
}
}
}
}
}
return true;
}
int can(int t)
{
for (int i = 0; i < mmp[t].size(); i++) {
int tmp = mmp[t][i];
if (!vis[tmp]) {
vis[tmp] = true;
if (link[tmp] == -1 || can(link[tmp])) {
link[tmp] = t;
return 1;
}
}
}
return 0;
}
int maxmatch()
{
int num = 0;
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (can(x[i])) {
num++;
}
}
return num;
}
int main()
{
while (scanf("%d%d", &nn, &mm) != EOF)
{
memset(mmp, 0, sizeof(mmp));
memset(cnt, -1, sizeof(cnt));
for (int i = 0; i < mm; i++)
{
int x, y;
scanf("%d%d", &x, &y);
mmp[x].push_back(y);
mmp[y].push_back(x);
}
bool flag=BFS();
if (!flag)
{
printf("No\n");
}
else
{
n = m = 0;
memset(link, -1, sizeof(link));
memset(vis, false, sizeof(vis));
for (int i = 1; i <= nn; i++)
{
if (cnt[i])
{
x[++n] = i;
}
else
{
y[++m] = i;
}
}
printf("%d\n", maxmatch());
}
}
getchar();
getchar();
}