The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5267 Accepted Submission(s): 2385
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
No
3
Source 2008 Asia Harbin Regional Contest Online
Recommend gaojie | We have carefully selected several similar problems for you: 2438 2443 2442 2441 2440
1、染色法判定二分图:
任意取一个点进行染色,如果发现要涂某一块时这个块已经被涂了色,并且与我们要使用的颜色不同的话,就说明这个图不能被染成BICOLORABLE的。
a、如果没有染色,将它染色,并将它周围的点变成相反色。
b、如果已经染色,判断是否与现在染色的点的颜色相同,相同,则退出,否则继续。
2、用匈牙利法求最大匹配数
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <map>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 205;
struct Edge {
int next;
int to;
} edge[MAXN * MAXN];
int n, m, head[MAXN];
int cnt;
bool vis[MAXN], color[MAXN];
int match[MAXN];
void AddEdge(int a, int b) {
edge[cnt].to = b;
edge[cnt].next = head[a];
head[a] = cnt++;
}
bool canFind(int u) {
vis[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next) {
int& v = edge[i].to;
if (match[v] == -1 || !vis[match[v]] && canFind(match[v])) {
match[v] = u;
return true;
}
}
return false;
}
int Hungary() {
int ans = 0;
memset(match, -1, sizeof(match));
for (int i = 1; i <= n; ++i) {
memset(vis, 0, sizeof(vis));
if (canFind(i)) {
++ans;
}
}
return ans;
}
bool IsBipartite(int u) {
vis[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next) {
int& v = edge[i].to;
if (!vis[v]) {
color[v] = !color[u];
if (!IsBipartite(v)) {
return false;
};
}
else if (color[u] == color[v]) {
return false;
}
}
return true;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (scanf("%d%d", &n, &m) != EOF) {
memset(head, -1, sizeof(head));
cnt = 0;
//这里用链式前向星存图
for (int i = 0, a, b; i < m; ++i) {
scanf("%d%d", &a, &b);
AddEdge(a, b);
AddEdge(b, a);
}
memset(vis, 0, sizeof(vis));
memset(color, 0, sizeof(color));
bool yes = true;
for (int i = 1; i <= n && yes; ++i) {
if (!vis[i]) {
yes = IsBipartite(i);
}
}
if (yes)
printf("%d\n", Hungary() >> 1);
else
printf("No\n");
}
return 0;
}