Link: http://acm.hdu.edu.cn/showproblem.php?pid=2647, not a complex sort of topology.
This problem is not complex, there is a point to consider is currently saved at the top of the queue at the vertex of the edge of the deletion, will produce a new degree of 0 vertex, when the new arrival of the vertex of 0 is higher than the top of the queue vertex to a level, that is, pay more than he 1, this treatment is nothing.
#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<string>#include<string.h>#include<algorithm>using namespacestd;#defineLL __int64#defineEPS 1e-8#defineINF 1e8#defineLson L, M, RT << 1#defineRson m + 1, R, RT << 1 | 1Const intMOD =2333333; Const intMAXN =10000+5;intRANK[MAXN], Degree[maxn];vector<int>GRAPH[MAXN];intN, M;BOOLToposort () {intI, J, u, V, tot; Tot=0; memset (Rank,0,sizeof(Rank)); memset (degree,0,sizeof(degree)); for(inti =1; I <= N; i++) for(intj =0; J < Graph[i].size (); J + +) degree[Graph[i][j]]++; Queue<int>que; for(inti =1; I <= N; i++) { if(Degree[i] = =0) {Que.push (i); Rank[i]=0; Tot++; } } while(!Que.empty ()) { intU =Que.front (); Que.pop (); for(inti =0; I < graph[u].size (); i++) { intTMP =Graph[u][i]; DEGREE[TMP]--; if(Degree[tmp] = =0) {Que.push (TMP); RANK[TMP]= Rank[u] +1;//it's here. 1 more Paytot++; } } } returntot = =N;}intMain () {intA, B; while(~SCANF ("%d%d", &n, &m)) { for(inti =1; I <= N; i++) graph[i].clear (); while(m--) {scanf ("%d%d", &a, &b); Graph[b].push_back (a); } if(Toposort ()) {intres =888*N; for(inti =1; I <= N; i++) Res+=Rank[i]; printf ("%d\n", RES); } Else{printf ("-1\n"); } } return 0;}
HDU2647 topology Ordering