結果

問題 No.2588 Increasing Record
ユーザー suisensuisen
提出日時 2023-12-23 20:52:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 345 ms / 3,000 ms
コード長 3,676 bytes
コンパイル時間 1,772 ms
コンパイル使用メモリ 115,512 KB
実行使用メモリ 40,432 KB
最終ジャッジ日時 2023-12-23 20:52:24
合計ジャッジ時間 11,607 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 164 ms
15,616 KB
testcase_13 AC 174 ms
15,488 KB
testcase_14 AC 170 ms
15,616 KB
testcase_15 AC 175 ms
16,384 KB
testcase_16 AC 215 ms
24,064 KB
testcase_17 AC 289 ms
30,180 KB
testcase_18 AC 254 ms
35,072 KB
testcase_19 AC 236 ms
36,608 KB
testcase_20 AC 228 ms
35,592 KB
testcase_21 AC 219 ms
35,184 KB
testcase_22 AC 255 ms
35,184 KB
testcase_23 AC 217 ms
35,316 KB
testcase_24 AC 213 ms
35,184 KB
testcase_25 AC 192 ms
26,164 KB
testcase_26 AC 174 ms
29,880 KB
testcase_27 AC 175 ms
33,904 KB
testcase_28 AC 208 ms
33,692 KB
testcase_29 AC 178 ms
33,972 KB
testcase_30 AC 245 ms
27,904 KB
testcase_31 AC 289 ms
34,188 KB
testcase_32 AC 320 ms
39,108 KB
testcase_33 AC 304 ms
40,308 KB
testcase_34 AC 313 ms
40,428 KB
testcase_35 AC 302 ms
40,432 KB
testcase_36 AC 345 ms
40,432 KB
testcase_37 AC 209 ms
34,892 KB
testcase_38 AC 226 ms
28,212 KB
testcase_39 AC 134 ms
31,636 KB
testcase_40 AC 135 ms
31,880 KB
testcase_41 AC 113 ms
32,348 KB
testcase_42 AC 117 ms
32,468 KB
testcase_43 AC 127 ms
33,068 KB
testcase_44 AC 211 ms
26,192 KB
testcase_45 AC 200 ms
29,520 KB
testcase_46 AC 167 ms
32,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <deque>
#include <iostream>
#include <utility>
#include <vector>

#include <atcoder/modint>

using mint = atcoder::modint998244353;

std::vector<mint> naive(int n, const std::vector<std::pair<int, int>>& edges) {
    std::vector<std::vector<int>> g(n);
    for (const auto& [u, v] : edges) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    std::vector<mint> f(n, 1);
    for (int x = 0; x < n; ++x) {
        std::vector<int8_t> vis(n);
        vis[x] = true;
        std::deque<int> dq{ x };
        while (dq.size()) {
            int u = dq.front();
            dq.pop_front();
            for (int v : g[u]) if (not vis[v]) {
                vis[v] = true;
                if (v < x) {
                    dq.push_back(v);
                }
                if (v > x) {
                    f[v] += f[x];
                }
            }
        }
    }
    return f;
}

#include <atcoder/dsu>
#include <algorithm>
#include <set>

std::vector<mint> solve(int n, const std::vector<std::pair<int, int>>& edges) {
    atcoder::dsu uf(n);

    std::vector<std::vector<int>> ts(n);
    for (const auto& [u, v] : edges) {
        ts[u].push_back(v);
    }

    std::vector<std::set<int>> neighbors(n);
    std::vector<mint> lazy(n);

    for (const auto& [u, v] : edges) {
        neighbors[v].insert(u);
    }

    std::vector<mint> f(n, 1);
    for (int s = 0; s < n; ++s) {
        std::vector<int> cmps;
        for (int t : ts[s]) {
            cmps.push_back(uf.leader(t));
        }
        std::sort(cmps.begin(), cmps.end());
        cmps.erase(std::unique(cmps.begin(), cmps.end()), cmps.end());
        for (int t : cmps) {
            uf.merge(s, t);
            f[s] += lazy[t];
            lazy[s] += lazy[t];
        }
        lazy[s] += f[s];
        if (cmps.empty()) continue;

        auto argmax = *std::max_element(cmps.begin(), cmps.end(), [&](int i, int j) { return neighbors[i].size() < neighbors[j].size(); });
        for (int t : cmps) if (t != argmax) {
            lazy[s] -= lazy[t];
        }
        for (int t : cmps) if (t != argmax) {
            for (int neighbor : neighbors[t]) if (neighbor != s) {
                f[neighbor] += lazy[t];
                if (neighbors[argmax].count(neighbor)) {
                    continue;
                }
                f[neighbor] -= lazy[argmax];
                neighbors[argmax].insert(neighbor);
            }
        }
        if (s != argmax) {
            for (int neighbor : neighbors[s]) if (neighbor != s) {
                if (neighbors[argmax].count(neighbor)) {
                    continue;
                }
                f[neighbor] -= lazy[argmax];
                neighbors[argmax].insert(neighbor);
            }
        }
        int root = uf.leader(s);
        std::swap(lazy[root], lazy[s]);
        std::swap(neighbors[root], neighbors[argmax]);
        neighbors[root].erase(s);
    }
    return f;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    std::vector<std::pair<int, int>> edges(m);
    for (auto& [u, v] : edges) {
        std::cin >> u >> v;
        --u, --v;
        if (u < v) {
            std::swap(u, v);
        }
    }

    bool debug = false;
    if (debug) {
        auto f = naive(n, edges);
        for (const auto& e : f) {
            std::cout << e.val() << std::endl;
        }

        auto g = solve(n, edges);
        for (const auto& e : g) {
            std::cout << e.val() << std::endl;
        }
    } else {
        mint ans = 0;
        for (const auto &e : solve(n, edges)) ans += e;
        std::cout << ans.val() << std::endl;
    }
}
0