hdu 3342 Legal or Not - 拓扑排序

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?

We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. 

Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.

Input

The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0. 
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.

Output

For each test case, print in one line the judgement of the messy relationship. 
If it is legal, output "YES", otherwise "NO".

Sample Input

3 2
0 1
1 2
2 2
0 1
1 0
0 0

Sample Output

YES
NO

  prentice向master连一条边,然后跑拓扑排序,跑完判一下是否所有点都进过队了。

Code

  1 /**
  2  * hdu
  3  * Problem#3342
  4  * Accepted
  5  * Time:31ms
  6  * Memory:1820k
  7  */
  8 #include <iostream>
  9 #include <cstdio>
 10 #include <ctime>
 11 #include <cmath>
 12 #include <cctype>
 13 #include <cstring>
 14 #include <cstdlib>
 15 #include <fstream>
 16 #include <sstream>
 17 #include <algorithm>
 18 #include <map>
 19 #include <set>
 20 #include <stack>
 21 #include <queue>
 22 #include <vector>
 23 #include <stack>
 24 #ifndef WIN32
 25 #define Auto "%lld"
 26 #else
 27 #define Auto "%I64d"
 28 #endif
 29 using namespace std;
 30 typedef bool boolean;
 31 const signed int inf = (signed)((1u << 31) - 1);
 32 const double eps = 1e-6;
 33 const int binary_limit = 128;
 34 #define smin(a, b) a = min(a, b)
 35 #define smax(a, b) a = max(a, b)
 36 #define max3(a, b, c) max(a, max(b, c))
 37 #define min3(a, b, c) min(a, min(b, c))
 38 template<typename T>
 39 inline boolean readInteger(T& u){
 40     char x;
 41     int aFlag = 1;
 42     while(!isdigit((x = getchar())) && x != '-' && x != -1);
 43     if(x == -1) {
 44         ungetc(x, stdin);    
 45         return false;
 46     }
 47     if(x == '-'){
 48         x = getchar();
 49         aFlag = -1;
 50     }
 51     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
 52     ungetc(x, stdin);
 53     u *= aFlag;
 54     return true;
 55 }
 56 
 57 ///map template starts
 58 typedef class Edge{
 59     public:
 60         int end;
 61         int next;
 62         Edge(const int end = 0, const int next = 0):end(end), next(next){}
 63 }Edge;
 64 
 65 typedef class MapManager{
 66     public:
 67         int ce;
 68         int *h;
 69         Edge *edge;
 70         MapManager(){}
 71         MapManager(int points, int limit):ce(0){
 72             h = new int[(const int)(points + 1)];
 73             edge = new Edge[(const int)(limit + 1)];
 74             memset(h, 0, sizeof(int) * (points + 1));
 75         }
 76         inline void addEdge(int from, int end){
 77             edge[++ce] = Edge(end, h[from]);
 78             h[from] = ce;
 79         }
 80         inline void addDoubleEdge(int from, int end){
 81             addEdge(from, end);
 82             addEdge(end, from);
 83         }
 84         Edge& operator [] (int pos) {
 85             return edge[pos];
 86         }
 87         inline void clear() {
 88             delete[] edge;
 89             delete[] h;
 90         }
 91 }MapManager;
 92 #define m_begin(g, i) (g).h[(i)]
 93 ///map template ends
 94 
 95 int n, m;
 96 MapManager g;
 97 int* dag;
 98 
 99 inline boolean init() {
100     readInteger(n);
101     if(n == 0)    return false;
102     readInteger(m);
103     g = MapManager(n, m);
104     dag = new int[(n + 1)];
105     memset(dag, 0, sizeof(int) * (n + 1));
106     for(int i = 1, a, b; i <= m; i++) {
107         readInteger(a);
108         readInteger(b);
109         g.addEdge(b, a);
110         dag[a]++;
111     }
112     return true;
113 }
114 
115 queue<int> que;
116 inline void topu() {
117     for(int i = 0; i < n; i++)
118         if(!dag[i])
119             que.push(i);
120     while(!que.empty()) {
121         int e = que.front();
122         que.pop();
123         for(int i = m_begin(g, e); i; i = g[i].next) {
124             int& eu = g[i].end;
125             dag[eu]--;
126             if(!dag[eu])
127                 que.push(eu);
128         }
129     }
130 }
131 
132 inline void solve() {
133     topu();
134     for(int i = 0; i < n; i++) {
135         if(dag[i]) {
136             puts("NO");
137             return;
138         }
139     }
140     puts("YES");
141 }
142 
143 inline void clear() {
144     g.clear();
145     delete[] dag;
146 }
147 
148 int main() {
149     while(init()) {
150         solve();
151         clear();
152     }
153     return 0;
154 }
posted @ 2017-07-10 11:57  阿波罗2003  阅读(232)  评论(0编辑  收藏  举报