标签:.net names str namespace targe max ems const code
#include<cstdio> #include<queue> #include<cstring> #include<vector> using namespace std; const int maxm = 105; vector<int> ve[maxm]; int indeg[maxm], n, m, num, a[maxm], x, y; queue<int> q; void topsort() { while(!q.empty())q.pop(); num = 0; for(int i = 1; i <= n; i++) { if(!indeg[i]) q.push(i); } while(!q.empty()) { int nx = q.front(); a[num++] = nx; q.pop(); for(int i = 0; i < ve[nx].size(); i++) { if(--indeg[ ve[nx][i] ] == 0) q.push(ve[nx][i]); } } } int main() { while(~scanf("%d%d", &n, &m)) { if(n == 0 && m == 0) break; memset(indeg, 0, sizeof(indeg)); memset(a, 0, sizeof(a)); for(int i = 1; i <= n; i++) { ve[i].clear(); } while(m--) { scanf("%d%d", &x, &y); ve[x].push_back(y); indeg[y]++; } topsort(); for(int i = 0; i < n; i++) { if(i == 0) printf("%d", a[i]); else printf(" %d", a[i]); } printf("\n"); } }
找入度为零的点,然后一个一个存,一般用于很多任务,给出几个优先级, 然后得到总的顺序。
https://vjudge.net/contest/280900#problem/A习题
标签:.net names str namespace targe max ems const code
原文地址:https://www.cnblogs.com/downrainsun/p/10327531.html