QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588207#906. 强连通分量shiqiaqiayaWA 0ms3668kbC++173.4kb2024-09-25 06:31:522024-09-25 06:31:53

Judging History

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

  • [2024-09-25 06:31:53]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3668kb
  • [2024-09-25 06:31:52]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define int long long
using ll = long long;
mt19937_64 rd(time(0));
template <class K, class C = less<>> using paring_heap = __gnu_pbds::priority_queue<K, C>;
template <class K, class V = null_type, class C = less<>> using rb_tree = tree<K, V, C, rb_tree_tag, tree_order_statistics_node_update>;
template <class T, class ... A> void debug(const T & t, const A & ... a) { cerr << "[" << t, ((cerr << ", " << a), ...), cerr << "]\n"; }
const ll mod = [](bool n) { return n ? 998244353 : 1e9 + 7; } (1);

template <ll mod = mod> ll binpow(ll a, ll b, ll res = 1) {
    for (a %= mod; b; b >>= 1, (a *= a) %= mod) {
        if (b & 1) (res *= a) %= mod;
    }
    return res;
}

template <class type = int, int len = 2> struct Tarjan_SC : vector<vector<signed>> {
    vector<array<type, len>> e;
    vector<signed> dfn, scc, low;
    vector sc = {};
    Tarjan_SC(int n, int m) : vector(n), e(m), dfn(n), scc(n), low(n) {}
    void operator()(stack<signed> s = {}, int tot_dfn = 0) {    // !! 会包含 0 , 可能 0 会成孤岛
        vector<signed> vis(size());
        for (int rt = 0; rt < size(); rt++) {
            auto self = [&](auto && self, int u) -> void {
                s.emplace(u), vis[u] = 1, dfn[u] = low[u] = ++tot_dfn;
                for (auto & i : at(u)) {
                    int v = e[i][0] ^ e[i][1] ^ u;
                    if (!dfn[v]) self(self, v), low[u] = min(low[u], low[v]);
                    else if (vis[v]) low[u] = min(low[u], dfn[v]);
                }
                if (dfn[u] == low[u]) for (sc.emplace_back(); !sc.back().size() || sc.back().back() != u; s.pop()) {
                    scc[s.top()] = sc.size() - 1, vis[s.top()] = 0, sc.back().emplace_back(s.top());
                }
            };
            if (!dfn[rt]) self(self, rt), s = {};
        }
    }
    pair<vector, vector<signed>> Topu_Rebuild() {
        vector<signed> du(sc.size());
        vector adj(sc.size());
        for (int u = 0; u < sc.size(); u++) {
            unordered_map<int, int> mp = {{u, 1}};
            for (auto & tu : sc[u]) {
                for (auto & i : at(tu)) {
                    int tv = e[i][0] ^ e[i][1] ^ tu, v = scc[tv];
                    if (!mp.count(v)) adj[u].emplace_back(v), mp[v] = 1;
                }
            }
        }
        return {adj, du};
    }
};


void QAQ() {
    int n, m;
    cin >> n >> m;

    Tarjan_SC tar(n, m);

    for (int i = 0, u, v; i < m; i++) {
        cin >> u >> v;
        tar[u].push_back(i);
        tar.e[i] = {u, v};
    }

    tar();

    cout << tar.sc.size() << "\n";

    auto [adj, du] = tar.Topu_Rebuild();

    queue<signed> q;
    for (int u = 0; u < adj.size(); u++) {
        if (du[u] == 0) q.emplace(u);
    }

    while (q.size()) {
        auto u = q.front(); q.pop();
        cout << tar.sc[u].size();
        for (auto & v : tar.sc[u]) {
            cout << " " << v;
        }
        cout << "\n";
        for (auto & v : adj[u]) {
            if (--du[v] == 0) q.emplace(v);
        }
    }
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    for (cout << fixed << setprecision(12); t--; QAQ());
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3668kb

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)