結果

問題 No.2588 Increasing Record
ユーザー 👑 rin204rin204
提出日時 2023-11-24 14:58:27
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,665 bytes
コンパイル時間 3,286 ms
コンパイル使用メモリ 261,092 KB
実行使用メモリ 33,184 KB
最終ジャッジ日時 2023-12-15 23:30:55
合計ジャッジ時間 10,888 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1 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 2 ms
6,676 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 94 ms
6,676 KB
testcase_13 AC 94 ms
6,676 KB
testcase_14 WA -
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 146 ms
16,256 KB
testcase_31 AC 163 ms
22,732 KB
testcase_32 AC 182 ms
27,568 KB
testcase_33 AC 182 ms
28,660 KB
testcase_34 AC 183 ms
28,772 KB
testcase_35 AC 181 ms
28,776 KB
testcase_36 AC 190 ms
28,776 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 165 ms
23,676 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#include "atcoder/modint.hpp"
using mint = atcoder::modint998244353;

struct UnionFind {
    int n;
    vector<int> par;
    vector<vector<int>> member;
    vector<mint> add;
    vector<mint> val;

    UnionFind(int n) : n(n), par(n, -1), member(n), add(n), val(n, 0) {
        for (int i = 0; i < n; i++) member[i].push_back(i);
    }

    int find(int x) {
        if (par[x] < 0) return x;
        return par[x] = find(par[x]);
    }

    void unite(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) return;
        if (par[u] > par[v]) swap(u, v);
        par[u] += par[v];
        par[v] = u;
        mint d = add[v] - add[u];
        for (auto x : member[v]) {
            member[u].push_back(x);
            val[x] += d;
        }
    }

    bool same(int u, int v) {
        return find(u) == find(v);
    }

    mint get_val(int u) {
        return val[u] + add[find(u)];
    }

    void add_val(int u, mint x) {
        add[find(u)] += x;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    vector E(n, vector<int>());
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        E[v - 1].emplace_back(u - 1);
    }
    UnionFind UF(n);
    mint ans = 0;
    for (int v = 0; v < n; v++) {
        sort(E[v].begin(), E[v].end());
        mint add = 1;
        for (auto u : E[v]) {
            if (!UF.same(u, v)) {
                add += UF.get_val(u);
                UF.unite(u, v);
            }
        }

        UF.add_val(v, add);
        ans += add;
    }
    cout << ans.val() << endl;
}

int main() {
    solve();
}
0