HDU 1811 并查集

题意:

思路:topo+并查集

 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <iostream>
 6 using namespace std;
 7 vector<int > web[100010];
 8 int p[100010],n,m,sum,f[100010],A[100010],B[100010];
 9 char C[100010];
10 bool ok;
11 int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}
12 void order(){
13     queue<int> q;
14     for ( int i = 0 ; i < n ; ++i)
15         if ( f[i] == 0 && find(i) == i )
16             q.push(i);
17     while(!q.empty()){
18         if ( q.size() > 1) ok=true;///否则说明信息不完全
19         int cur = q.front();
20         q.pop();
21         sum--;
22         for(int i = 0 ; i < web[cur].size() ; ++i)
23         {
24             if(--f[web[cur][i]]==0)
25                 q.push(web[cur][i]);
26         }
27     }
28     return ;
29 }
30 int main(){
31     while (scanf("%d %d",&n,&m) != EOF){
32         ok = false ;sum = n;
33         for (int i = 0 ;i <= n;++i){
34             p[i] = i;
35             web[i].clear();
36             f[i] = 0 ;
37         }
38         for (int i = 0 ;i < m; ++i){
39             scanf("%d %c %d",&A[i],&C[i],&B[i]);
40             int x = find(A[i]),y = find(B[i]);
41             if (C[i] == '='){
42                 if (x != y){
43                     p[y] = x;
44                     sum--;
45                 }
46             }
47         }
48         for (int i = 0 ;i < m;++i){
49             if (C[i] == '=') continue;
50             int x = find(A[i]);
51             int y = find(B[i]);
52             if (C[i] == '>'){
53                 web[x].push_back(y);
54                 f[y]++;
55             }
56             else {
57                 web[y].push_back(x);
58                 f[x]++;
59             }
60         }
61         order();
62         if (sum > 0)  printf("CONFLICT\n");
63         else if (ok)  printf("UNCERTAIN\n");
64         else printf("OK\n");
65     }
66     return 0;
67 }
View Code

 

posted @ 2015-06-02 23:10  一麻袋码的玛侬  阅读(195)  评论(0编辑  收藏  举报