博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Legal or Not (拓扑排序判环)
阅读量:5282 次
发布时间:2019-06-14

本文共 2208 字,大约阅读时间需要 7 分钟。

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 20 11 22 20 11 00 0

Sample Output

YESNO

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
const int maxn=1e5+5;typedef long long ll;using namespace std;int n,m;vector
ans[105];int in[105];void init(){ for(int t=0;t
q; for(int t=0;t
vec; while(!q.empty()) { int k=q.top(); q.pop(); vec.push_back(k); for(int t=0;t
>n>>m) { if(n==0) { break; } int a,b; init(); for(int t=0;t

 

转载于:https://www.cnblogs.com/Staceyacm/p/10781774.html

你可能感兴趣的文章
百度地图Api进阶教程-地图鼠标左右键操作实例和鼠标样式6.html
查看>>
游标使用
查看>>
LLBL Gen Pro 设计器使用指南
查看>>
SetCapture() & ReleaseCapture() 捕获窗口外的【松开左键事件】: WM_LBUTTONUP
查看>>
Android 设置界面的圆角选项
查看>>
百度地图api服务端根据经纬度得到地址
查看>>
根据xml生成相应的对象类
查看>>
Android StageFrightMediaScanner源码解析
查看>>
springBoot 项目 jar/war打包 并运行
查看>>
HDU 1501 Zipper
查看>>
打包java程序生成exe
查看>>
八叉树
查看>>
poj 1129 搜索
查看>>
Git 远程仓库
查看>>
HttpClient的巨坑
查看>>
关于静态文本框透明度的问题
查看>>
海量数据、高并发的优化方案
查看>>
javascript的发展及个人笔记
查看>>
全选,反全选,反选,获取选中的值,根据子选择控制全选按钮
查看>>
梦断代码读后感01
查看>>