#HDU 2444The Accomodation of Students (discriminating method bipartite graph and the maximum matching principle)


 

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

Subject to the effect: given N and M individual pairs, each pair of input representation they know, people only know to live in a room, how many can live up to ask right? If it is not divided into two groups, the output "NO"

Ideas: First, can see that this is a half maximum matching problem, but must first determine whether the graph is not bipartite graph bipartite graph with only two variables or better judgment, with 1 indicating that the point is black, 2 represents the point white (and vice versa does not matter), as long as they let all the figures are colored and an adjacent two points is not the same color, then this figure is a bipartite graph of. But this question there is a problem, he did not tell you bipartite graph which is the first set point, which is the second set of points, so we can save directly to both sides, and finally get out of the results ➗ 2 on OK.

AC Code:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 2e2 + 5;

int p[maxn][maxn], net[maxn], op[maxn], n, m;
bool vis[maxn];
void init() {
    memset(p, 0, sizeof(p));
    memset(net, 0, sizeof(net));
    memset(op, 0, sizeof(op));
}
bool bfs() {
    queue <int> q;
    q.push(1);
    op[1] = 1;
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        for (int i = 1; i <= n; i++) {
            if (p[now][i]) {
                if (!op[i]) {
                    if (op[now] == 1) op[i] = 2;
                    else op[i] = 1;
                    q.push(i);
                }
                else if (op[now] == op[i]) return 0;
            }
        }
    }
    return 1;
}
bool find_(int x) {
    for (int i = 1; i <= n; i++) {
        if (!vis[i] && p[x][i]) {
            vis[i] = 1;
            if (!net[i] || find_(net[i])) {
                net[i] = x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    while (cin >> n >> m) {
        init();
        for (int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            p[u][v] = p[v][u] = 1;
        }
        if (!bfs()) cout << "No" << endl;
        else {
            int ans = 0;
            for (int i = 1; i <= n; i++) {
                memset(vis, 0, sizeof(vis));
                if (find_(i)) ans++;
            }
            cout << (ans >> 1) << endl;
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/91383928