zoukankan      html  css  js  c++  java
  • 杭电3342--Legal or Not (拓扑排序)

    Legal or Not

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5920    Accepted Submission(s): 2743


    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 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
     

     

    Author
    QiuQiu@NJFU
     

     

    Source
     

     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2647 3333 3339 3341 3336 
    判环, 有向图中出现环路判断方法: <1> : 某次遍历过程中找不到入度为0的节点。 <2>: 假设该图可以进行拓扑排序,节点将全部输出(判断条件)。
    邻接矩阵:
     1  #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 int map[120][120], indegree[120];
     6 int n, m;
     7 bool Tsort()
     8 {
     9     int m;
    10     for(int i = 0; i < n; i++)
    11     {
    12         m = -1;
    13         for(int j = 0; j < n; j++)
    14             if(indegree[j] == 0)
    15             {
    16                 m = j;
    17                 break;
    18             }    
    19         if(m == -1)
    20             return false;
    21         indegree[m]--;
    22         for(int j = 0; j < n; j++)
    23             if(map[m][j] == 1)
    24                 indegree[j]--;
    25     }
    26     return true;
    27 } 
    28 int main()
    29 {
    30     int a, b;
    31     while(~scanf("%d %d", &n, &m), n+m)
    32     {
    33         memset(map, 0, sizeof(map));
    34         memset(indegree, 0, sizeof(indegree));
    35         for(int i = 0; i < m; i++)
    36         {
    37             scanf("%d %d", &a , &b);
    38             if(!map[a][b])
    39             {
    40                 map[a][b] = 1;
    41                 indegree[b]++;
    42             }
    43         }
    44         if(Tsort())
    45             printf("YES
    ");
    46         else
    47             printf("NO
    ");
    48     }
    49     return 0;
    50 }

    邻接表:

     1 #include <queue>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 int head[120], indegree[120];
     7 int ant, n, m;
     8 struct node
     9 {
    10     int to;
    11     int next;
    12 } map[120];
    13 void Add(int a, int b)  // Z or N? 
    14 {
    15     map[ant].to = b;
    16     map[ant].next = head[a];
    17     head[a] = ant++; 
    18 }
    19 bool Tsort()
    20 {
    21     priority_queue<int, vector<int>, greater<int> > q; int num = 0;
    22     for(int i = 0; i < n; i++)
    23         if(!indegree[i])
    24             q.push(i);
    25 //    printf("%d
    ", q.size());
    26     while(!q.empty())
    27     {
    28         int temp = q.top();
    29         q.pop(); num++;       /************************/
    30         indegree[temp]--;
    31         for(int i = head[temp]; i != -1; i = map[i].next)
    32         {
    33             if(!--indegree[map[i].to])
    34                 q.push(map[i].to); 
    35         }
    36     }
    37     if(num == n)
    38         return true;
    39     else
    40         return false;
    41 } 
    42 int main()
    43 {
    44     while(~scanf("%d %d", &n, &m), n+m)
    45     {
    46         ant = 0;
    47         memset(head, -1, sizeof(head));
    48         memset(indegree, 0, sizeof(indegree));
    49         for(int i = 0; i < m; i++)
    50         {
    51             int a, b;
    52             scanf("%d %d", &a, &b);
    53             Add(a, b);
    54             indegree[b]++;    //易忘; 
    55         }
    56         if(Tsort())
    57             printf("YES
    ");
    58         else
    59             printf("NO
    ");
    60     }
    61     return 0; 
    62 } 
  • 相关阅读:
    Oracle Grid 11.2.0.4 安装是出现“[INS-41112] Specified network interface doesnt maintain connectivity across cluster”错误
    复旦大学2018--2019学年第二学期(18级)高等代数II期末考试第七大题解答
    复旦大学2018--2019学年第二学期(18级)高等代数II期末考试第八大题解答
    复旦大学高等代数在线课程2018--2019学年记录
    凝心聚力、创建一流 —“综合性高校高等代数课程教学研讨会”在复旦举行
    复旦高等代数II(18级)每周一题
    复旦大学高等代数在线课程2018--2019学年学习情况分析及文字评教信息
    复旦大学2018--2019学年第一学期高等代数I期末考试情况分析
    复旦大学2018--2019学年第一学期(18级)高等代数I期末考试第七大题解答
    复旦大学2018--2019学年第一学期(18级)高等代数I期末考试第八大题解答
  • 原文地址:https://www.cnblogs.com/soTired/p/4730421.html
Copyright © 2011-2022 走看看