結果

問題 No.2531 Coloring Vertices on Namori
ユーザー suisensuisen
提出日時 2023-11-03 23:32:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 931 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 23,748 KB
最終ジャッジ日時 2023-11-03 23:32:07
合計ジャッジ時間 5,188 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 71 ms
23,748 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 70 ms
23,748 KB
testcase_08 AC 98 ms
23,748 KB
testcase_09 AC 102 ms
23,748 KB
testcase_10 AC 111 ms
23,748 KB
testcase_11 AC 52 ms
16,624 KB
testcase_12 AC 52 ms
16,624 KB
testcase_13 AC 52 ms
16,624 KB
testcase_14 AC 131 ms
23,748 KB
testcase_15 AC 107 ms
23,748 KB
testcase_16 AC 105 ms
23,748 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 96 ms
15,828 KB
testcase_21 AC 97 ms
15,828 KB
testcase_22 AC 99 ms
15,828 KB
testcase_23 AC 95 ms
15,828 KB
testcase_24 AC 93 ms
15,828 KB
testcase_25 AC 93 ms
15,828 KB
testcase_26 AC 99 ms
15,828 KB
testcase_27 AC 105 ms
15,828 KB
testcase_28 AC 124 ms
15,828 KB
testcase_29 AC 101 ms
15,828 KB
testcase_30 AC 98 ms
15,828 KB
testcase_31 AC 99 ms
15,828 KB
testcase_32 AC 96 ms
15,828 KB
testcase_33 AC 112 ms
15,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <atcoder/dsu>
#include <atcoder/modint>

using mint = atcoder::modint998244353;

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

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

    std::vector<std::vector<int>> g(n);

    atcoder::dsu uf(n);
    int x = -1, y = -1;
    for (int i = 0; i < n; ++i) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        if (uf.same(u, v)) {
            x = u, y = v;
        } else {
            g[u].push_back(v);
            g[v].push_back(u);
            uf.merge(u, v);
        }
    }

    std::vector<int> d(n);
    auto dfs = [&](auto dfs, int u, int p) -> void { for (int v : g[u]) if (v != p) d[v] = d[u] + 1, dfs(dfs, v, u); };
    dfs(dfs, x, -1);
    int t = d[y] + 1;

    mint a = mint(k - 1).pow(n), b = mint(k - 1).pow(n - t + 1);
    std::cout << (t % 2 ? a - b : a + b).val() << std::endl;
}
0