标签:source ase contain ++ eve one targe nes seve
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9918 Accepted Submission(s): 3165
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 20001 using namespace std; queue<int>que; int n,m,tot,num,ans,into[MAXN]; int to[MAXN],net[MAXN],head[MAXN],pre[MAXN]; void add(int u,int v){ to[++tot]=v;net[tot]=head[u];head[u]=tot; } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ num=0;tot=0;ans=0; memset(to,0,sizeof(to)); memset(net,0,sizeof(net)); memset(pre,-1,sizeof(pre)); memset(into,0,sizeof(into)); memset(head,0,sizeof(head)); for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); add(y,x); into[x]++; } while(!que.empty()) que.pop(); for(int i=1;i<=n;i++) if(!into[i]){ num++; pre[i]=1; que.push(i); } while(!que.empty()){ int now=que.front(); que.pop(); for(int i=head[now];i;i=net[i]){ into[to[i]]--; if(!into[to[i]]){ num++; pre[to[i]]=max(pre[now]+1,pre[to[i]]); que.push(to[i]); } } } if(num!=n) cout<<"-1"<<endl; else{ for(int i=1;i<=n;i++) ans+=888+(pre[i]-1); cout<<ans<<endl; } } }
标签:source ase contain ++ eve one targe nes seve
原文地址:http://www.cnblogs.com/cangT-Tlan/p/7502608.html