結果

問題 No.2588 Increasing Record
ユーザー suisensuisen
提出日時 2023-12-23 20:21:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,418 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 113,452 KB
実行使用メモリ 41,516 KB
最終ジャッジ日時 2023-12-23 20:22:07
合計ジャッジ時間 13,011 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 WA -
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 175 ms
15,360 KB
testcase_13 AC 178 ms
15,488 KB
testcase_14 AC 177 ms
15,616 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 250 ms
27,776 KB
testcase_31 WA -
testcase_32 AC 315 ms
38,980 KB
testcase_33 AC 341 ms
40,180 KB
testcase_34 AC 325 ms
40,300 KB
testcase_35 AC 321 ms
40,304 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 154 ms
31,636 KB
testcase_40 AC 179 ms
32,648 KB
testcase_41 AC 182 ms
34,836 KB
testcase_42 AC 222 ms
36,508 KB
testcase_43 AC 254 ms
41,516 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

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::set<int> cmps_set;
        for (int t : ts[s]) {
            cmps_set.insert(uf.leader(t));
        }
        std::vector<int> cmps(cmps_set.begin(), cmps_set.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];
        }

        if (neighbors[argmax].size() < neighbors[s].size()) {
            argmax = s;
        }
        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);
        }
    }

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

0