結果

問題 No.2531 Coloring Vertices on Namori
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2023-11-03 23:14:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,196 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 210,196 KB
実行使用メモリ 16,820 KB
最終ジャッジ日時 2023-11-03 23:15:06
合計ジャッジ時間 7,921 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 134 ms
16,820 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 136 ms
16,820 KB
testcase_08 AC 170 ms
16,820 KB
testcase_09 AC 169 ms
16,820 KB
testcase_10 AC 172 ms
16,820 KB
testcase_11 AC 102 ms
12,160 KB
testcase_12 AC 94 ms
12,160 KB
testcase_13 AC 95 ms
12,160 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
4,348 KB
testcase_18 WA -
testcase_19 AC 2 ms
4,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll const m = 998244353;
int main () {
	int N; ll K;
	cin >> N >> K;
	std::vector<vector<int>> gr(N);
	for (int i = 0; i < N; i ++) {
		int a, b;
		cin >> a >> b;
		if (a > b) swap(a, b);
		gr[--a].push_back(--b);
	}
	vector<int> D(N, -1), D2(N, -1);
	int len = 0;
	for (int i = 0; i < N; i ++) {
		if (D[i] != -1) continue;
		D[i] = D2[i] = 0;
		vector<int> V;
		queue<int> que;
		que.push(i);
		V.push_back(i);
		while (!que.empty()) {
			int u = que.front();
			que.pop();
			for (int v : gr[u]) {
				if (u == v) {
					len = 1;
					break;
				}
				if (D2[v] == -1) {
					D[v] = D2[v] = D2[u] + 1;
					que.push(v);
					V.push_back(v);
				} else {
					int p = D2[u] + 1, q = D2[v];
					len = p - q + 2;
					break;
				}
			}
		}
		if (len) break;
		for (auto u : V) {
			D2[u] = -1;
		}
	}
	ll ans = K;
	ll hog = K;
	if (len > 1) {
		ans = (K * (K - 1)) % m;
		hog = (K * (K - 1)) % m;
	}
	for (int i = 2; i < len; i ++) {
		ans = (K - 2) * ans + (K - 1) * ((hog - ans + m) % m);
		ans %= m;
		hog = (hog * (K - 1)) % m;
	}
	for (int i = 0; i < N - len; i ++) {
		ans = (ans * (K - 1)) % m;
	}
	cout << ans << endl;
}
0