您的位置:首页 > 运维架构

【bzoj】1051 &&【poj】2186 Popular Cows Tarjan scc

2016-05-21 19:12 441 查看
据说这是个Tarjan模板题……Orz you all

这个题主要是找唯一的出度为零的点,请注意是唯一!

这个题我的课件里有详细讲解QwQ

神奇的poj传送门

神奇的bzoj传送门

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn=100000+10;
int head[maxn],next[maxn],to[maxn];
int ff[maxn],tt[maxn],cd[maxn];
int scc_tong[maxn],scc[maxn],low[maxn],dfn[maxn];
int tot=0,cnt=0,dfs_clock;
void build(int f,int t)
{
to[++tot]=t;
next[tot]=head[f];
head[f]=tot;
}
stack<int>s;
void find_scc(int u)
{
low[u]=dfn[u]=++dfs_clock;
s.push(u);
for(int i=head[u];i;i=next[i])
{
int v=to[i];
if(!dfn[v])
{
find_scc(v);
low[u]=min(low[u],low[v]);
}
else if(!scc[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
cnt++;
while(343)
{
int x=s.top();
s.pop();
scc[x]=cnt;
scc_tong[cnt]++;
if(x==u)
break;
}
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>ff[i]>>tt[i];
build(ff[i],tt[i]);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i]) find_scc(i);
}
for(int i=1;i<=m;i++)
{
if(scc[ff[i]]!=scc[tt[i]])
{
cd[scc[ff[i]]]++;
}
}
int ans=0,t=0;
for(int i=1;i<=cnt;i++)
{
if(cd[i]==0)
{
ans+=scc_tong[i];
t++;
}
}
if(t==1) cout<<ans;
else cout<<"0";
return 0;
}


大蒟蒻写题解 求轻喷QwQ
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: