Leagal 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.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
If it is legal, output "YES", otherwise "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 —— 拓扑排序(王道)的更多相关文章
- Day1:T1 模拟 T2 拓扑排序
T1:模拟 自己第一天的简直跟白痴一样啊...模拟都会打错.. 当时貌似在更新最大值的时候打逗比了... if((sum[x]==max && x<maxh) || sum[x] ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
随机推荐
- locust===官方说明文档,关于tasks
安装: >>> pip install locust locust在官方simple_code中如下: from locust import HttpLocust, TaskSet ...
- [ Python - 13 ] 批量管理主机必备模块
批量管理程序必备模块 optparse configparser paramiko optparse模块 简介: optparse模块主要用来为脚本传递命令参数功能 使用步骤: 1. i ...
- 【C++】隐式类型转换和 explicit关键字
来自:黄邦勇帅 1. 当类中带有一个参数的构造函数时,将执形对象的隐式转换,比如有类 A,有一个带有一个参数的构造函数A(int i){}则当出现语句A m=1;就会调用带有一个参数的构造函数来创建对 ...
- request.getRequestDispatcher(url).forward(request, response)
request.getRequestDispatcher().forward(request, response) 意思是将客户端的请求转向到 getRequestDispatcher()方法中参数定 ...
- selenium 警告框处理 (弹窗处理)
在web应用中常常会遇见很多用JavaScript编写的alert .confirm 以及prompt 弹窗,这是就需要driver.switchTo().alert()来选取(定位)警告弹窗.再对弹 ...
- springboot hystrix turbine 聚合监控
1.新建Module eureka-monitor-client 2.父级pom中添加module 3.编写eureka-monitor-client 中的pom <?xml version= ...
- ajaxfileupload异步上传文件
ajaxfileupload插件可以以异步方式上传文件(其实现:iframe),不像传统那样需要刷新,下面就介绍下其使用 1.HTML部分(先引入jquery): <!DOCTYPE html& ...
- Spring的自动装配
在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法: 自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...
- Feign负载均衡(五)
一.Feign定义 Feigin是服务消费者,Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Fei ...
- mysql实现full outer join
由于MySQL设计时不支持full outer join, 所以如果有全连接需求时,需要一点小技巧来实现. 假设有两个表t1,t2 full outer join 的效果和下面的效果一样: SELEC ...