結果

問題 No.2531 Coloring Vertices on Namori
ユーザー ks2mks2m
提出日時 2023-11-03 22:40:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,387 ms / 2,000 ms
コード長 1,917 bytes
コンパイル時間 3,308 ms
コンパイル使用メモリ 78,628 KB
実行使用メモリ 156,812 KB
最終ジャッジ日時 2023-11-03 22:41:08
合計ジャッジ時間 30,284 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,364 KB
testcase_01 AC 53 ms
53,372 KB
testcase_02 AC 53 ms
53,372 KB
testcase_03 AC 53 ms
53,368 KB
testcase_04 AC 52 ms
53,368 KB
testcase_05 AC 1,065 ms
151,324 KB
testcase_06 AC 54 ms
53,372 KB
testcase_07 AC 992 ms
156,812 KB
testcase_08 AC 1,142 ms
134,992 KB
testcase_09 AC 1,190 ms
153,212 KB
testcase_10 AC 1,135 ms
153,336 KB
testcase_11 AC 737 ms
97,652 KB
testcase_12 AC 776 ms
97,616 KB
testcase_13 AC 651 ms
93,492 KB
testcase_14 AC 1,180 ms
131,676 KB
testcase_15 AC 1,223 ms
145,584 KB
testcase_16 AC 1,387 ms
148,516 KB
testcase_17 AC 53 ms
53,372 KB
testcase_18 AC 53 ms
53,380 KB
testcase_19 AC 53 ms
53,364 KB
testcase_20 AC 873 ms
95,356 KB
testcase_21 AC 786 ms
96,372 KB
testcase_22 AC 994 ms
96,168 KB
testcase_23 AC 974 ms
95,944 KB
testcase_24 AC 844 ms
95,988 KB
testcase_25 AC 911 ms
95,848 KB
testcase_26 AC 867 ms
96,064 KB
testcase_27 AC 1,008 ms
96,524 KB
testcase_28 AC 973 ms
96,232 KB
testcase_29 AC 1,058 ms
97,784 KB
testcase_30 AC 885 ms
96,236 KB
testcase_31 AC 915 ms
96,068 KB
testcase_32 AC 1,019 ms
95,932 KB
testcase_33 AC 986 ms
97,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

public class Main {
	static int n, k;
	static List<List<Integer>> list;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		n = Integer.parseInt(sa[0]);
		k = Integer.parseInt(sa[1]);
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			int u = Integer.parseInt(sa[0]) - 1;
			int v = Integer.parseInt(sa[1]) - 1;
			list.get(u).add(v);
			list.get(v).add(u);
		}
		br.close();

		List<Integer> res = getCycle(0, -1, new LinkedHashSet<>());
		int size = res.size();
		int mod = 998244353;
		long[][] dp = new long[2][size];
		dp[0][0] = k;
		for (int i = 1; i < size; i++) {
			dp[1][i] += dp[0][i - 1] * (k - 1);
			dp[0][i] += dp[1][i - 1];
			dp[1][i] += dp[1][i - 1] * (k - 2);
			dp[1][i] %= mod;
		}
		long ans = dp[1][size - 1];
		ans *= power(k - 1, n - size, mod);
		ans %= mod;
		System.out.println(ans);
	}

	static List<Integer> getCycle(int x, int p, LinkedHashSet<Integer> his) {
		if (his.contains(x)) {
			List<Integer> ret = new ArrayList<>();
			boolean flg = false;
			for (int i : his) {
				if (i == x) {
					flg = true;
				}
				if (flg) {
					ret.add(i);
				}
			}
			return ret;
		}
		his.add(x);
		for (int i : list.get(x)) {
			if (i == p) {
				continue;
			}
			List<Integer> res = getCycle(i, x, his);
			if (res != null) {
				return res;
			}
		}
		his.remove(x);
		return null;
	}

	static long power(long x, long n, int m) {
		if (n == 0) {
			return 1;
		}
		long val = power(x, n / 2, m);
		val = val * val % m;
		if (n % 2 == 1) {
			x %= m;
			val = val * x % m;
		}
		return val;
	}
}
0