首页 > 代码库 > Hdu 4751(2-SAT)

Hdu 4751(2-SAT)

题目链接

Divide Groups

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


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
33 01 01 2 0

 

Sample Output
YES
思路:对于任意一个游客,要么属于第一个集合,要么属于第二个集合,因为题中的“关系”是单向的,
所以当两个人之间只有一条单向边时就意味着:如果A属于第一个集合,那么B就一定属于第二个集合,反之亦然。
这样就可以建图了。
Accepted Code:
 1 /************************************************************************* 2     > File Name: 4751.cpp 3     > Author: Stomach_ache 4     > Mail: sudaweitong@gmail.com 5     > Created Time: 2014年08月09日 星期六 22时55分53秒 6     > Propose: 2-SAT 7  ************************************************************************/ 8 #include <cmath> 9 #include <string>10 #include <cstdio>11 #include <vector>12 #include <fstream>13 #include <cstring>14 #include <iostream>15 #include <algorithm>16 using namespace std;17 18 const int maxn = 102;19 bool a[maxn][maxn];20 struct SCC {21       int n;22     vector<int> g[maxn<<1];23     vector<int> rg[maxn<<1];24     vector<int> vs;25     bool vis[maxn<<1];26     int cmp[maxn<<1];27     void addEdge(int from, int to) {28         g[from].push_back(to);29         rg[to].push_back(from);30     }31     void init(int nn) {32         this->n = nn * 2;33         for (int i = 0; i <= n; i++) {34             g[i].clear();35             rg[i].clear();36         }37         vs.clear();38     }39     void dfs(int u) {40         vis[u] = true;41         for (int i = 0; i < (int)g[u].size(); i++) {42               int v = g[u][i];43               if (!vis[v]) dfs(v);44         }45         vs.push_back(u);46     }47     void rdfs(int u, int k) {48         vis[u] = true;49         cmp[u] = k;50         for (int i = 0; i < (int)rg[u].size(); i++) {51             int v = rg[u][i];52             if (!vis[v]) rdfs(v, k);53         }54     }55     int find_scc() {56         memset(vis, false, sizeof(vis));57         for (int i = 0; i < n; i++) if (!vis[i]) dfs(i);58         memset(vis, false, sizeof(vis));59         int k = 0;60         for (int i = (int)vs.size()-1; i >= 0; i--) 61              if (!vis[vs[i]]) rdfs(vs[i], k++);62         return k;63     }64 };65 SCC A;66 67 int main(void) {68       int n;69       while (~scanf("%d", &n)) {70           memset(a, false, sizeof(a));71           for (int i = 0; i < n; i++) {72             int to;73             while (scanf("%d", &to), to) {74                 a[i][to-1] = true;    75             }76         }77         A.init(n);78         for (int i = 0; i < n; i++) {79             for (int j = 0; j < n; j++) {80                   if (a[i][j]&&!a[j][i] || !a[i][j]&&a[j][i]) {81                     A.addEdge(i, j + n);82                     A.addEdge(i + n, j);83                 }84             }85         }86         A.find_scc();87         bool flag = true;88         for (int i = 0; i < n; i++) {89               if (A.cmp[i] == A.cmp[i+n]) {90                 flag = false;91                 break;92             }93         }94         puts(flag ? "YES" : "NO");95     }96     return 0;97 }