HDU 3342 Legal or Not, hdu3342
In A group, you ask questions from each other. For example, if A asks B, we call B master and A apprentice. This will lead to many relationships between masters and apprentices, one apprentice can have many masters, and one master can also have many apprentices. This is legal, but it cannot appear that A is A master of B and B is A master of, or A is B's apprentice and B is A's apprentice, or in A larger link. It is obvious that the question is to determine whether a given directed graph has a ring. After understanding this, the solution is very simple, that is, you can directly sort the topology, and count the number of nodes whose degree is 0 when the Topology Sorting is complete, if the number is equal to the number of nodes, there is no ring. Otherwise, there is a ring.
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4713 Accepted Submission (s): 2148
Problem DescriptionACM-DIY is a large QQ group where your excellent acmers get together. it is so harmonious that just like a big family. every day, daily "holy cows" like HH, hh, AC, ZT, KP, BF, Qinz and so on chat on-line to exchange their ideas. when someone has questions, warm-hearted cows like Lost will come to help. then the one being helped will call Lost "master", and Lost will have a n Ice "prentice ". by and by, there are using pairs of "master and prentice ". but then problem occurs: there are too processing masters and too processing prentices, how can we know whether it is legal or not?
We all know a master can have tables 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 3 xian for instant, HH is 3xian's master and, at the same time, 3 xian 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.
InputThe 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.
OutputFor each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO ".
Sample Input
3 20 11 22 20 11 00 0
Sample Output
YESNO
The idea of this topological order is to reduce the inbound degree of the subsequent node by 1 After accessing a node in the figure. If the inbound degree of the x node is 0, it means that the x node can be accessed later. Here, the stack is used to store nodes with an inbound value of 0 and not yet accessed.
#include <cstdio>#include <stack>#include <vector>using namespace std;const int N = 105;vector<int> adj[N];int in_cnt[N];stack<int> in_zero_st;bool top_sort(int n){int cnt = n, u, v;while (!in_zero_st.empty()){u = in_zero_st.top();--cnt;in_zero_st.pop();for (int i = 0; i < adj[u].size(); ++i){v = adj[u][i];--in_cnt[v];if (in_cnt[v] == 0)in_zero_st.push(v);}}if (cnt == 0)return true;return false;}int main(){int n, m, u, v;while (scanf("%d %d", &n, &m)){if (n == 0)break;memset(in_cnt, 0, sizeof(in_cnt));for (int i = 0; i < m; ++i){scanf("%d %d", &u, &v);adj[u].push_back(v);++in_cnt[v];}for (int i = 0; i < n; ++i){if (in_cnt[i] == 0)in_zero_st.push(i);}//true indicates legal false for the otherbool ret = top_sort(n);if (ret){printf("YES\n");}else{printf("NO\n");}for (int i = 0; i < n; ++i){adj[i].clear();}}return 0;}