結果

問題 No.2531 Coloring Vertices on Namori
ユーザー ks2mks2m
提出日時 2023-11-03 22:40:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,224 ms / 2,000 ms
コード長 1,917 bytes
コンパイル時間 3,022 ms
コンパイル使用メモリ 78,628 KB
実行使用メモリ 140,048 KB
最終ジャッジ日時 2024-09-25 21:06:13
合計ジャッジ時間 28,019 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,068 KB
testcase_01 AC 50 ms
36,968 KB
testcase_02 AC 51 ms
36,948 KB
testcase_03 AC 50 ms
36,876 KB
testcase_04 AC 49 ms
36,888 KB
testcase_05 AC 910 ms
132,300 KB
testcase_06 AC 50 ms
37,208 KB
testcase_07 AC 777 ms
136,392 KB
testcase_08 AC 907 ms
123,476 KB
testcase_09 AC 966 ms
138,356 KB
testcase_10 AC 1,063 ms
132,900 KB
testcase_11 AC 657 ms
82,632 KB
testcase_12 AC 629 ms
79,408 KB
testcase_13 AC 643 ms
79,180 KB
testcase_14 AC 1,149 ms
121,640 KB
testcase_15 AC 1,196 ms
140,048 KB
testcase_16 AC 1,224 ms
135,364 KB
testcase_17 AC 51 ms
37,188 KB
testcase_18 AC 52 ms
37,144 KB
testcase_19 AC 50 ms
37,140 KB
testcase_20 AC 840 ms
81,148 KB
testcase_21 AC 808 ms
82,416 KB
testcase_22 AC 967 ms
84,452 KB
testcase_23 AC 967 ms
83,076 KB
testcase_24 AC 1,039 ms
82,800 KB
testcase_25 AC 917 ms
82,712 KB
testcase_26 AC 916 ms
83,144 KB
testcase_27 AC 890 ms
82,404 KB
testcase_28 AC 935 ms
83,132 KB
testcase_29 AC 918 ms
84,168 KB
testcase_30 AC 930 ms
83,056 KB
testcase_31 AC 824 ms
82,980 KB
testcase_32 AC 898 ms
83,448 KB
testcase_33 AC 837 ms
83,032 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