QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#479639#906. 强连通分量Gold_Dino#WA 4ms18224kbC++141.2kb2024-07-15 19:45:322024-07-15 19:45:34

Judging History

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

  • [2024-07-15 19:45:34]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:18224kb
  • [2024-07-15 19:45:32]
  • 提交

answer

#include <bits/stdc++.h>
const int N = 500007;
int n, m;
struct
{
    int v, nxt;
} e[N];
std::vector<int> st[N];
int dfn[N], low[N], pdfn, stk[N], top, head[N], vis[N], pscc, scc[N], tot;
void Add(int u, int v) { e[++tot] = {v, head[u]}, head[u] = tot; }
void DFS(int u)
{
    // fprintf(stderr, "%d\n", u);
    low[u] = dfn[u] = ++pdfn;
    stk[++top] = u, vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt)
    {
        auto &x = e[i];
        if (!dfn[x.v])
            DFS(x.v), low[u] = std::min(low[u], low[x.v]);
        else if (vis[x.v])
            low[u] = std::min(low[u], dfn[x.v]);
    }
    if (low[u] == dfn[u])
    {
        ++pscc;
        while (vis[u])
        {
            int x = stk[top--];
            vis[x] = 0, scc[x] = pscc, st[pscc].push_back(x);
        }
    }
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++i)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        Add(u, v);
    }
    for (int i = 0; i < n; ++i)
        if (!dfn[i])
            DFS(i);
    printf("%d\n", pscc);
    // fprintf(stderr, "%d\n", dfn[2]);
    for (int i = 1; i <= pscc; ++i)
    {
        printf("%d", (int)st[i].size());
        for (int x : st[i])
            printf(" %d", x);
        printf("\n");
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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)