Legal or Not

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

  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

题目大意:

在一个群里面,大家互相请教问题,比如A请教B,我们就把B叫做师傅,把A叫做徒弟,这样会产生很多“师傅——徒弟”的关系,一个徒弟可以有很多的师傅,一个师傅也可以有很多徒弟,这是合法的,但是不能出现A是B的师傅而且B是A的师傅,或者A是B的徒弟而且B是A的徒弟,或者在一个更大的关系环里面出现这种情况。

思路:

很明显题目的意思就是,判断一个给定的有向图中是否存在环。了解了这些,解题方法就非常简单了,那就是直接进行拓扑排序即可,统计拓扑排序完成之时能记录的度为0的节点的个数,若个数等于节点个数则说明无环,否则是有环的。

注意·:

该题为输入多组数据,所以我们每一次进行询问和查询的时候都要将所有的数组以及sum,tot等记录的值清空。

代码:

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1000
using namespace std;
int n,m,a,b,x,sum,tot;
int head[N],in[N];
queue<int>q;
struct Edge
{int from,to,next;
}edge[N];
void add(int x,int y)
{tot++;edge[tot].to=y;edge[tot].next=head[x];head[x]=tot;
}
int main()
{while(scanf("%d%d",&n,&m)){while(!q.empty()) q.pop();tot=0,sum=0;memset(in,0,sizeof(in));memset(head,0,sizeof(head));if(n==0) return 0;for(int i=1;i<=m;i++)scanf("%d%d",&a,&b),add(a,b),in[b]++;for(int i=0;i<n;i++)if(in[i]==0) q.push(i);while(!q.empty()){x=q.front();q.pop();sum++;for(int i=head[x];i;i=edge[i].next){in[edge[i].to]--;if(in[edge[i].to]==0) q.push(edge[i].to);}}if(sum!=n) printf("NO\n");else printf("YES\n");}
}

转载于:https://www.cnblogs.com/z360/p/6978846.html

Legal or Not相关推荐

  1. 【Paper】Few-Shot Charge Prediction with Discriminative Legal Attributes

    传送门: paper--COLING2018 paper. codes 文章目录 Abstract 1 Introduction 2 Related Work 2.1 Zero-Shot Classi ...

  2. 【Paper】Learning to Predict Charges for Criminal Cases with Legal Basis

    文章目录 Abstract 1 Introduction 2 相关工作 3 Data Preparation 4 Our Approach 4.1 Document Encoder 4.2 Using ...

  3. Part 1 — Introduction to Smart (legal?) Contracts

    A version of this article first appeared in Coindesk on April 11, 2016. Encouraged by the feedback t ...

  4. 如何判断derived-to-base conversion是否legal

    我们知道,在类内的access specifier public .protected.private都只是为了限定用户程序对类内的访问权限,而在继承list中的access specifier则是影 ...

  5. HDU 3342 Legal or Not(拓扑排序)

    描述 ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that ju ...

  6. 【HDU - 3342】Legal or Not(拓扑排序)

    题干: ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that j ...

  7. Re32:读论文 Summarizing Legal Regulatory Documents using Transformers

    诸神缄默不语-个人CSDN博文目录 论文名称:Summarizing Legal Regulatory Documents using Transformers 论文下载地址:https://dl.a ...

  8. Medical robotics-Regulatory, ethical, and legal considerations for increasing levels of autonomy

    借鉴自动驾驶技术自主化等级的划分,大佬们对医疗机器人领域中的自主化(自治化)等级进行了划分和表述.非常有参考意义,可以尝试把自己的机器人归入其中哪一类,提升等级~ 医疗机器人-对提升的自主化等级的规范 ...

  9. ios appstore 审核 Guideline 5.2.2 - Legal ios新闻资讯类APP5.2.2被拒

    新闻资讯类APP5.2.2被拒. 新闻资讯类APP5.2.2被拒,说是上传什么资质 Guideline 5.2.2 - Legal Your app contains content or featu ...

  10. 论文阅读:How Does NLP Benefit Legal System:A Summary of LAI如何使用NLP技术帮助法律智能:关于法律智能的综述

    How Does NLP Benefit Legal System:A Summary of Legal ArtificialIntelligence Intelligence 如何使用NLP技术帮助 ...

最新文章

  1. 8天学通MongoDB——第五天 主从复制
  2. WPF绘制光滑连续贝塞尔曲线
  3. 雷军玩谐音梗:称米粉为“小粽子” 因为粽子“心中有MI”
  4. php 计算日期差几周,PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)
  5. iOS核心动画 - CALayer
  6. Flash as3.0 保存MovieClip运动轨迹到json文件
  7. .NET实现中英文验证码
  8. python字典常见操作
  9. WIN10电脑自动关机命令
  10. 理解 HTTP 幂等性
  11. 数据库索引:位图索引
  12. 计算机基础知识(上)(硬件篇)
  13. 0day安全:软件漏洞分析技术 学习分析记录
  14. 中国最小黑客:为不做作业 黑掉学校系统
  15. 高薪程序员面试题精讲系列23之说一下如何实现文件上传、下载以及断点续传?
  16. java 获取meta-inf路径_【Java】WEB-INF目录与META-INF目录的作用
  17. 【思维导图】Excel转成思维导图
  18. AMD显卡安装Caffe|深度学习|Ubuntu
  19. Oracle Coherence 3.5 读书笔记之3 - 满足性能,可扩展和可用性目标
  20. 《一个投资家的20年》读书笔记

热门文章

  1. 图像处理与机器视觉网络资源收罗——倾心大放送
  2. 多个Excel文件合并成一个文件
  3. 数据结构-----平衡二叉树
  4. Android自定义View【实战教程】6⃣️---深入理解 Android 中的 Matrix
  5. 当你成为大龄码农时,你会怎么找你的出路?
  6. 最牛逼android上的图表库MpChart(三) 条形图
  7. Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
  8. python基础篇——集合
  9. 汇编64位无法生成可用exe_MASM学习x86汇编语言2 寄存器、伪指令与程序调试
  10. nginx 代理springmvc到二级目录_详解Nginx从入门到实践