标签:nbsp ali amp names ref sample cep next cpp
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 40304 | Accepted: 16412 |
Description
Input
Output
Sample Input
3 3 1 2 2 1 2 3
Sample Output
1
Hint
Source
#include <stack> #include <cstdio> #include <cstring> #define maxn 100005 using namespace std; stack<int> s; int dfn[maxn],low[maxn],head[maxn],f[maxn]; bool vis[maxn]; int ind[maxn]; int tot,cnt,sum; struct edges { int u,v,next; }edge[maxn]; int n,m; void addedge(int u,int v) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void Tarjan(int num) { dfn[num]=low[num]=++tot; vis[num]=true; s.push(num); for(int i=head[num];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!dfn[v]) { Tarjan(v); low[num]=min(low[num],low[v]); } else if(vis[num]) { low[num]=min(low[num],dfn[v]); } } if(dfn[num]==low[num]) { sum++; while(true) { int now=s.top(); s.pop(); vis[now]=false; ind[now]=sum; if(now==num) break; } } } int main() { freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m)) { memset(head, -1, sizeof(head)); memset(dfn,0, sizeof(dfn)); memset(low,0, sizeof(low)); memset(f,0, sizeof(f)); memset(vis,false, sizeof(vis)); memset(ind,0, sizeof(ind)); tot=sum=cnt=0; while(!s.empty()) s.pop(); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); addedge(u, v); } for (int i = 1; i <= n; i++) { if (!dfn[i]) Tarjan(i); } for (int i = 1; i <= n; i++) { for (int j = head[i]; j != -1; j = edge[j].next) { int v = edge[j].v; if (ind[i] != ind[v]) { f[ind[i]]++; } } } int tol = 0; int index; for (int i = 1; i <= sum; i++) { if (!f[i]) { tol++; index = i; } } int ans = 0; if (tol == 1) { for (int i = 1; i <= n; i++) { if (ind[i] == index) ans++; } printf("%d\n", ans); } else printf("0\n"); } return 0; }
标签:nbsp ali amp names ref sample cep next cpp
原文地址:https://www.cnblogs.com/zyf3855923/p/9602051.html