只A#6求调

B3609 [图论与代数结构 701] 强连通分量

daitangchen2008 @ 2024-07-05 15:32:50

#include <bits/stdc++.h>
using namespace std;
int n,m,cnt,ans;
vector<int> edge[10010];
priority_queue<int,vector<int>,greater<>>p[10010];
bool instack[10010];
int dfn[10010];
int low[10010];
int scc[10010];
stack<int> s;
void Tarjan(int u)
{
    ++cnt;
    dfn[u]=low[u]=cnt;
    s.push(u);
    instack[u]=true;
    for(int v:edge[u])
    {
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        ans++;
        int temp;
        do
        {
            temp=s.top();
            s.pop();
            instack[temp]=false;
            scc[temp]=ans;
            p[ans].push(temp);
        }while(temp!=u);
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;++i)
    {
        int u,v;
        cin>>u>>v;
        edge[u].push_back(v);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            Tarjan(i);
    cout<<ans<<endl;
    for(int i=1;i<=n;i++)
    {
        if(!p[scc[i]].empty())
        {
            while (!p[scc[i]].empty()) {
                cout << p[scc[i]].top() << " ";
                p[scc[i]].pop();
            }
            cout << endl;
        }
    }
    return 0;
}

by lpx2024 @ 2024-08-01 19:12:16

同问


by glw2567 @ 2024-08-19 01:14:48

可以试试这组数据

10 20
9 7
3 3
10 7
1 10
6 3
3 3
7 10
3 6
3 9
2 2
2 1
1 1
7 1
1 1
9 6
7 5
2 8
7 10
3 1
1 2

加油


|