1 条题解
-
1
#include<bits/stdc++.h> using namespace std; const int N=1e4+5,M=5e4+5; struct edge{ int x,y,pre; }a[M]; int last[N],alen; void ins(int x,int y){ a[++alen]=edge{x,y,last[x]}; last[x]=alen; } int dfn[N],low[N],v[N],id; int sta[N],top; int cnt,c[N],siz[N],cd[N]; void tarjan(int x){ dfn[x]=low[x]=++id,v[x]=1; sta[++top]=x; for(int k=last[x];k;k=a[k].pre){ int y=a[k].y; if(!dfn[y]){ tarjan(y); low[x]=min(low[x],low[y]); } else if(v[y]){ low[x]=min(low[x],dfn[y]); } } if(dfn[x]==low[x]){ cnt++; int y; do{ y=sta[top--]; v[y]=0; c[y]=cnt; siz[cnt]++; }while(y!=x); } } int main(){ int n,m;scanf("%d%d",&n,&m); alen=1;memset(last,0,sizeof(last)); for(int i=1;i<=m;i++){ int x,y;scanf("%d%d",&x,&y); ins(x,y); } for(int i=1;i<=n;i++){ if(!dfn[i])tarjan(i); } for(int x=1;x<=n;x++){ for(int k=last[x];k;k=a[k].pre){ int y=a[k].y; if(c[x]==c[y])continue; cd[c[x]]++; } } int tot=0,pos; for(int i=1;i<=cnt;i++){ if(!cd[i]){ tot++; pos=i; } } if(tot==1)printf("%d",siz[pos]); else printf("0"); return 0; }
- 1
信息
- ID
- 1051
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- (无)
- 递交数
- 53
- 已通过
- 32
- 上传者