hdu 3342 Legal or Not - 拓扑排序
2017-07-10 11:57
465 查看
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
Sample Output
prentice向master连一条边,然后跑拓扑排序,跑完判一下是否所有点都进过队了。
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
/** * hdu * Problem#3342 * Accepted * Time:31ms * Memory:1820k */ #include <iostream> #include <cstdio> #include <ctime> #include <cmath> #include <cctype> #include <cstring> #include <cstdlib> #include <fstream> #include <sstream> #include <algorithm> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <stack> #ifndef WIN32 #define Auto "%lld" #else #define Auto "%I64d" #endif using namespace std; typedef bool boolean; const signed int inf = (signed)((1u << 31) - 1); const double eps = 1e-6; const int binary_limit = 128; #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) template<typename T> inline boolean readInteger(T& u){ char x; int aFlag = 1; while(!isdigit((x = getchar())) && x != '-' && x != -1); if(x == -1) { ungetc(x, stdin); return false; } if(x == '-'){ x = getchar(); aFlag = -1; } for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0'); ungetc(x, stdin); u *= aFlag; return true; } ///map template starts typedef class Edge{ public: int end; int next; Edge(const int end = 0, const int next = 0):end(end), next(next){} }Edge; typedef class MapManager{ public: int ce; int *h; Edge *edge; MapManager(){} MapManager(int points, int limit):ce(0){ h = new int[(const int)(points + 1)]; edge = new Edge[(const int)(limit + 1)]; memset(h, 0, sizeof(int) * (points + 1)); } inline void addEdge(int from, int end){ edge[++ce] = Edge(end, h[from]); h[from] = ce; } inline void addDoubleEdge(int from, int end){ addEdge(from, end); addEdge(end, from); } Edge& operator [] (int pos) { return edge[pos]; } inline void clear() { delete[] edge; delete[] h; } }MapManager; #define m_begin(g, i) (g).h[(i)] ///map template ends int n, m; MapManager g; int* dag; inline boolean init() { readInteger(n); if(n == 0) return false; readInteger(m); g = MapManager(n, m); dag = new int[(n + 1)]; memset(dag, 0, sizeof(int) * (n + 1)); for(int i = 1, a, b; i <= m; i++) { readInteger(a); readInteger(b); g.addEdge(b, a); dag[a]++; } return true; } queue<int> que; inline void topu() { for(int i = 0; i < n; i++) if(!dag[i]) que.push(i); while(!que.empty()) { int e = que.front(); que.pop(); for(int i = m_begin(g, e); i; i = g[i].next) { int& eu = g[i].end; dag[eu]--; if(!dag[eu]) que.push(eu); } } } inline void solve() { topu(); for(int i = 0; i < n; i++) { if(dag[i]) { puts("NO"); return; } } puts("YES"); } inline void clear() { g.clear(); delete[] dag; } int main() { while(init()) { solve(); clear(); } return 0; }
相关文章推荐
- hdu 3342 Legal or Not(拓扑排序dfs)
- hdu 3342 Legal or Not(拓扑排序)
- hdu 3342 Legal or Not (拓扑排序)
- 【拓扑排序】hdu 3342 Legal or Not
- 【hdu 3342】【拓扑排序(DAG) BFS】Legal or Not
- HDU 3342 Legal or Not (有向图的拓扑排序)
- HDU 3342 Legal or Not(拓扑排序)
- hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
- hdu 3342 Legal or Not(拓扑排序)
- [ACM] hdu 3342 Legal or Not (拓扑排序)
- 拓扑排序 HDU 3342 Legal or Not
- hdu 3342 Legal or Not 拓扑排序
- HDU 3342 Legal or Not (拓扑排序)
- Hdu 3342 Legal or not(基于邻接表的BFS+队列的拓扑排序)
- HDU 3342 Legal or Not (拓扑排序)
- hdu 3342 Legal or Not(拓扑排序)
- 【HDU】3342 Legal or Not 拓扑排序
- HDU 3342 Legal or Not (拓扑排序)
- HDU 3342 Legal or Not【拓扑排序】
- hdu 3342 Legal or Not(拓扑排序)