Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 565 Accepted Submission(s): 216
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.
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
每个人要么属于第一个集合,要么属于第二个集合,赤裸裸的2-sat 啊!
#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100 + 5;
const int INF = 10000 + 5;
struct TwoSAT {
int n;
vector<int> G[maxn*2];
bool mark[maxn*2];
int S[maxn*2], c;
bool dfs(int x) {
if (mark[x^1]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = 0; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
}
void init(int n) {
this->n = n;
for (int i = 0; i < n*2; i++) G[i].clear();
memset(mark, 0, sizeof(mark));
}
//kind=0 -> &&;kind=1 -> ||;kind=2 -> ^;
void add_edge(int a,int b,int c,int kind){
if(kind == 0){
if(c == 0){
G[2*a].push_back(2*b+1);
G[2*b].push_back(2*a+1);
}
else{
G[2*a+1].push_back(2*a);
G[2*b+1].push_back(2*b);
}
}
else if(kind == 1){
if(c == 0){
G[2*a].push_back(2*a+1);
G[2*b].push_back(2*b+1);
}
else{
G[2*a+1].push_back(2*b);
G[2*b+1].push_back(2*a);
}
}
else{
if(c == 0){
G[2*a].push_back(2*b);
G[2*a+1].push_back(2*b+1);
G[2*b].push_back(2*a);
G[2*b+1].push_back(2*a+1);
}
else{
G[2*a].push_back(2*b+1);
G[2*a+1].push_back(2*b);
G[2*b].push_back(2*a+1);
G[2*b+1].push_back(2*a);
}
}
}
bool solve() {
for(int i = 0; i < n*2; i += 2){
if(!mark[i] && !mark[i+1]) {
c = 0;
if(!dfs(i)) {
while(c > 0) mark[S[--c]] = false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
};
TwoSAT solver;
int M[maxn][maxn];
int main(){
int n;
while(scanf("%d",&n) != EOF){
solver.init(n);
memset(M,0,sizeof(M));
for(int i = 0;i < n;i++){
int x;
while(scanf("%d",&x)){
if(x == 0) break;
x--;
M[i][x] = 1;
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(i == j) continue;
if(M[i][j] == 0 || M[j][i] == 0){
solver.add_edge(i,j,1,2); }
}
}
if(solver.solve()) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}