zoukankan      html  css  js  c++  java
  • HDU 3342 Legal or Not(判断是否存在环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342

    Legal or Not

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


    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
     
    题目大意:有N个人,给出M组两个人之间的师徒关系,问是否存在悖论,即某个人的徒弟又是自己的师傅。
     
    解题思路:用拓扑排序判断图中是否存在环即可
     
    AC代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 queue <int > q;
     8 int line[110][110];
     9 int in[110];
    10 int main ()
    11 {
    12     int i,j,m,n,num,u,v;
    13     while (scanf("%d%d",&n,&m)!=EOF)
    14     {
    15         if (n == 0)
    16             break;
    17         memset(line,0,sizeof(line));
    18         memset(in,0,sizeof(in));
    19 
    20         for (i = 0; i < m; i ++)
    21         {
    22             scanf("%d%d",&u,&v);  
    23             if (line[u][v]==0)  //判断重边
    24             {
    25                 line[u][v] ++;
    26                 in[v] ++;
    27             }
    28         }
    29         for (i = 0; i < n; i ++)
    30             if (in[i] == 0) q.push(i);  //入度为0的存入队列
    31         num = 0;
    32         while (!q.empty())
    33         {
    34             int k = q.front();
    35             q.pop();
    36             num ++;
    37             for (i = 0; i < n; i ++)
    38             {
    39                 if (line[k][i] > 0)  //删掉与该入度为0的节点相连的边
    40                 {
    41                     in[i] --;   
    42                     if (in[i] == 0)
    43                         q.push(i);
    44                 }
    45             }
    46         }
    47         if (num == n)    //判断是否存在环
    48             printf("YES
    ");
    49         else
    50             printf("NO
    ");
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    SQL去除重复记录
    FullCalendar应用——整合农历节气和节日
    Dropzone.js实现文件拖拽上传
    HTML5实现文件断点续传
    FullCalendar日历插件说明文档
    网络电影免会员播放器
    百度网盘搜索工具
    HTML5学习
    HTML2 -- 布局格式
    JS10 -- url问号后的数据
  • 原文地址:https://www.cnblogs.com/yoke/p/6080646.html
Copyright © 2011-2022 走看看