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. Basic-Paxos协议日志同步应用

    使用Basic-Paxos协议的日志同步与恢复 传统数据库保持服务持续可用通常采用1主N备, 既采取两种日志同步模式: Maximum Availability和Maximum Protection. ...

  2. STL各个数据结构特点

    STL容器特征总结 2011-11-09 11:10:50|  分类: STL|举报|字号 订阅     STL中顺序容器类和关联式容器类的主要特征如下:(1)Vector 1.内部数据结构:连续存储 ...

  3. 【反演复习计划】【bzoj3529】数表

    Orz PoPoQQQ大爷 按照他ppt的解法,这题可以划归到之前的题了OrzOrz 跪wy写的题解(Stealth Assassin)https://www.luogu.org/wiki/show? ...

  4. PHP常用函数及其注释

    <?php //===============================时间日期=============================== //y返回年最后两位,Y年四位数,m月份数字 ...

  5. 常用的WebService一览表

    天气预报Web服务,数据来源于中国气象局Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmxDisco       ...

  6. G - Rescue 【地图型BFS+优先队列(有障碍物)】

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  7. 20、Django实战第20天:课程详情页

    1.把course-detail.html复制到templates目录下 2.编辑course-detail.html,分析页面,继承base.html 3.编辑courses.views .... ...

  8. virtualenvwrapper的安装及问题解决

    安装virtualenvwrapperyum install python-setuptools python-develpip install virtualenvwrapper # linux下 ...

  9. openresty的时间获取

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

  10. POP3、IMAP、SMTP邮件协议的理解

    一个热爱技术的菜鸟...用点滴的积累铸就明日的达人 CSDN博客链接: http://blog.csdn.net/my_confesser    正文   今天入职配置OutLook的时候,看到公司的 ...