QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#640837#906. 强连通分量Zhou_JKWA 4ms33404kbC++231.9kb2024-10-14 16:25:112024-10-14 16:25:12

Judging History

你现在查看的是最新测评结果

  • [2024-10-14 16:25:12]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:33404kb
  • [2024-10-14 16:25:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
struct Trajan_SCC
{
    static constexpr int N=500005;
    int n;
    Trajan_SCC(int _n):n(_n){}
    vector<int>G[N];
    int dfn[N],low[N],Index;
    bool ins[N];
    stack<int>s;
    int bel[N],tot;
    vector<int>block[N];
    void dfs(int u)
    {
        dfn[u]=low[u]=++Index;
        ins[u]=true;
        s.push(u);
        for(int v:G[u])
        {
            if(!dfn[v])
            {
                dfs(v);
                low[u]=min(low[u],low[v]);
            }
            else if(ins[v]) low[u]=min(low[u],dfn[v]);
        }
        if(dfn[u]==low[u])
        {
            tot++;
            block[tot].clear();
            while(!s.empty()&&s.top()!=u)
            {
                int v=s.top();
                s.pop();
                ins[v]=false;
                bel[v]=tot;
                block[tot].push_back(v);
            }
            s.pop();
            ins[u]=false;
            bel[u]=tot;
            block[tot].push_back(u);
        }
        return;
    }
    void add_edge(int u,int v)
    {
        G[u].emplace_back(v);
        return;
    }
    void tarjan()
    {
        fill(dfn+1,dfn+n+1,0);
        fill(low+1,low+n+1,0);
        fill(ins+1,ins+n+1,false);
        tot=0;
        for(int i=1;i<=n;i++)
            if(!dfn[i]) dfs(i);
        return;
    }
};
int main()
{
    int n,m;
    cin>>n>>m;
    Trajan_SCC scc(n);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin>>u>>v;
        u++,v++;
        scc.add_edge(u,v);
    }
    scc.tarjan();
    int tot=scc.tot;
    cout<<tot<<"\n";
    reverse(scc.block+1,scc.block+tot+1);
    for(int i=tot;i>=1;i--)
    {
        int l=scc.block[i].size();
        cout<<l;
        for(int u:scc.block[i])
            cout<<" "<<u-1;
        cout<<"\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 33404kb

input:

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

output:

4
2 3 0
1 2
2 4 1
1 5

result:

wrong answer 5 is later than 2, but there is an edge (5, 2)