Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
哪位大哥路过帮忙看看我的为什么错了啊。。。thx思路是: 先判断是否有环,如果有就是误解 然后如果i比j重,就连一条i到j的边,逆向拓扑排序,用优先队列取最小,一直wa, 下面是code。。。 #include <iostream> #include <queue> using namespace std; const int N = 210; bool ad[N][N], g[N][N]; int deg[N], ans[N]; int n, m; bool Cycle() { int i, j, k; for(k = 1; k <= n; k++) for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) if(g[i][k] && g[k][j]) g[i][j] = true; for(i = 1; i <= n; i++) for(j = i; j <= n; j++) if(g[i][j] && g[j][i]) return true; return false; } int main() { int i, j, k, ca; for(scanf("%d",&ca); ca; ca--) { scanf("%d%d",&n, &m); memset(ad, false, sizeof(ad)); memset(g, false, sizeof(g)); memset(deg, 0, sizeof(deg)); for(; m; m--) { scanf("%d%d",&i, &j); if(ad[j][i]) continue; ad[j][i] = true; g[j][i] = true; deg[j] ++; } if(Cycle()) {puts("-1"); continue;} priority_queue<int> pq; for(i = 1; i <= n; i++) if(deg[i] == 0) pq.push(-i); int cnt = 0; while(!pq.empty()) { i = pq.top(); pq.pop(); i = -i; ans[i] = ++cnt; for(j = 1; j <= n; j++) if(ad[j][i]) { if( (--deg[j]) == 0 ) pq.push(-j); } } for(i = 1; i <= n; i++) printf("%d%c",ans[i], i == n? '\n':' '); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator