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 }
时间: 2024-10-27 10:53:16

hdu 3342 Legal or Not - 拓扑排序的相关文章

hdu 3342 Legal or Not 拓扑排序判断环。 现在的我,除了刷水题,,还能干什么

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4960    Accepted Submission(s): 2270 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

HDU 1258 确定比赛名次 &amp;&amp;HDU 3342 Legal or Not 【临接表+拓扑排序】

HDU 1258 链接:click here HDU 3342 链接:click here 题意: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14142    Accepted Submission(s): 5667 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,

[ACM] hdu 3342 Legal or Not (拓扑排序)

Legal or Not Problem Description 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 excha

HDU 3213 Box Relations(拓扑排序构造)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题意:有n个长方体,四种限制条件.(1)I x y x和y有相交:(2)X/Y/Z  x y x的最大X/Y/Z坐标小于y的最大X/Y/Z.构造出这样的n个长方体. 思路:首先,XYZ三个方向是可以分开考 虑的.那么我们可以一个个分别求解.将每个长方体拆成左上角右下角两个点,我们假设现在考虑X方向,也即是一个长方体对应两个X方向的点,共2*n个点, 边<i,j>表示i小于j,那么首先有边&l

HDU 2647 Reward(图论-拓扑排序)

Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards. The workers will compare their rewards ,a

HDU 2467 Reward(逆拓扑排序)

拓扑排序的变形,逆序建图就好了 Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3951    Accepted Submission(s): 1203 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival

HDU 4324 Triangle LOVE (拓扑排序)

Triangle LOVE Problem Description Recently, scientists find that there is love between any of two people. For example, between A and B, if A don't love B, then B must love A, vice versa. And there is no possibility that two people love each other, wh

HDU 3342 Legal or Not(拓扑排序判环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目: Problem Description 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, lc

HDU 3342 Legal or Not(拓扑排序判断成环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即可. 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int N=1e2+5; 6 7 int n,m; 8 int vis[N];