Tarjan学习笔记

· · 个人记录

缩点

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
    int x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int MAXN = 200005;
int vis[MAXN],col[MAXN],dfn[MAXN],low[MAXN];
int w[MAXN],a[MAXN],in[MAXN],ans[MAXN],out[MAXN];
vector<int> G[MAXN],adj[MAXN],rev[MAXN];
stack<int> st; queue<int> q;
int n,m,t1,t2,dep,cnt;

void targan(int x)
{
    dfn[x]=low[x]=++dep;
    vis[x]=1; st.push(x);
    int sz=G[x].size();
    for (int i=0; i < sz; i++)
    {
        int y=G[x][i];
        if (!dfn[y])
            targan(y),low[x]=min(low[x],low[y]);
        else if (vis[y])
            low[x]=min(dfn[y],low[x]);
    }
    if (dfn[x]==low[x])
    {
        col[x]=++cnt; vis[x]=0; w[cnt]+=a[x];
        while (st.top()!=x)
        {
            w[cnt]+=a[st.top()];
            col[st.top()]=cnt;
            vis[st.top()]=0; st.pop();
        }
        st.pop();
    }
}

int main()
{
    n=read(),m=read();
    for (int i=1; i <= n; i++)
        a[i]=read();
    for (int i=1; i <= m; i++)
    {
        t1=read(),t2=read();
        G[t1].push_back(t2);
    }
    for (int i=1; i <= n; i++)
        if (!dfn[i]) targan(i);
    for (int i=1; i <= n; i++)
    {
        int sz=G[i].size();
        for (int j=0; j < sz; j++)
        {
            int from=i,to=G[i][j];
            if (col[from] != col[to])
            {
                adj[col[from]].push_back(col[to]),
                in[col[to]]++,out[col[from]]++,
                rev[col[to]].push_back(col[from]);
            }   
        }
    }
    for (int i=1; i <= cnt; i++)
        if (!in[i]) q.push(i),ans[i]=w[i];
    while (!q.empty())
    {
        int mx=-1,x=q.front(); q.pop();
        int sz=rev[x].size();
        for (int i=0; i < sz; i++)
        {
            int to=rev[x][i];
            mx=max(mx,ans[to]);
        }
        ans[x]=mx+w[x]; sz=adj[x].size();
        for (int i=0; i < sz; i++)
        {
            int to=adj[x][i]; in[to]--;
            if (!in[to]) q.push(to);
        }
    }
    int tmp=-1;
    for (int i=1; i <= cnt; i++)
        tmp=max(tmp,ans[i]);
    cout << ++tmp << endl;
    return 0;
}

割点

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
    int x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int MAXN=100005;
int dfn[MAXN],low[MAXN],cut[MAXN];
vector<int> G[MAXN];
int n,m,cnt,num;

inline void addedge(int u,int v)
{
    G[u].push_back(v);
    G[v].push_back(u);
}

inline void tarjan(int x,int fa)
{
    int cur=0,sz=G[x].size();
    dfn[x]=low[x]=++cnt;
    for(int i = 0; i < sz; ++i)
    {
        int v=G[x][i];
        if(!dfn[v])
        {
            tarjan(v,fa);
            low[x]=min(low[x],low[v]);
            if(dfn[x]<=low[v] && x!=fa) cut[x]=1;
            if(x==fa) cur++;
        }
        low[x]=min(low[x],dfn[v]);
    }
    if(cur>1 && x==fa) cut[x]=1;
}

int main(int argc, char const *argv[])
{
    n=read(); m=read();
    int u,v;
    for(int i = 1; i <= m; ++i)
    {
        u=read(); v=read();
        addedge(u,v);
    }
    for(int i = 1; i <= n; ++i)
        if(!dfn[i]) tarjan(i,i);
    for(int i = 1; i <= n; ++i)
        if(cut[i]) num++;
    printf("%d\n", num);
    for(int i = 1; i <= n; ++i)
        if(cut[i]) printf("%d ", i);
    return 0;
}