結果

問題 No.2531 Coloring Vertices on Namori
コンテスト
ユーザー maeshun
提出日時 2023-11-11 17:59:38
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 1,469 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,283 ms
コンパイル使用メモリ 283,444 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2026-07-03 00:13:09
合計ジャッジ時間 9,669 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:54:35: warning: 't' may be used uninitialized [-Wmaybe-uninitialized]
   54 |     vector<vector<mint>> dp(dist[t]+1, vector<mint>(2));
      |                                   ^
main.cpp:25:12: note: 't' was declared here
   25 |     int s, t;
      |            ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/allocator.h:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/string:45,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bitset:54,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
In member function 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = int; _Args = {const int&}; _Tp = int]',
    inlined from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = int; _Args = {const int&}; _Tp = int]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/alloc_traits.h:674:17,
    inlined from 'void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_deque.h:1610:30,
    inlined from 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_queue.h:313:20,
    inlined from 'int main()' at main.cpp:40:11:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/new_allocator.h:191:11: warning: 's' may be used uninitialized [-Wmaybe-uninitialized]
  191 |         { ::new((void *)__p) _Up(std::forward<_A

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i, n) for(int i=0;i<(n);++i)
#define rep1(i, n) for(int i=1;i<=(n);i++)
#define ll long long
using mint = modint998244353;
using P = pair<ll,ll>;
using lb = long double;
using T = tuple<ll, ll, ll>;
#ifdef LOCAL
#  include <debug_print.hpp>
#  define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define dbg(...) (static_cast<void>(0))
#endif

int main()
{
    int n, k;
    cin >> n >> k;
    vector<vector<int>> g(n);
    dsu uf(n);
    int s, t;
    rep(i,n){
        int a, b;
        cin >> a >> b;
        --a;--b;
        if(uf.same(a,b)){
            s = a;
            t = b;
            continue;
        }
        uf.merge(a,b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    queue<int> q;
    q.push(s);
    vector<int> dist(n, 1e9);
    dist[s] = 0;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int v : g[u]){
            if(dist[v]!=1e9) continue;
            q.push(v);
            dist[v] = dist[u] + 1;
        }
    }
    dbg(s,t);
    dbg(dist);
    vector<vector<mint>> dp(dist[t]+1, vector<mint>(2));
    dp[0][0] = 1;
    rep(i,dist[t]){
        dp[i+1][0] += dp[i][1];
        dp[i+1][1] += dp[i][0] * (k-1) + dp[i][1] * (k-2); 
    }
    mint ans = dp[dist[t]][1];
    ans *= k;
    ans *= mint(k-1).pow(n-dist[t]-1);
    cout << ans.val() << endl;
    return 0;
}
0