HDU3342 Legal or not "topological sort" "Chain forward Star"

Source: Internet
Author: User

Legal or not

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)Total Submission (s): 4633 Accepted Submission (s): 2115
Problem Description
Acm-diy is a large QQ group where many excellent acmers get together. It's so harmonious this just like a big family. Every Day,many "holy cows" like HH, hh, AC, ZT, LCC, BF, Qinz and so on chats on-line to exchange their ideas. When someone had questions, many warm-hearted cows like Lost would come to help. Then the one being helped would call Lost "master", and Lost would have a nice "Prentice". By and by, there is many pairs of "Master and Prentice". But then problem Occurs:there was too many masters and too many prentices, how can we know whether it's legal or not?

We all know a master can has many prentices and a Prentice may has a lot of masters too, it ' s legal. Nevertheless,some cows is not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH was 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 are legal or not.

Please note that the ' Master and Prentice ' relation is transitive. It means 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-integers, N (members to is 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 are 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 on one line the judgement of the messy relationship.
If It is legal, output "YES", otherwise "NO".

Sample Input
3 2
0 1
0 S
2 2
0 1
1 0
0 0

Sample Output
YES

NO


The main topic: give you a map to determine whether there is a ring.

Idea: To construct a topological sort, if the sort fails, indicates that the forward graph has a forward loop.

Another way of thinking, using the chain forward to the star storage diagram, data input at the same time to count the degree of each point,

and deposit the Indegree array, each deleting a point, traversing the edge as the starting point, the Edge

The corresponding entry minus 1 selects and deletes the next point. Use a queue to store the found 0 in the

Update the queue at the same time as the update-in degree. If you finally get the number of elements in the queue less than

The total number of elements, indicating that the sorting failed, existence ring.

The first method:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;int n,m,t;int topo[110],vis[110],g[110][110];bool dfs (int u) {Vis[u] =-1;            for (int v = 0; v < N; v++) {if (G[u][v]) {if (Vis[v] < 0) return false;        else if (!vis[v] &&!dfs (v)) return false;    }} Vis[u] = 1;    TOPO[--T] = u; return true;}    BOOL Toposort () {t = N;    memset (vis,0,sizeof (VIS));    for (int u = 0; u < N; u++) if (!vis[u]) if (!dfs (U)) return false; return true;}    int main () {int a A, B; while (Cin >> N >> M) {if (! N &&!        M) break;        memset (g,0,sizeof (G));            for (int i = 0; i < M; i++) {cin >> a >> b;        G[A][B] = 1;        } if (Toposort ()) cout << "YES" << Endl; else cout << "NO"<< Endl; } return 0;}
The second method:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    std;const int MAXN = 110;const int maxm = 110;int head[maxn],queue[maxn],indegree[maxn],n,m;struct EdgeNode{int to;    int W; int next;};    Edgenode Edges[maxm];bool Toposort () {int iq = 0;    for (int i = 0; i < N; i++) {if (indegree[i] = = 0) queue[iq++] = i;            } for (int i = 0; i < IQ; i++) {for (int k = head[queue[i]]; K! =-1; k = edges[k].next) {            indegree[edges[k].to]--;            if (indegree[edges[k].to] = = 0) {queue[iq++] = edges[k].to;    }}} if (iq = = N) return true; return false;}    int main () {int x, y; while (Cin >> N >> M) {if (! N &&!        M) break;        memset (edges,0,sizeof (Edges));        memset (head,-1,sizeof (head));        memset (indegree,0,sizeof (Indegree));    memset (queue,0,sizeof (queue));    for (int i = 0; i < M; i++) {cin >> x >> y;            edges[i].to = y;            EDGES[I].W = 1;            Edges[i].next = Head[x];            HEAD[X] = i;        indegree[y]++;        } if (Toposort ()) cout << "YES" << Endl;    else cout << "NO" << Endl; } return 0;}




HDU3342 Legal or not "topological sort" "Chain forward Star"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.