欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 4751 Divide Groups(判断二分图)

程序员文章站 2022-06-02 17:38:06
...

Divide Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2982    Accepted Submission(s): 1043


Problem Description

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

 Source

2013 ACM/ICPC Asia Regional Nanjing Online

题目要求等价于这个图的补图为二分图,染色法判断一下是否为二分图就行了

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
typedef long long ll;
bool g[MAXN][MAXN];
vector<int> G[MAXN];
int color[MAXN];
bool bipartite(int u)
{
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if(color[v] == color[u]) return false;
        if(!color[v]) {
            color[v] = 3 - color[u];
            if(!bipartite(v)) return false;
        }
    }
    return true;
}
int main(void)
{
    int n;
    while(scanf("%d",&n) != EOF) {
        int id;
        memset(g,0,sizeof(g));
        for(int i = 1; i <= n; i++) {
            while(1) {
                scanf("%d",&id);
                if(id == 0) {
                    break;
                }
                g[i][id] = 1;
            }
            G[i].clear();
        }
        for(int i = 1; i <= n; i++) {
            for(int j = i + 1; j <= n; j++) {
                if(!g[i][j] || !g[j][i]) {
                    G[i].push_back(j);
                    G[j].push_back(i);
                }
            }
        }
        memset(color,0,sizeof(color));
        int flag = 0;
        for(int i = 1; i <= n; i++) {
            if(color[i] == 0) {
                color[i] = 1;
                if(!bipartite(i)) {
                    flag = 1;
                    break;
                }
            }
        }
        if(flag) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

 

相关标签: 二分图