【东方模拟赛】【模板】上白泽慧音 tarjan找scc

10 篇文章 0 订阅
4 篇文章 0 订阅

这里写图片描述
这里写图片描述


又是裸题,丢个模板跑

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;

const int SIZE=100010;

int head[SIZE],nxt[SIZE],tot=0;

int to[SIZE];

void build(int f,int t)
{
    to[++tot]=t;
    nxt[tot]=head[f];
    head[f]=tot;
}

int low[SIZE],dfn[SIZE],dfs_clock=0;
int sccnum[SIZE],cnt=0;
stack<int> s;
vector<int> g[SIZE];
void dfs(int u)
{
    dfn[u]=low[u]=++dfs_clock;
    s.push(u);
    for(int i=head[u];i;i=nxt[i])
    {
        int v=to[i];
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!sccnum[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        cnt++;
        while(233)
        {
            int x=s.top(); s.pop();
            sccnum[x]=cnt;
            g[cnt].push_back(x);
            if(x==u) break;
        }
    }   
}

bool cmp(vector<int> a,vector<int> b)
{
    if(a.size()!=b.size()) return a.size()>b.size();
    for(int i=0;i<a.size();i++)
    {
        if(a[i]!=b[i]) return a[i]>b[i];
    }
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        switch(c)
        {
            case 1:
                build(a,b);
                break;
            case 2:
                build(a,b); build(b,a);
                break;
        }
    }

    for(int i=1;i<=n;i++)
        if(!dfn[i])
            dfs(i);

    for(int i=1;i<=cnt;i++) sort(g[i].begin(),g[i].end());

    sort(g+1,g+1+cnt,cmp);
    printf("%d\n",g[1].size());
    for(int i=0;i<g[1].size();i++)
        printf("%d ",g[1][i]);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值