HDU 4751 Divide Groups【二分图染色】

来源:互联网 发布:零用钱大作战mac 编辑:程序博客网 时间:2024/06/05 10:37

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

题意:将n个人分到两个集合,且每个集合中的人任意两者之间都相互认识((不同集合的不用管).
思路:小技巧主要在建图上,把和i点不相互认识的点建边,那么两点之间没建边的就是相互认识了(相互认识未必一定要在同一集合),当然所建的图未必只有一个。然后dfs进行染色,遇到同一条边的两点具有同一种颜色,那么直接NO.

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <queue>using namespace std;const int MAXN = 110;int ma[MAXN][MAXN];int vis[MAXN];vector<int> v[MAXN];bool dfs(int x, int col) {    vis[x] = col;    for(int i = 0; i < v[x].size(); i++) {        int cnt = v[x][i];        if(vis[cnt] == vis[x]) return false; //邻居同色         else if(vis[cnt] == -1 && !dfs(cnt, !col)) return false;    }    return true;}void init() {    memset(ma, 0, sizeof(ma));    memset(vis, -1, sizeof(vis));    for(int i = 0; i < MAXN; i++) {        v[i].clear();     }}int main() {    int n, k;    while(scanf("%d", &n) != EOF) {        init();bool flag = true;        for(int i = 1; i <= n; i++) {            while(scanf("%d", &k) && k) {                ma[i][k] = 1;            }        }        for(int i = 1; i <= n; i++) {            for(int j = i + 1; j <= n; j++) {                if(!ma[i][j] || !ma[j][i]) { //建图                     v[i].push_back(j);                    v[j].push_back(i);                }            }        }         for(int i = 1; i <= n; i++) {            if(vis[i] == -1 && !dfs(i, 0)) { //不止一张图                 flag = false;                break;            }        }        if(flag) printf("YES\n");        else printf("NO\n");    }    return 0;} 
原创粉丝点击