HDU - 2444 The Accomodation of Students (二分图染色 + 最大匹配)

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

题意:

有n个人,给出m个相互认识的关系,问能否把这些人分成两组,每组中没有人相互认识,两个相互认识的人可以被分到一个房间中,问最多需要安排多少间房

先染色判是否是二分图,再二分图最大匹配,最多的房间数也就是匹配数

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 +7;
const int N = 220;

int uN, vN;
int linker[N];
bool vis[N];
int color[N];
int head[N], tot;

struct Edge {
    int to, next;
}edge[100 * N];    //注意边数 >> 点数

void init() {
    tot = 0;
    memset(head, -1, sizeof(head));
    memset(edge, 0, sizeof(edge));
    memset(color, -1, sizeof(color));
    memset(linker, -1, sizeof(linker));
}

void addedge(int u, int v) {
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

bool set_color(int u, int col) {
    for(int i = head[u]; ~i; i = edge[i].next) {
        int v = edge[i].to;
        if(color[v] == -1) {
            color[v] = col ^ 1;
            if(!set_color(v, color[v])) return 0;
        }
        else if(color[v] == col) return 0;
    }
    return 1;
}

bool judge() {
    for(int i = 1; i <= uN; ++i) {
        if(color[i] == -1 && head[i] != -1) {
            color[i] = 0;
            if(!set_color(i, 0))
                return 0;
        }
    }
    return 1;
}

bool dfs(int u) {
    for(int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if(!vis[v]) {
            vis[v] = 1;
            if(linker[v] == -1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}

int hungary() {
    int res = 0;
    for(int u = 1; u <= uN; ++u) {
        memset(vis, 0, sizeof(vis));
        if(dfs(u))
            res++;
    }
    return res;
}

int main() {
    while(~scanf("%d%d", &uN, &vN)) {
        init();
        int u, v;
        for(int i = 1; i <= vN; ++i) {
            scanf("%d%d", &u, &v);
            addedge(u, v);
            addedge(v, u);
        }
        if(!judge()) printf("No\n");
        else printf("%d\n", hungary() / 2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/109099637
今日推荐