LuoGu P2863 [USACO06JAN]牛的舞会The Cow Prom
题目传送门
这个题还是个缩点的板子题......
答案就是size大于1的强连通分量的个数
加一个size来统计就好了
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int N=1e5+5;
const int M=5e5+5;
struct edge{
int to,next;
}e[M];
int n,m,dfn[N],low[N],cnt,head[N];
int idx[N],s[N],top,tot,sum,siz[N];
bool ins[N];int ind[N],ans;
inline void build(int u,int v){
e[++tot].next=head[u];
head[u]=tot;e[tot].to=v;
return ;
}
inline void tarjan(int cur){
s[++top]=cur;ins[cur]=true;
dfn[cur]=low[cur]=++cnt;
for(int i=head[cur];i;i=e[i].next){
int k=e[i].to;
if(!dfn[k]){
tarjan(k);
low[cur]=min(low[cur],low[k]);
}else if(ins[k]) low[cur]=min(low[cur],dfn[k]);
}
if(low[cur]==dfn[cur]){
register int ss=0;
++sum;
while(s[top+1]!=cur){
++ss;
idx[s[top]]=sum;
ins[s[top--]]=false;
}
siz[sum]=ss;
}
return ;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
register int u,v;
scanf("%d%d",&u,&v);
build(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;j=e[j].next){
int k=e[j].to;
if(idx[i]!=idx[k]) ++ind[idx[k]];
}
for(int i=1;i<=sum;++i) if(!ind[i]&&siz[i]>1) ++ans;
printf("%d\n",ans);
return 0;
}
May you return with a young heart after years of fighting.