题目链接:https://www.luogu.org/problemnew/show/P2002
这题和刻录光盘差不多,只是数据范围大了,必须用tarjan。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int rad()
{
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
const int maxn=1e5+5,maxm=5e5+5;
int n,m,tot,tim,cnt,ans,f[maxn],lnk[maxn],son[maxm],nxt[maxm],low[maxn],vis[maxn],st[maxn],tp,dfn[maxn],bel[maxn];
void tarjan(int x)
{
low[x]=dfn[x]=++tim;
vis[x]=1;st[++tp]=x;
for (int i=lnk[x],y;i;i=nxt[i]) if (!dfn[y=son[i]])
{
tarjan(y);
if (low[y]<low[x]) low[x]=low[y];
}else if (vis[y]&&low[x]>dfn[y]) low[x]=dfn[y];
if (low[x]==dfn[x])
{
++cnt;do{
bel[st[tp]]=cnt;
vis[st[tp--]]=0;
}while(tp>0&&x!=st[tp+1]);
}
}
int main()
{
n=rad();m=rad();
for (int i=1;i<=m;++i)
{
int x=rad(),y=rad();
son[++tot]=y;nxt[tot]=lnk[x];lnk[x]=tot;
}
for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i);
for (int i=1;i<=n;++i)
for (int j=lnk[i];j;j=nxt[j])
if (bel[i]!=bel[son[j]]) f[bel[son[j]]]=1;
for (int i=1;i<=cnt;++i) if (!f[i]) ++ans;
printf("%d\n",ans);
return 0;
}