拓扑排序 杭电5154 Harry and Magical Computer
来源:互联网 发布:厦门淘宝招聘 编辑:程序博客网 时间:2024/04/29 03:00
Harry and Magical Computer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 265 Accepted Submission(s): 123
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
#include <iostream>#include <algorithm>#include <string>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include<queue>#include<stack>#include<map>#include<set>using namespace std;#define lson rt<<1,l,MID#define rson rt<<1|1,MID+1,r//#define lson root<<1//#define rson root<<1|1#define MID ((l+r)>>1)typedef long long ll;typedef pair<int,int> P;const int maxn=10005;const int base=1000;const int inf=999999;const double eps=1e-5;int d[maxn];vector<int> G[maxn];bool tupo_sort(int n){ stack<int> s; for(int i=1;i<=n;i++) if(d[i]==0)s.push(i); int cnt=0; while(!s.empty()) { int m=s.top();//printf("%d ",m); s.pop(); cnt++; for(int i=0;i<G[m].size();i++) { d[G[m][i]]--; if(d[G[m][i]]==0)s.push(G[m][i]); } } if(cnt<n)return false; return true;}int main(){ int n,m,i,j,k,t; while(~scanf("%d%d",&n,&m)) { memset(d,0,sizeof(d)); for(i=0;i<=n;i++)//每次使用注意 清空 这里忘记了 错误了很多次 G[i].clear(); while(m--) { int s,e; scanf("%d%d",&s,&e); G[e].push_back(s); d[s]++; } if(tupo_sort(n)) puts("YES"); else puts("NO"); } return 0;}
0 0
- 杭电5154 Harry and Magical Computer(拓扑排序)
- 拓扑排序 杭电5154 Harry and Magical Computer
- HDU5154 Harry and Magical Computer【拓扑排序】
- hdu5154--Harry and Magical Computer(拓扑排序)
- hdu5154 Harry and Magical Computer(拓扑排序)
- hdu5154 Harry and Magical Computer--拓扑排序
- hdu 5154 Harry and Magical Computer(拓扑排序)
- HDU 5154 Harry and Magical Computer 拓扑排序
- HDU 5154 Harry and Magical Computer 拓扑排序
- HDOJ 题目5154 Harry and Magical Computer(拓扑排序)
- HDOJ 5154 Harry and Magical Computer (拓扑排序)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- HDOJ 5154 Harry and Magical Computer (拓扑排序)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- HDU 5154 Harry and Magical Computer (拓扑排序)
- HDU 5154 Harry and Magical Computer (拓扑排序)
- HDOJ 5154 Harry and Magical Computer (拓扑排序)
- Hdoj 5154 Harry and Magical Computer 【拓扑】
- 循环水处理剂:对游泳池水处理药剂必要性
- koa学习笔记----callback,promise,generator
- AdapterView setEmptyView
- Runloop相关文章
- KVM克隆的CentOS6.5的虚拟机的基本配置
- 拓扑排序 杭电5154 Harry and Magical Computer
- LeanMessage for Windows Phone 8 已上架 Nuget
- libubox [1] - utils socket list
- spring引入资源文件时,使用#和$符的区别
- libubox [2] - avl vlist kvlist
- Android在代码中设置控件的drawableLeft/Right/Top/Bottom
- libubox [4] - uloop runqueue ustream
- 2015跨年之殇
- linker command failed