結果

問題 No.2588 Increasing Record
ユーザー fdironiafdironia
提出日時 2023-12-16 00:51:17
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 420 ms / 3,000 ms
コード長 1,460 bytes
コンパイル時間 1,048 ms
コンパイル使用メモリ 113,548 KB
実行使用メモリ 35,668 KB
最終ジャッジ日時 2023-12-16 00:51:34
合計ジャッジ時間 12,953 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 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 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 176 ms
14,208 KB
testcase_13 AC 182 ms
14,208 KB
testcase_14 AC 184 ms
14,208 KB
testcase_15 AC 229 ms
14,720 KB
testcase_16 AC 287 ms
19,200 KB
testcase_17 AC 306 ms
23,708 KB
testcase_18 AC 320 ms
28,532 KB
testcase_19 AC 337 ms
32,320 KB
testcase_20 AC 280 ms
33,248 KB
testcase_21 AC 267 ms
33,352 KB
testcase_22 AC 269 ms
33,360 KB
testcase_23 AC 279 ms
33,364 KB
testcase_24 AC 297 ms
33,364 KB
testcase_25 AC 215 ms
23,324 KB
testcase_26 AC 259 ms
28,020 KB
testcase_27 AC 257 ms
32,540 KB
testcase_28 AC 241 ms
32,432 KB
testcase_29 AC 278 ms
32,592 KB
testcase_30 AC 324 ms
24,348 KB
testcase_31 AC 364 ms
29,940 KB
testcase_32 AC 398 ms
34,496 KB
testcase_33 AC 415 ms
35,552 KB
testcase_34 AC 399 ms
35,656 KB
testcase_35 AC 400 ms
35,664 KB
testcase_36 AC 420 ms
35,668 KB
testcase_37 AC 300 ms
33,492 KB
testcase_38 AC 257 ms
23,452 KB
testcase_39 AC 214 ms
30,032 KB
testcase_40 AC 203 ms
30,032 KB
testcase_41 AC 217 ms
29,944 KB
testcase_42 AC 192 ms
29,900 KB
testcase_43 AC 180 ms
29,892 KB
testcase_44 AC 239 ms
23,516 KB
testcase_45 AC 250 ms
27,112 KB
testcase_46 AC 258 ms
31,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <numeric>
#include <algorithm>

using namespace std;

using vi = vector<int>;
using v2i = vector<vi>;
using si = set<int>;

constexpr int Z = 998244353;

int fi(int f[], int u) {
    return f[u] == u ? u : f[u] = fi(f, f[u]);
}

int main() {
    int n, m;
    cin >> n >> m;
    si to[n];
    vi e[n];
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        e[v].push_back(u);
        to[u].insert(v);
    }

    int f[n], add[n];
    iota(f, f + n, 0);
    fill(add, add + n, 0);

    int dp[n];
    fill(dp, dp + n, 1);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j : e[i]) {
            int u = fi(f, i), v = fi(f, j);
            if (u != v) {
                dp[i] = (dp[i] + add[v]) % Z;
                to[v].erase(i);

                if (to[u].size() < to[v].size()) {
                    swap(u, v);
                }

                for (int w : to[v]) {
                    dp[w] = (dp[w] + add[v]) % Z;
                    if (!to[u].count(w)) {
                        dp[w] = (dp[w] - add[u] + Z) % Z;
                        to[u].insert(w);
                    }
                }

                f[v] = u;
                to[v].clear();
            }
        }

        int u = fi(f, i);
        add[u] = (add[u] + dp[i]) % Z;
        ans = (ans + dp[i]) % Z;
    }

    cout << ans << endl;

    return 0;
}
0