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

拓扑排序,题意是给出多个人之见的关系,让你判断是否有环。

#include <iostream>
#include<queue>
#include<cstdio>
using namespace std;
vector<int> edge[];//邻接链表,只需保存与其邻接的节点编号
queue<int> Q;//保存入度为0的节点的队列
int main()
{
int inDegree[];//统计每个节点的入度
int n,m;
while(scanf("%d %d",&n,&m)!=EOF){
if(n== || m==)
break;
for(int i=;i<n;i++){//初始化
inDegree[i]=;
edge[i].clear();
}
while(m--){
int a,b;
scanf("%d %d",&a,&b);
inDegree[b]++;//b的入度+1
edge[a].push_back(b);//a的邻接表中添加b
}
while(!Q.empty())//清空队列
Q.pop();
for(int i=;i<n;i++){
if(inDegree[i]==)//入度为0的放入队列
Q.push(i);
}
int cnt=;
while(!Q.empty()){
int nowP=Q.front();
Q.pop();
cnt++;//被确定的节点个数加一
for(int i=;i<edge[nowP].size();i++){//将该节点以及以其为弧尾的所有边去除
inDegree[edge[nowP][i]]--;//该边的入度减一
if(inDegree[edge[nowP][i]]==)//该点的入度变为0
Q.push(edge[nowP][i]);//把这个点放入队列当中
}
}
puts(cnt==n?"YES":"NO");//所有节点都被确定拓扑序列,原图为有向无环图;否则非有向无环图
}
return ;
}

Leagal or Not —— 拓扑排序(王道)的更多相关文章

  1. Day1:T1 模拟 T2 拓扑排序

    T1:模拟 自己第一天的简直跟白痴一样啊...模拟都会打错.. 当时貌似在更新最大值的时候打逗比了... if((sum[x]==max && x<maxh) || sum[x] ...

  2. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  3. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  4. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  5. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  6. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  7. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  8. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  9. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

随机推荐

  1. facets学习(1):什么是facets

    ML 数据集可以包含数亿个数据点,每个数据点由数百(甚至数千)的特征组成,几乎不可能以直观的方式了解整个数据集.为帮助理解.分析和调试 ML 数据集,谷歌开源了 Facets,一款可视化工具. Fac ...

  2. 经常用到的Eclipse快捷键(更新中....)

    alt+shift+s:弹出Source选项,用于生成get,set等方法. Ctrl+E:快速显示当前Editer的下拉列表 alt+shift+r:重命名 Ctrl+Shift+→/Ctrl+Sh ...

  3. 使用 URLDecoder 和 URLEncoder 对统一认证中的http地址转义字符进行处理

    import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; ...

  4. [BZOJ2555]SubString LCT+后缀自动机

    2555: SubString Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3253  Solved: 975[Submit][Status][Di ...

  5. 文件的上传(TCP)

    问题描述:将本地文件上传(需将文件名一起上传)至指定服务器,服务器将上传的文件保存至指定路径下并文件名添加前缀 "Downlod_原文件名". 思路: 客户端需要一个输入流来读取本 ...

  6. 部署web应用到虚拟主机的三种方式

    方式一:            在 [tomcat]/conf/server.xml 文件中的<Engine>标签下的<Host>标签内部, 添加一个 <Context ...

  7. NOIP2018提高组模拟题(六)

    购物(shop) Description 小林来到商店中进行购物.商店里一共有 n 件物品,第 i 件物品的价格为 a[i] 元.小林总共需要购买 m 件物品,他希望他所花费的钱最少,请你计算出最小 ...

  8. 解决VM虚拟机中的ubuntu不能全屏的问题

    Ctrl+alt+T:打开终端 输入命令:sudo apt install open-vm* 运行之后重启一下虚拟机就可以了

  9. openresty的时间获取

    ngx.say('ngx.time()' .. ngx.time()) ngx.say('ngx.now()' .. ngx.now()) ngx.say('ngx.today()' .. ngx.t ...

  10. 解决Post提交乱码问题

    在web.xml里面配置 <filter> <filter-name>charac</filter-name> <filter-class>org.sp ...