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

来源:互联网 发布:海量数据与数据港谁好 编辑:程序博客网 时间:2024/05/23 00:04

据说这是个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

0 0
原创粉丝点击