結果

問題 No.2531 Coloring Vertices on Namori
ユーザー coindarwcoindarw
提出日時 2023-11-03 22:56:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 2,742 bytes
コンパイル時間 4,780 ms
コンパイル使用メモリ 280,244 KB
実行使用メモリ 35,180 KB
最終ジャッジ日時 2023-11-03 22:57:03
合計ジャッジ時間 9,211 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 135 ms
35,180 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 135 ms
35,180 KB
testcase_08 AC 195 ms
35,180 KB
testcase_09 AC 143 ms
35,180 KB
testcase_10 AC 197 ms
35,180 KB
testcase_11 AC 51 ms
16,636 KB
testcase_12 AC 50 ms
16,636 KB
testcase_13 AC 51 ms
16,636 KB
testcase_14 AC 200 ms
35,180 KB
testcase_15 AC 201 ms
35,180 KB
testcase_16 AC 197 ms
35,180 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 101 ms
15,044 KB
testcase_21 AC 98 ms
15,044 KB
testcase_22 AC 99 ms
15,044 KB
testcase_23 AC 92 ms
15,044 KB
testcase_24 AC 91 ms
15,044 KB
testcase_25 AC 96 ms
15,044 KB
testcase_26 AC 89 ms
15,044 KB
testcase_27 AC 87 ms
15,044 KB
testcase_28 AC 89 ms
15,044 KB
testcase_29 AC 113 ms
15,044 KB
testcase_30 AC 88 ms
15,044 KB
testcase_31 AC 87 ms
15,044 KB
testcase_32 AC 89 ms
15,044 KB
testcase_33 AC 97 ms
15,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef ONLINE_JUDGE
#include <bits/stdc++.h>

#include <atcoder/all>
#else
#include <mylibs/all.h>
#endif

using ll = long long;
using lll = __int128_t;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define length(v) ((int)(v).size())
constexpr int inf = 2'000'000'000;
constexpr ll linf = 4'000'000'000'000'000'000, M7 = 1'000'000'007, M9 = 998'244'353;
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using namespace atcoder;

using mint = modint998244353;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    struct edge {
        int to;
        edge(int to) : to(to) {}
    };
    vector<vector<edge>> G(n);
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    auto search_cycle = [](const auto& G) {
        const int n = G.size();
        vector<bool> seen(n);
        vector<int> prv(n, -1);
        rep(i, n) {
            if (seen.at(i)) continue;
            stack<int> s;
            s.emplace(i);
            while (!s.empty()) {
                int u = s.top();
                s.pop();
                seen.at(u) = true;
                for (auto& e : G.at(u)) {
                    if (!seen.at(e.to)) {
                        prv.at(e.to) = u;
                        s.emplace(e.to);
                    } else if (e.to != prv.at(u)) {
                        vector<int> cycle = {u};
                        int p = u;
                        while (p != e.to) {
                            p = prv.at(p);
                            cycle.emplace_back(p);
                        }
                        reverse(all(cycle));
                        return cycle;
                    }
                }
            }
        }
        return vector<int>();
    };
    auto cycle = search_cycle(G);
    set<int> st(all(cycle));
    int m = cycle.size();

    vector<vector<mint>> dp = vector<vector<mint>>(m, vector<mint>(2));
    dp.at(0).at(1) = k;
    rep(i, m - 2) {
        dp.at(i + 1).at(0) = dp.at(i).at(0) * (k - 2) + dp.at(i).at(1) * (k - 1);
        dp.at(i + 1).at(1) = dp.at(i).at(0);
    }
    dp.at(m - 1).at(0) = dp.at(m - 2).at(0) * (k - 2) + dp.at(m - 2).at(1) * (k - 1);
    mint ans = dp.at(m - 1).at(0) * mint(k - 1).pow(n - cycle.size());
    cout << ans.val() << endl;
    return 0;
}
0