hdu3342——Legal or Not
来源:互联网 发布:数据库系统全书 豆瓣 编辑:程序博客网 时间:2024/05/21 11:22
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
Author
QiuQiu@NJFU
Source
简单题就不解释了。
简单题就不解释了。
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N=105;struct node{ int next; int to;}edge[N];int head[N];int tot,n;int in_degree[N];void addedge(int from,int to){ edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++;}void init(){ memset(head,-1,sizeof(head)); memset(in_degree,0,sizeof(head)); tot=0;}void topo_sort(){int cnt=0; queue<int>qu; while (!qu.empty()) qu.pop(); for (int i=0;i<n;i++) if (!in_degree[i]) qu.push(i); while (!qu.empty()) { int u=qu.front(); qu.pop(); cnt++; for (int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; in_degree[v]--; if(!in_degree[v]) qu.push(v); } }if(cnt!=n)printf("NO\n");elseprintf("YES\n");}int main(){//int t;//int icase=1;//scanf("%d",&t);//while(t--)int m;while(~scanf("%d%d",&n,&m)){if(!n && !m)break;//scanf("%d",&n);init();int x,y;for(int i=0;i<m;i++){scanf("%d%d",&x,&y);addedge(x,y);in_degree[y]++;}topo_sort();}return 0;}
0 0
- hdu3342——Legal or Not
- HDU3342---Legal or Not
- HDU3342-Legal or Not
- HDU3342-Legal or Not
- hdu3342 Legal or Not 拓扑排序
- Legal or Not(hdu3342拓扑排序)
- hdu3342 Legal or Not (拓扑排序+邻接矩阵)
- hdu3342 Legal or Not---拓扑排序
- HDU3342--Legal or Not(强连通)
- HDU3342 Legal or Not 【拓扑排序】
- HDU3342 Legal or Not(拓扑排序)
- hdu3342 Legal or Not(拓扑排序)
- hdu3342 Legal or Not【拓扑排序】
- HDU3342:Legal or Not(拓扑排序)
- HDU3342---Legal or Not(拓扑排序。)
- HDU3342 Legal or Not 解题报告--拓扑排序
- hdu3342 Legal or Not(拓扑排序,判环)
- HDU3342 Legal or Not【拓扑排序】【链式前向星】
- MOV AL,061h
- Android中的进程通信之Broadcast
- UVA 1395 - Slim Span
- vim简单使用教程
- UVA - 307 Sticks(回溯+剪枝)
- hdu3342——Legal or Not
- 你生日那天你说你说呢
- 数组作为堆栈实现表达式整型计算器
- KEIL产生WARNINGL15: MULTIPLE CALL TO SEGMENT的原因及解决方法
- PADS软件概述
- Android中的进程通信之Content Provider
- windows如何上传到文件linux服务器上-Sftp上传
- HDU 3572 Task Schedule(ISAP模板&&最大流问题)
- 程序员面试题目总结--链表(6)【单链表排序】