結果
問題 | No.2531 Coloring Vertices on Namori |
ユーザー | keisuke6 |
提出日時 | 2023-11-03 21:49:55 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 282 ms / 2,000 ms |
コード長 | 1,561 bytes |
コンパイル時間 | 2,725 ms |
コンパイル使用メモリ | 213,180 KB |
実行使用メモリ | 25,124 KB |
最終ジャッジ日時 | 2023-11-03 21:50:14 |
合計ジャッジ時間 | 9,954 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 1 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 1 ms
4,348 KB |
testcase_04 | AC | 2 ms
4,348 KB |
testcase_05 | AC | 220 ms
25,124 KB |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 217 ms
25,124 KB |
testcase_08 | AC | 273 ms
25,124 KB |
testcase_09 | AC | 282 ms
25,124 KB |
testcase_10 | AC | 277 ms
25,124 KB |
testcase_11 | AC | 122 ms
23,744 KB |
testcase_12 | AC | 121 ms
23,744 KB |
testcase_13 | AC | 122 ms
23,744 KB |
testcase_14 | AC | 270 ms
25,124 KB |
testcase_15 | AC | 268 ms
25,124 KB |
testcase_16 | AC | 273 ms
25,124 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 | 215 ms
20,112 KB |
testcase_21 | AC | 215 ms
19,844 KB |
testcase_22 | AC | 218 ms
19,844 KB |
testcase_23 | AC | 210 ms
19,844 KB |
testcase_24 | AC | 208 ms
19,844 KB |
testcase_25 | AC | 214 ms
19,844 KB |
testcase_26 | AC | 213 ms
19,844 KB |
testcase_27 | AC | 208 ms
19,844 KB |
testcase_28 | AC | 198 ms
20,108 KB |
testcase_29 | AC | 194 ms
19,844 KB |
testcase_30 | AC | 197 ms
20,108 KB |
testcase_31 | AC | 193 ms
20,108 KB |
testcase_32 | AC | 197 ms
20,108 KB |
testcase_33 | AC | 198 ms
19,844 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long struct Unionfind{ vector<int> par, siz; //初期化 Unionfind(int n) : par(n,-1) , siz(n,1) {} int root(int x) { if(par[x] == -1) return x; else return par[x] = root(par[x]); } bool issame(int x,int y) { return root(x) == root(y); } bool unite(int x, int y){ x = root(x); y = root(y); if(x == y) return false; if(siz[x] < siz[y]) swap(x,y); par[y] = x; siz[x] += siz[y]; return true; } int size(int x) { return siz[root(x)]; } }; int mod = 998244353; int N,K; int int_pow(int x, int n) { int ret = 1; while (n > 0) { if (n & 1) ret = ret * x % mod; x = x * x % mod; n >>= 1; } return ret; } int f(int a){ if(a == 2){ return K*(K-1)%mod; } else { return (K*int_pow(K-1,a-1)-f(a-1))%mod; } } signed main(){ cin>>N>>K; vector<vector<int>> G(N); Unionfind uf(N); int s = 0; for(int i=0;i<N;i++){ int a,b; cin>>a>>b; a--; b--; G[a].push_back(b); G[b].push_back(a); if(uf.issame(a,b)) s = a; else uf.unite(a,b); } vector<int> dist(N,1e18); dist[s] = 0; stack<pair<int,int>> q; q.push({s,-1}); while(!q.empty()){ auto pos = q.top(); q.pop(); if(dist[s]) break; for(int x:G[pos.first]){ if(pos.second == x) continue; q.push({x,pos.first}); dist[x] = dist[pos.first]+1; } } int ans = f(dist[s]); for(int i=dist[s];i<N;i++) ans = (ans*(K-1))%mod; cout<<ans<<endl; }