Test instructions not to say, said the idea.
The relationship is that a requires more pay than B, because as much as possible to let the boss pay less money, then A's salary is B's salary +1. To determine the relationship as a>b, the principle of building the edge based on topological ordering is to treat the "less than" relationship as a forward edge. Then we can build v->u.
#include <stdio.h> #include <string.h> #include <string> #include <iostream> #include < algorithm> #include <vector> #include <math.h> #include <map> #include <queue> #include < stack> #include <set> #define M 10000+5#define LL long long#define Ld __int64#define eps 0.00001#define INF 9999999 99#define MOD 112233#define MAX 26#define PI acos ( -1.0) using namespace std;vector<int> g[m];int num[m];int n;int int O[m],ans;int toposort () {queue<int> q;int cnt=0;ans=0;for (int i=1;i<=n;i++) {if (into[i]==0)// 0 points into the queue q.push (i);} while (!q.empty ()) {Cnt++;int U=q.front (), Ans+=num[u];q.pop (); for (int i=0;i<g[u].size (); i++) {int v=g[u][i];if (-- into[v]==0)//If the Edge is deleted (U,V), the degree of V is also 0, then pressed into the queue {Q.push (v); num[v]=num[u]+1;//v requires a higher salary than U}}}if (cnt!=n)//inference has no ring. -1 indicates a ring. 1 means no ring ans=-1;return ans;} int main () {int m;while (~scanf ("%d%d", &n,&m)) {for (int i=0;i<=n;i++) {g[i].clear (); into[i]=0;num[i]=888;} while (m--) {int u,v;scanf ("%d%d", &u,&v); G[v].pUsh_back (u); into[u]++;} printf ("%d\n", Toposort ());} return 0;} /*5 42 12 55 33 4*/
HDU2647 (topology sort + reverse build)