Legal or Not
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
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
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
Output
If it is legal, output "YES", otherwise "NO".
Sample Input
Sample Output
题目大意:
在一个群里面,大家互相请教问题,比如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相关推荐
- 【Paper】Few-Shot Charge Prediction with Discriminative Legal Attributes
传送门: paper--COLING2018 paper. codes 文章目录 Abstract 1 Introduction 2 Related Work 2.1 Zero-Shot Classi ...
- 【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 ...
- 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 ...
- 如何判断derived-to-base conversion是否legal
我们知道,在类内的access specifier public .protected.private都只是为了限定用户程序对类内的访问权限,而在继承list中的access specifier则是影 ...
- HDU 3342 Legal or Not(拓扑排序)
描述 ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that ju ...
- 【HDU - 3342】Legal or Not(拓扑排序)
题干: ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that j ...
- Re32:读论文 Summarizing Legal Regulatory Documents using Transformers
诸神缄默不语-个人CSDN博文目录 论文名称:Summarizing Legal Regulatory Documents using Transformers 论文下载地址:https://dl.a ...
- Medical robotics-Regulatory, ethical, and legal considerations for increasing levels of autonomy
借鉴自动驾驶技术自主化等级的划分,大佬们对医疗机器人领域中的自主化(自治化)等级进行了划分和表述.非常有参考意义,可以尝试把自己的机器人归入其中哪一类,提升等级~ 医疗机器人-对提升的自主化等级的规范 ...
- 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 ...
- 论文阅读:How Does NLP Benefit Legal System:A Summary of LAI如何使用NLP技术帮助法律智能:关于法律智能的综述
How Does NLP Benefit Legal System:A Summary of Legal ArtificialIntelligence Intelligence 如何使用NLP技术帮助 ...
最新文章
- 8天学通MongoDB——第五天 主从复制
- WPF绘制光滑连续贝塞尔曲线
- 雷军玩谐音梗:称米粉为“小粽子” 因为粽子“心中有MI”
- php 计算日期差几周,PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)
- iOS核心动画 - CALayer
- Flash as3.0 保存MovieClip运动轨迹到json文件
- .NET实现中英文验证码
- python字典常见操作
- WIN10电脑自动关机命令
- 理解 HTTP 幂等性
- 数据库索引:位图索引
- 计算机基础知识(上)(硬件篇)
- 0day安全:软件漏洞分析技术 学习分析记录
- 中国最小黑客:为不做作业 黑掉学校系统
- 高薪程序员面试题精讲系列23之说一下如何实现文件上传、下载以及断点续传?
- java 获取meta-inf路径_【Java】WEB-INF目录与META-INF目录的作用
- 【思维导图】Excel转成思维导图
- AMD显卡安装Caffe|深度学习|Ubuntu
- Oracle Coherence 3.5 读书笔记之3 - 满足性能,可扩展和可用性目标
- 《一个投资家的20年》读书笔记
热门文章
- 图像处理与机器视觉网络资源收罗——倾心大放送
- 多个Excel文件合并成一个文件
- 数据结构-----平衡二叉树
- Android自定义View【实战教程】6⃣️---深入理解 Android 中的 Matrix
- 当你成为大龄码农时,你会怎么找你的出路?
- 最牛逼android上的图表库MpChart(三) 条形图
- Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
- python基础篇——集合
- 汇编64位无法生成可用exe_MASM学习x86汇编语言2 寄存器、伪指令与程序调试
- nginx 代理springmvc到二级目录_详解Nginx从入门到实践