标签:pac ace string sample cstring 表示 测试 names 拓扑排序
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5703 Accepted Submission(s): 1671
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> using namespace std; const int N = 30000 + 5; int in[N]; vector<int> edge[N], topo; struct cmp{ bool operator () (const int & x, const int &y){ return x < y; } }; void Solve_question(int n){ priority_queue<int, vector<int>, cmp> Q; topo.clear(); for(int i = 1; i <= n; i++) if(!in[i]) Q.push( i ); while(!Q.empty()){ int u = Q.top(); Q.pop(); topo.push_back(u); for(int i = edge[u].size() - 1; i >= 0; i--){ int v = edge[u][i]; if(-- in[v] == 0) Q.push(v); } } int len = topo.size(); for(int i = len - 1; i >= 0; i--) printf("%d%c", topo[i], i == 0? ‘\n‘:‘ ‘); } void Input_data(int n, int m){ for(int i = 1; i <= n; i++) edge[i].clear(), in[i] = 0; int u, v; for(int i = 1; i <= m; i++){ scanf("%d %d", &u, &v); in[u] ++; edge[v].push_back(u); } } int main(){ int T; scanf("%d", &T); while(T--){ int n, m; scanf("%d %d", &n, &m); Input_data(n, m); Solve_question( n ); } return 0; }
标签:pac ace string sample cstring 表示 测试 names 拓扑排序
原文地址:http://www.cnblogs.com/Pretty9/p/7413120.html