描述

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.

输入

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.

输出

For each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO".

样例输入

3 2
0 1
1 2
2 2
0 1
1 0
0 0

样例输出

YES
NO
题意

给你N个人编号0-N-1,M对关系,(a,b),a是b的师傅,判断所以关系是否合法

题解

一道简单的拓扑排序题,如果N的人存在拓扑排序输出YES,否则输出NO

拓扑排序

每次找一个入度为0的点(u),删去所有G[u][v]=1(u,v)的边,In[v]--

如果所有N个点都找过,输出YES

如果某个点没有找过,输出NO

代码

 1 #include<cstdio>
 2 int main()
 3 {
 4     int n,m,a,b;
 5     while(scanf("%d%d",&n,&m)!=EOF,n)
 6     {
 7         int In[105]={0},G[105][105]={0};
 8         for(int i=0;i<m;i++)
 9         {
10             scanf("%d%d",&a,&b);
11             if(G[a][b]==0)//避免重复
12             {
13                 In[b]++;
14                 G[a][b]=1;
15             }
16         }
17         int cnt=0;
18         while(cnt<n)
19         {
20             int p,flag=0;
21             for(int i=0;i<n;i++)
22             {
23                 if(In[i]==0)
24                 {
25                     p=i;
26                     flag=1;
27                     In[i]=-1;
28                     break;
29                 }
30             }
31             if(++cnt==n)//所有n个点都找过
32             {
33                 printf("YES\n");
34                 break;
35             }
36             if(flag==0)//如果某个点不行
37             {
38                 printf("NO\n");
39                 break;
40             }
41             for(int i=0;i<n;i++)
42             {
43                 if(G[p][i]==1)//删去边
44                 {
45                     In[i]--;
46                     G[p][i]=0;
47                 }
48             }
49         }
50     }
51     return 0;
52 }

转载于:https://www.cnblogs.com/taozi1115402474/p/8508007.html

HDU 3342 Legal or Not(拓扑排序)相关推荐

  1. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  2. HDU 3342 Legal or Not【拓扑排序】

    题意: 给出一些人的名次关系,问存不存在冲突的情况. 分析: 这题其实就是判断拓扑排序的过程中是否会出现环,判断环的方法是: 在某一次排序的过程中找不到入度为 0 的点,即说明有环. PS:  重边 ...

  3. HDU 4857 逃生 (反向拓扑排序 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  4. HDU 3342 Legal or Not

    在一个群里面,大家互相请教问题,比如A请教B,我们就把B叫做师傅,把A叫做徒弟,这样会产生很多"师傅--徒弟"的关系,一个徒弟可以有很多的师傅,一个师傅也可以有很多徒弟,这是合法的 ...

  5. [ACM] hdu 1285 确定比赛 (拓扑排序)

    确定比赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. 题解报告:hdu 5695 Gym Class(拓扑排序)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=5695 Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课 ...

  7. HDU 1285 确定比赛名次 拓扑排序(邻接矩阵 邻接表

    确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description ...

  8. HDU Problem 4857 逃生【拓扑排序+优先队列】

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. 【拓扑排序】确定比赛名次

    HDU P1285 确定比赛名次 拓扑排序裸题qwq 注意:入度为一的点删掉之后,它的入度要更新为-1 这个题刷出了我人生中第一次PE 可还行qaq 我搜索PE是输出格式与标准输出不符, 结果发现语言 ...

最新文章

  1. 下面首先来看GCD的使用
  2. php无法查询mysql字母,php – 无法在Multi MYSQL查询语句中获取结果
  3. 通过预训练提升语言理解
  4. 《温故而知新》JAVA基础四
  5. 在vivado hls软件上打开zynqnet工程,Vivado HLS Command Prompt(Vivado HLS 命令提示符)是什么
  6. 4. Storm可靠性
  7. python划分train val test
  8. 网站被黑检测-网页挂马及暗链检测
  9. H5游戏《守塔兵团》你必须要知道的4件事
  10. Android与iPhone的对比
  11. GitHub开源项目学习 电商系统Mall (一) Mall简介
  12. java汉诺塔5层攻略_史上最难智力游戏第5关汉诺塔图文通关攻略
  13. 几个冷门linux与BSD发行版中文学习论坛
  14. 计算24点游戏C语言课设
  15. 可口可乐市场调查失败的原因_可口可乐公司的市场调查为什么没有起到预期效果?...
  16. 服务器无线桥接怎么设置,高科路由器怎么设置无线中继 | 192路由网
  17. MPII姿态估计性能评价标准-PCK
  18. 求阶乘求1!+2!+…+20!,其中x!=1*2*…*x,表示阶乘
  19. VUE 解决双击事件与单击事件冲突(单击模拟双击)
  20. 《出奇制胜》读书笔记

热门文章

  1. PAT_B_1065_Java(17分)_C++(25分)
  2. oracle的打开图标,Oracle的SQL Developer 在Ubuntu上以图标显示且双击能运行
  3. 深度学习(33)随机梯度下降十一: TensorBoard可视化
  4. 光是无限远服务器有道馆吗,光是无限远服务器客户端
  5. C#时间与时间戳格式互相转化
  6. Codeforces Round #618 (Div. 2)-B. Assigning to Classes
  7. 【Tensorflow】tf.map_fn() 使用过程中遇到【inf, NaN】报错问题
  8. [z] 电子技术的老生常谈 —— 接地
  9. 熔池 沉积_用于3D打印的AI(第2部分):异常熔池检测的一课学习
  10. 看完这一系列,彻底搞懂 Gradle