結果

問題 No.2531 Coloring Vertices on Namori
ユーザー 👑 kmjpkmjp
提出日時 2023-11-03 21:58:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,399 bytes
コンパイル時間 5,089 ms
コンパイル使用メモリ 213,604 KB
実行使用メモリ 34,924 KB
最終ジャッジ日時 2023-11-03 21:59:01
合計ジャッジ時間 10,893 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,464 KB
testcase_01 AC 5 ms
13,464 KB
testcase_02 AC 5 ms
13,464 KB
testcase_03 AC 5 ms
13,464 KB
testcase_04 AC 5 ms
13,464 KB
testcase_05 AC 67 ms
34,924 KB
testcase_06 AC 5 ms
13,464 KB
testcase_07 AC 69 ms
34,924 KB
testcase_08 AC 115 ms
34,924 KB
testcase_09 AC 135 ms
34,924 KB
testcase_10 AC 117 ms
34,924 KB
testcase_11 AC 141 ms
32,704 KB
testcase_12 AC 140 ms
32,704 KB
testcase_13 AC 141 ms
32,704 KB
testcase_14 AC 136 ms
34,924 KB
testcase_15 AC 113 ms
34,924 KB
testcase_16 AC 108 ms
34,924 KB
testcase_17 AC 5 ms
13,464 KB
testcase_18 AC 5 ms
13,464 KB
testcase_19 AC 5 ms
13,464 KB
testcase_20 AC 183 ms
32,392 KB
testcase_21 AC 189 ms
32,532 KB
testcase_22 AC 187 ms
32,528 KB
testcase_23 AC 190 ms
32,508 KB
testcase_24 AC 202 ms
32,512 KB
testcase_25 AC 204 ms
32,464 KB
testcase_26 AC 213 ms
32,528 KB
testcase_27 AC 168 ms
32,420 KB
testcase_28 AC 179 ms
32,476 KB
testcase_29 AC 172 ms
32,408 KB
testcase_30 AC 165 ms
32,376 KB
testcase_31 AC 165 ms
32,444 KB
testcase_32 AC 154 ms
32,460 KB
testcase_33 AC 199 ms
32,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,K;
set<int> E[202020];

ll dp[202020][2];
const ll mo=998244353;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N) {
		cin>>x>>y;
		x--,y--;
		E[x].insert(y);
		E[y].insert(x);
	}
	queue<int> Q;
	FOR(i,N) if(E[i].size()==1) Q.push(i);
	int num=N;
	while(Q.size()) {
		x=Q.front();
		Q.pop();
		num--;
		FORR(e,E[x]) {
			E[e].erase(x);
			if(E[e].size()==1) Q.push(e);
		}
	}
	
	dp[0][0]=K;
	dp[0][1]=0;
	FOR(i,num-1) {
		dp[i+1][0]=dp[i][1];
		(dp[i+1][1]=dp[i][0]*(K-1)+dp[i][1]*(K-2))%=mo;
	}
	ll ret=dp[num-1][1];
	FOR(i,N-num) ret=ret*(K-1)%mo;
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0