2013 ACM/ICPC Asia Regional Nanjing Online hdu 4751 补图+二分图染色判定

Divide Groups

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


Problem Description

  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
 
Recommend
liuyiding
 
AC代码:
w[i][j]非零表示i,j之间有限制,不能在同一活动中,即不能同一集合:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define LL long long
#define ULL unsigned long long
#define UINT unsigned int
#define MAX_INT 0x7fffffff
#define MAX_LL 0x7fffffffffffffff
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

#define MAXN 111
int w[MAXN][MAXN];
int n;
vector<int> g[MAXN];
int col[MAXN];

bool dfs(int u, int cur){
    col[u]=cur;
    for(int i=0; i<g[u].size(); i++){
        int v=g[u][i];
        if(col[u]==col[v] || (!col[v] && !dfs(v, 3-cur))) return false;
    }
    return true;
}

int main(){
//  freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
    while(scanf(" %d", &n)==1 && n){
        int i,j;
        memset(w, -1, sizeof(w));
        for(i=1; i<=n; i++){
            g[i].clear();
            for(int tmp; scanf(" %d", &tmp)==1 && tmp; w[i][tmp]=0);
        }
        for(i=1; i<=n; i++)
            for(j=1+i; j<=n; j++) if(w[i][j] || w[j][i]){
                g[i].push_back(j);
                g[j].push_back(i);
            }
        memset(col, 0, sizeof(col));
        for(i=1; i<=n; i++) if(!col[i])
            if(!dfs(i, 1)) break;
        if(i>n) printf("YES
");
        else printf("NO
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ramanujan/p/3331701.html