結果

問題 No.2531 Coloring Vertices on Namori
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-04-02 01:47:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 1,189 bytes
コンパイル時間 2,459 ms
コンパイル使用メモリ 211,448 KB
実行使用メモリ 16,708 KB
最終ジャッジ日時 2024-04-02 01:47:50
合計ジャッジ時間 8,220 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 2 ms
6,676 KB
testcase_05 AC 60 ms
15,044 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 59 ms
15,044 KB
testcase_08 AC 80 ms
15,044 KB
testcase_09 AC 78 ms
15,044 KB
testcase_10 AC 81 ms
15,044 KB
testcase_11 AC 50 ms
16,708 KB
testcase_12 AC 50 ms
16,708 KB
testcase_13 AC 50 ms
16,708 KB
testcase_14 AC 76 ms
15,044 KB
testcase_15 AC 80 ms
15,044 KB
testcase_16 AC 77 ms
15,044 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 87 ms
15,428 KB
testcase_21 AC 103 ms
15,428 KB
testcase_22 AC 106 ms
15,428 KB
testcase_23 AC 87 ms
15,428 KB
testcase_24 AC 94 ms
15,428 KB
testcase_25 AC 84 ms
15,428 KB
testcase_26 AC 87 ms
15,428 KB
testcase_27 AC 89 ms
15,428 KB
testcase_28 AC 89 ms
15,428 KB
testcase_29 AC 89 ms
15,428 KB
testcase_30 AC 92 ms
15,428 KB
testcase_31 AC 92 ms
15,428 KB
testcase_32 AC 87 ms
15,428 KB
testcase_33 AC 101 ms
15,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/modint>
using namespace std;
using mint = atcoder::modint998244353;
void fast_io() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

int main() {
    fast_io();
    int n, k;
    cin >> n >> k;
    vector<vector<int>> g(n);
    vector<int> deg(n, 0);
    for (int i = 0; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    deque<int> q;
    for (int i = 0; i < n; i++) {
        if (deg[i] == 1) {
            q.push_back(i);
        }
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop_front();
        for (int v : g[u]) {
            if (deg[v] > 1) {
                deg[v]--;
                if (deg[v] == 1) {
                    q.push_back(v);
                }
            }
        }
    }
    int loop = 0;
    for (int i = 0; i < n; i++) {
        if (deg[i] > 1) {
            loop++;
            assert(deg[i] == 2);
        }
    }
    mint ans = mint(k - 1).pow(loop) + (loop % 2 ? -(k - 1) : k - 1);
    ans *= mint(k - 1).pow(n - loop);
    cout << ans.val() << endl;
}
0