QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357923#906. 强连通分量IsrothyWA 1ms4076kbC++232.5kb2024-03-19 15:01:562024-03-19 15:01:56

Judging History

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

  • [2024-03-19 15:01:56]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4076kb
  • [2024-03-19 15:01:56]
  • 提交

answer

#include <cstdio>
#include <queue>
#include <span>
#include <stack>
#include <vector>

struct TarjanScc {
    std::vector<std::vector<size_t>> adj, sccs;
    std::vector<size_t> dfn, low;
    std::stack<size_t> stk;
    std::vector<bool> on_stack;
    size_t dfs_clock;
    TarjanScc(std::span<std::pair<size_t, size_t>> edges, size_t n)
        : adj(n), dfn(n), low(n), on_stack(n), dfs_clock(0) {
        for (auto [u, v]: edges) {
            adj[u].emplace_back(v);
        }
        for (size_t u = 0; u < n; ++u) {
            if (!dfn[u]) {
                find_sccs(u);
            }
        }
    }

private:
    void find_sccs(size_t u) {
        dfn[u] = low[u] = ++dfs_clock;
        stk.push(u);
        on_stack[u] = true;
        for (auto v: adj[u]) {
            if (!dfn[v]) {
                find_sccs(v);
                low[u] = std::min(low[u], low[v]);
            } else if (on_stack[v]) {
                low[u] = std::min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            std::vector<size_t> scc;
            while (true) {
                auto v = stk.top();
                stk.pop();
                on_stack[v] = false;
                scc.push_back(v);
                if (v == u) {
                    break;
                }
            }
            sccs.emplace_back(std::move(scc));
        }
    }
};

int main() {
    size_t n, m;
    scanf("%zd%zd", &n, &m);
    std::vector<std::pair<size_t, size_t>> edges(m);
    for (auto &[u, v]: edges) {
        scanf("%zu%zu", &u, &v);
    }
    auto sccs = TarjanScc(edges, n).sccs;
    std::vector<std::vector<size_t>> adj(sccs.size());
    std::vector<size_t> degree(sccs.size(), 0), sccno(n, -1);
    for (size_t i = 0; i < sccs.size(); ++i) {
        for (auto u: sccs[i]) {
            sccno[u] = i;
        }
    }
    for (auto [u, v]: edges) {
        if (sccno[u] != sccno[v]) {
            adj[sccno[u]].emplace_back(sccno[v]);
        }
    }
    std::queue<size_t> q;
    for (size_t i = 0; i < sccs.size(); ++i) {
        if (!degree[i]) {
            q.push(i);
        }
    }
    printf("%zu\n", sccs.size());
    while (!q.empty()) {
        auto u = q.front();
        q.pop();
        printf("%zu ", sccs[u].size());
        for (auto x: sccs[u]) {
            printf("%zu ", x);
        }
        puts("");
        for (auto v: adj[u]) {
            if (!--degree[v]) {
                q.push(v);
            }
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 4076kb

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)