拓扑排序 杭电5154 Harry and Magical Computer
2015-01-04 13:03
375 查看
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
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).
Sample Input
3 2 3 1 2 1 3 3 3 2 2 1 1 3
Sample Output
YES NO
#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; }
相关文章推荐
- 杭电5154 Harry and Magical Computer(拓扑排序)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- hdu 5154 Harry and Magical Computer(拓扑排序,dfs判环)
- HDU 5154 Harry and Magical Computer (拓扑排序)
- HDOJ 5154 Harry and Magical Computer (拓扑排序)
- HDOJ 题目5154 Harry and Magical Computer(拓扑排序)
- hdu 5154 Harry and Magical Computer(拓扑排序)
- hdu 5154 Harry and Magical Computer 拓扑排序
- 拓扑排序简单思路O(n^2)附HDU 5154 Harry and Magical Computer
- HDOJ 5154 Harry and Magical Computer (拓扑排序)
- HDU 5154 Harry and Magical Computer 拓扑排序
- HDU 5154 Harry and Magical Computer (拓扑排序)
- HDU5154 Harry and Magical Computer【拓扑排序】
- hdu 5154 Harry and Magical Computer
- HD5154-Harry and Magical Computer
- hdu_5154 Harry and Magical Computer
- hdu 5154 Harry and Magical Computer
- Harry and Magical Computer (HDU 5154)
- hdu Harry and Magical Computer 拓扑排序(简单)
- hdu 5154 Harry and Magical Computer