HDU 4751 Divide Groups (二分图染色)

题目

This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
After carefully planning, Tom200 announced his activity plan, one that contains two characters:

  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
  2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
    The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
    Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.

Input
The input contains several test cases, terminated by EOF.
Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.

Output
If divided successfully, please output "YES" in a line, else output "NO".

Sample Input
3
3 0
1 0
1 2 0

Sample Output
YES

思路

二分染色,恶心的是输入而已,其实还好

代码实现

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e3+10;
    vector <int> G[maxn];
    bool know[maxn][maxn];
    int color[maxn];
    int n;

    bool dfs(int v,int c) {
        color[v]=c;
        for (unsigned i=0;i<G[v].size();++i) {
            if (color[G[v][i]]==c) return false;
            if (color[G[v][i]]==0 &&!dfs(G[v][i],-c)) return false;
        }
        return true;
    }
    
    void check() {
        for (int i=1;i<=n;++i) {
            if (color[i]==0) {
                if (!dfs(i,1)) {
                    puts("NO");
                    return ;
                }
            }
        }
        puts("YES");
    }

    void solve () {
        while (~scanf ("%d",&n)) {
            memset (know,false,sizeof (know));
            for (int i=0;i<=n;i++) G[i].clear ();
            memset (color,0,sizeof (color));
            for (int i=1,a;i<=n;i++) {
                while (~scanf ("%d",&a)&&a) know[i][a]=true;
            }
            for (int i=1;i<=n;i++) 
            for (int j=i+1;j<=n;j++) {
                if (!know[i][j]||!know[j][i]) {
                    G[i].push_back (j);
                    G[j].push_back (i);
                }
            }
            check ();
        }

        return ;
    }

    int main () {
        int t;
        // scanf ("%d",&t);
        t=1;
        while (t--) {
            solve ();
        }
        return 0;
    }
原文地址:https://www.cnblogs.com/hhlya/p/13887369.html