結果

問題 No.2531 Coloring Vertices on Namori
ユーザー cho435cho435
提出日時 2023-11-03 23:34:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 5,646 ms
コンパイル使用メモリ 268,884 KB
実行使用メモリ 26,124 KB
最終ジャッジ日時 2023-11-03 23:35:04
合計ジャッジ時間 11,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 158 ms
26,124 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 137 ms
26,124 KB
testcase_08 AC 158 ms
26,124 KB
testcase_09 AC 159 ms
26,124 KB
testcase_10 AC 157 ms
26,124 KB
testcase_11 AC 108 ms
16,744 KB
testcase_12 AC 96 ms
16,744 KB
testcase_13 AC 98 ms
16,744 KB
testcase_14 AC 152 ms
26,124 KB
testcase_15 AC 157 ms
26,124 KB
testcase_16 AC 157 ms
26,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 149 ms
15,448 KB
testcase_21 AC 153 ms
15,436 KB
testcase_22 AC 179 ms
15,436 KB
testcase_23 AC 160 ms
15,436 KB
testcase_24 AC 150 ms
15,580 KB
testcase_25 AC 151 ms
15,536 KB
testcase_26 AC 171 ms
15,584 KB
testcase_27 AC 149 ms
15,472 KB
testcase_28 AC 152 ms
15,528 KB
testcase_29 AC 168 ms
15,472 KB
testcase_30 AC 151 ms
15,564 KB
testcase_31 AC 152 ms
15,488 KB
testcase_32 AC 151 ms
15,436 KB
testcase_33 AC 152 ms
15,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using mint = atcoder::modint998244353;

int main(){
	int n,k;
	cin>>n>>k;
	vector<vector<int>> g(n);
	vector<int> tn(n,0);
	rep(i,n){
		int u,v;
		cin>>u>>v;
		u--; v--;
		g.at(u).push_back(v);
		g.at(v).push_back(u);
		tn.at(u)++;
		tn.at(v)++;
	}
	queue<int> cn1;
	rep(i,n) if(tn.at(i)==1) cn1.push(i);
	while(!cn1.empty()){
		int x=cn1.front();
		cn1.pop();
		for(int y:g.at(x)){
			tn.at(y)--;
			if(tn.at(y)==1) cn1.push(y);
		}
	}
	mint ans=1;
	int lp=0;
	rep(i,n) if(tn.at(i)==2) lp++;
	ans*=mint(k-1).pow(n-lp);
	vector<vector<mint>> dp(lp,vector<mint>(2,0));
	dp.at(0).at(0)=k;
	rep(i,lp-1){
		dp.at(i+1).at(0)+=dp.at(i).at(1);
		dp.at(i+1).at(1)+=dp.at(i).at(0)*(k-1);
		dp.at(i+1).at(1)+=dp.at(i).at(1)*(k-2);
	}
	ans*=dp.at(lp-1).at(1);
	cout<<ans.val()<<endl;
}
0