HDU 5154_Harry and Magical Computer
来源:互联网 发布:淘宝售后退款多久到账 编辑:程序博客网 时间:2024/06/08 11:20
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1676 Accepted Submission(s): 656
Total Submission(s): 1676 Accepted Submission(s): 656
Problem Description
In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal with a process, it will work until the ending of the processes. One day the computer got n processes to deal with. We number the processes from 1 to n. However there are some dependencies between some processes. When there exists a dependencies (a, b), it means process b must be finished before process a. By knowing all the m dependencies, Harry wants to know if the computer can finish all the n processes.
Input
There are several test cases, you should process to the end of file.
For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies.1≤n≤100,1≤m≤10000
The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b).1≤a,b≤n
For each test case, there are two numbers n m on the first line, indicates the number processes and the number of dependencies.
The next following m lines, each line contains two numbers a b, indicates a dependencies (a, b).
Output
Output one line for each test case.
If the computer can finish all the process print "YES" (Without quotes).
Else print "NO" (Without quotes).
If the computer can finish all the process print "YES" (Without quotes).
Else print "NO" (Without quotes).
Sample Input
3 23 12 13 33 22 11 3
Sample Output
YESNO
这道题的意思是判断给出的数值里面有没有环,有的话输出NO,没有输出YES。
其实一开始是想到用二维数组来弄的,后来果断爆内存了,再后来想到了用一维数组来代替二维数组,一开始初始化数组的时候让数组的值等于它的下标,然后再输入数值,不断更新一维数组,如果遇到一开始更新时的那个值,就说明输入的数组里面有环,输出NO,否则输出YES。
#include<iostream>#include<string.h>#include<stdio.h>using namespace std;int num[10010];int flag=0;int find(int a,int b){if(num[b]==a) return 1;if(num[b]==b) return 0;find(a,num[b]);}int main(){int n,m;while(scanf("%d %d",&n,&m)!=EOF){flag=0;for(int i=1;i<=n;i++) num[i]=i;int ans,nut;for(int i=1;i<=m;i++){scanf("%d %d",&ans,&nut);if(find(ans,nut)) flag=1;else num[ans]=nut;}if(flag==1) printf("NO\n");else printf("YES\n");}return 0;}
0 0
- HDU 5154_Harry and Magical Computer
- hdu 5154 Harry and Magical Computer
- Harry and Magical Computer (HDU 5154)
- hdu 5154 Harry and Magical Computer
- HDU 5154 Harry and Magical Computer
- hdu 5154 Harry and Magical Computer
- HDU 5154 Harry and Magical Computer (BFS)
- HDU 5154 Harry and Magical Computer (Floyd)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- [BestCoder] Round #25 1001 - Harry and Magical Computer | HDU 5154
- HDU 5154 Harry and Magical Computer 拓扑排序
- hdu 5154 Harry and Magical Computer(BestCoder Round #25)
- HDU 5154 Harry and Magical Computer 拓扑排序
- HDU 5154 Harry and Magical Computer【逆拓扑】
- HDU 5154 Harry and Magical Computer(找环)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- HDU 5154 Harry and Magical Computer (拓扑排序)
- HDU 1096 A+B for Input-Output Practice (VIII)
- LVS + Keepalived之tun模式
- js上传图片预览
- dumpbin的使用
- java面向对象编程基础总结
- HDU 5154_Harry and Magical Computer
- 设计模式 工厂模式
- Cross-Origin Resource Sharing协议介绍及服务端跨域设置
- 你知道网线接入水晶头的两种正确方法吗? 做个备注,老忘记
- Android中不同包路径下Activity跳转的实现(解决ActivityNotFoundException)
- hdu 4647 Another Graph Game (贪心)
- css去掉iPhone、iPad默认按钮样式
- 修改tcp内核参数:somaxconn
- Seeding 2100