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. Linux nc (netcat) 详解

    Linux nc (netcat) 详解 http://blog.csdn.net/michael493439861/article/details/7445454

  2. SQLAlchemy中filter()和filter_by()有什么区别

    from:https://segmentfault.com/q/1010000000140472 filter: apply the given filtering criterion to a co ...

  3. CSS3制作旋转的小风车

    制作旋转小风车 一 我先搭建一个大盒子400x400px大盒子里面嵌套四个小盒子200x200px,放在一起肯定是四个排在一行,我想要的效果是上下各两个, css样式 *{ margin:0; pad ...

  4. zabbix mysql自动发现规则

    1.配置mysql,添加监控用的账号,授予查看所有用户线程/连接的权限 GRANT PROCESS ON *.* TO 'zabbix'@'127.0.0.1' identified BY '20c1 ...

  5. 如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?

    如果不考虑意外, 第一版最简单的构思如下: #!/usr/bin/env python # -*- coding: utf-8 -*- import getopt, sys import subpro ...

  6. DB2 v9.7官方下载链接

    http://blog.sina.com.cn/s/blog_8ea8e9d50102w2s6.html

  7. vs2008下Error LINK2005: already defined in ...的一种解决方式

    原因:不同的库之间都定义了相同的名称. 方法:右键工程->Properties->Configuration->Linker->Input 在右侧的Additional Dep ...

  8. scrapy中Selector的使用

    scrapy的Selector选择器其实也可以用来解析,今天主要总结下css和xpath的用法,其实我个人最喜欢用css 以慕课网嵩天老师教程中的一个网页为例,python123.io/ws/demo ...

  9. 洛谷——P1495 曹冲养猪

    题目描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把.举个例子,假如有 ...

  10. python sql语句封装连接mysql

    进行了代码优化,欢迎评审 #!/usr/bin/python # -*- coding:utf-8 -*- import logging logging.basicConfig(level=loggi ...