hdoj 3342 Legal or Not【拓扑排序】
2016-03-01 16:14
204 查看
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6514 Accepted Submission(s): 3043
Problem DescriptionACM-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<cstdio> #include<cstring> #include<cmath> using namespace std; const int M = 200; int map[M][M]; int ind[M]; void topo(int n) { int i, j, t; for(j = 0; j < n; j++) { t = n; for(i =0; i < n; i++) { if(!ind[i]) { ind[i]--; t = i; break; } } if(t == n) { puts("NO"); return ; } for(i = 0; i < n; i++) { if(map[t][i]) ind[i]--; } } puts("YES"); return ; } int main(){ int n,m; while(scanf("%d%d", &n,&m)==2, n|m) { memset(map, 0, sizeof(map)); memset(ind, 0, sizeof(ind)); for(int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); if(map[a][b]==0) { map[a][b] = 1; ind[b]++; } } topo(n); } return 0; }
相关文章推荐
- java 自定义异常类
- excel中日期转换成指定的文本格式
- KSSD
- python自动化运维 第2章 业务服务监控详解 2.1.1
- PullToRrefresh自定义下拉刷新动画
- WIP - 离散任务点击组件-错误:LOCATOR.CONTROL 的变元无效:ORG_LOCATOR_CONTROL=''
- setSingleChoiceItems和setPositiveButton两者触发时期
- Python Requests-学习笔记(11)-请求与响应对象
- SSO之CAS单点登录详细搭建教程
- Genymotion安装启动时报错问题
- 用户列表List保存到SP文件
- shell 初始化linux系统脚本
- tinyXML中FirstChild用法 http://blog.csdn.net/primer_programer/article/details/1968481
- 指针指向字符数组的赋值与输出
- js继承的实现 extend
- 37.消息机制之通知中心传值(NSNotificationCenter)
- spring自定义标签实现
- UML系列图--用例图
- 从零开始创建一个Ubuntu应用 -- 一个小小的RSS阅读器 (1)
- 并发修改异常的产生原因及解决方法