标签:integer mit ++ org ble 0ms algo explicit ret
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 34752 | Accepted: 14155 |
Description
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Source
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘; return x*f; } const int MAXN=500001; const int INF=999999; int N,M; int dfn[MAXN],low[MAXN]; int que[MAXN]; bool vis[MAXN]; vector<int> vec[MAXN]; int outdu[MAXN]; int tar[MAXN]; int tot,tmp,Col; int size[MAXN]; void Tarjan(int x){ ++tot; dfn[x]=low[x]=tot; vis[x]=true; que[++tmp]=x; for(int i=0;i<vec[x].size();i++){ int to=vec[x][i]; if(!dfn[to]){ Tarjan(to); low[x]=min(low[x],low[to]); } else if(vis[to]) low[x]=min(dfn[to],low[x]); } if(dfn[x]==low[x]){ ++Col; tar[x]=Col; vis[x]=false; while(que[tmp]!=x){ int k=que[tmp]; tar[k]=Col; vis[k]=false; tmp--; } tmp--; } } int ans,anst; int main(){ N=read(),M=read(); for(int i=1;i<=M;i++){ int u=read(),v=read(); vec[u].push_back(v); } for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i); for(int i=1;i<=N;i++){ int col=tar[i];size[col]++; for(int j=0;j<vec[i].size();j++){ if(tar[vec[i][j]]!=col) outdu[col]++; } } for(int i=1;i<=Col;i++){ if(!outdu[i]) ans+=size[i],anst++; } if(anst>1) printf("0\n"); else printf("%d\n",ans); }
标签:integer mit ++ org ble 0ms algo explicit ret
原文地址:http://www.cnblogs.com/wxjor/p/7281696.html