結果

問題 No.2531 Coloring Vertices on Namori
ユーザー maeshun
提出日時 2023-11-11 17:59:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 4,512 ms
コンパイル使用メモリ 260,380 KB
最終ジャッジ日時 2025-02-17 21:40:39
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 /usr/include/x86_64-linux-gnu/c++/13/bits/c++allocator.h:33,
                 from /usr/include/c++/13/bits/allocator.h:46,
                 from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 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 /usr/include/c++/13/bits/alloc_traits.h:538:17,
    inlined from ‘void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>]’ at /usr/include/c++/13/bits/stl_deque.h:1543:30,
    inlined from ‘void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >]’ at /usr/include/c++/13/bits/stl_queue.h:286:20,
    inlined from ‘int main()’ at main.cpp:40:11:
/usr/include/c++/13/bits/new_allocator.h:191:11: warning: ‘s’ may be used uninitialized [-Wmaybe-uninitialized]
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:25:9: note: ‘s’ was declared here
   25 |     int s, t;
      |         ^

ソースコード

diff #

#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