标签:
#include<bits/stdc++.h> using namespace std; vector<int> mp[20005]; int n,m; int a,b; int in[20005]; int L[20005]; int flag; int re; struct node { int sum; }; queue<node>q; struct node N,now; int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) mp[i].clear(); memset(in,0,sizeof(in)); memset(L,0,sizeof(L)); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); mp[b].push_back(a); in[a]++; } for(int i=1;i<=n;i++) { if(in[i]==0) { N.sum=i; q.push(N); } } int jishu=n; int temp; while(!q.empty()) { now=q.front(); q.pop(); jishu--; temp=L[now.sum]; //cout<<now.sum<<endl; for(unsigned int i=0;i<mp[now.sum].size();i++) { if(--in[mp[now.sum][i]]==0) { N.sum=mp[now.sum][i]; L[N.sum]=temp+1; q.push(N); } } } re=0; if(jishu>0) printf("-1\n"); else { for(int i=1;i<=n;i++) re+=L[i]; printf("%d\n",re+888*n); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/hsd-/p/4931642.html