結果
| 問題 |
No.2531 Coloring Vertices on Namori
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-11-03 22:10:43 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 122 ms / 2,000 ms |
| コード長 | 967 bytes |
| コンパイル時間 | 1,611 ms |
| コンパイル使用メモリ | 169,724 KB |
| 実行使用メモリ | 28,744 KB |
| 最終ジャッジ日時 | 2024-09-25 20:36:05 |
| 合計ジャッジ時間 | 6,143 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int p=998244353;
int po(int a,int b) {if(b==0) return 1;if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*u)%p;} else {int u=po(a,b-1);return (a*u)%p;}}
const int maxn=2e5+5;
vector<int> a[maxn];bool used[maxn];int corn[maxn];int cn=0;
void dfs(int x,int pr)
{
used[x]=true;
for(int v:a[x])
{
if(!used[v])
{
corn[v]=corn[x]+1;
dfs(v,x);
}
else if(v!=pr && corn[v]<corn[x])
{
cn=corn[x]-corn[v]+1;
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,k;cin>>n>>k;
for(int i=0;i<n;++i)
{
int x,y;cin>>x>>y;--x;--y;
a[x].push_back(y);a[y].push_back(x);
}
dfs(0,-1);
int res=po(k-1,cn);if(cn%2==0) {res+=(k-1);} else {res-=(k-1);}
res%=p;
res*=po(k-1,n-cn);res%=p;
cout<<(res%p+p)%p;
return 0;
}