杭电 3342 Legal or Not(拓扑排序)
来源:互联网 发布:美术史论研究生知乎 编辑:程序博客网 时间:2024/06/15 04:16
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.
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.
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".
If it is legal, output "YES", otherwise "NO".
Sample Input
3 20 11 22 20 11 00 0
Sample Output
YESNO#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;int map[510][510];//将输入的关系保存在有向图中 int indegree[510];//保存前驱数量 int queue[510];int main(){ int m,n,i,j; int a,b; while(scanf("%d%d",&n,&m),m|n) { memset(map,0,sizeof(map)); memset(indegree,0,sizeof(indegree)); for(i=0;i<m;i++) { scanf("%d%d",&a,&b); if(map[a][b]==0) { map[a][b]=1; indegree[b]++;//b的前驱 +1 } } int k,t=0; for(j=1;j<=n;j++) { k=-1; for(i=0;i<=n;i++) { if(indegree[i]==0)//找到前驱为0的点 { k=i; break; } } if(k==-1){ break; } else { indegree[k]--;//前驱-1 t++;//记录节点数 如果成环就跳出了 for(i=0;i<n;i++) { if(map[k][i]) indegree[i]--; } } } if(t!=n)//节点数不等于总人数 说明成环 printf("NO\n"); else printf("YES\n"); } return 0;}
0 0
- 杭电3342Legal or Not (拓扑排序)
- 杭电 3342 Legal or Not(拓扑排序)
- Legal or Not HDU杭电3342【拓扑排序】
- 杭电 3342 Legal or Not(拓扑排序)
- HDOJ 3342 Legal or Not (拓扑排序)
- 【拓扑排序】hdu 3342 Legal or Not
- 【HDU】3342 Legal or Not 拓扑排序
- HDOJ 3342 Legal or Not 【拓扑排序】
- 【拓扑排序】 HDOJ 3342 Legal or Not
- hdoj 3342 Legal or Not(拓扑排序)
- hdu 3342 Legal or Not 拓扑排序
- HDU 3342 Legal or Not (拓扑排序)
- 拓扑排序 HDU 3342 Legal or Not
- HDOJ--3342--Legal or Not(拓扑排序)
- hdoj 3342 Legal or Not 【拓扑排序】
- HDOJ 3342 Legal or Not (拓扑排序)
- HDOJ 3342 Legal or Not (拓扑排序)
- hdoj 3342 Legal or Not【拓扑排序】
- 奇偶线段树(区间更新)
- Boostrap入门+样式学习--贰--(慕课网大漠《玩转bootstrap》[基础])
- LVS基于DR模型实现简单负载均衡集群和持久连接
- Git 多账号使用
- Linux gcc 使用以及库文件的编译
- 杭电 3342 Legal or Not(拓扑排序)
- Java集群优化——dubbo+zookeeper构建高可用分布式集群
- spring 使用c3po连接池
- hdu 1242 Rescue (BFS+优先队列)
- 手动编译安装nginx
- openwrt 随机 ssid 随机 mac 脚本
- poj 1118
- csu 1110 RMQ with Shifts(线段树) 解题报告
- 黑马程序员--面向对象