結果

問題 No.1507 Road Blocked
ユーザー ks2mks2m
提出日時 2021-05-14 22:32:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 618 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 2,303 ms
コンパイル使用メモリ 78,056 KB
実行使用メモリ 85,480 KB
最終ジャッジ日時 2024-04-10 01:08:20
合計ジャッジ時間 19,455 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,256 KB
testcase_01 AC 53 ms
50,228 KB
testcase_02 AC 53 ms
49,788 KB
testcase_03 AC 395 ms
85,480 KB
testcase_04 AC 508 ms
72,272 KB
testcase_05 AC 518 ms
72,224 KB
testcase_06 AC 523 ms
72,140 KB
testcase_07 AC 508 ms
71,988 KB
testcase_08 AC 519 ms
72,084 KB
testcase_09 AC 509 ms
72,328 KB
testcase_10 AC 618 ms
72,164 KB
testcase_11 AC 550 ms
73,532 KB
testcase_12 AC 509 ms
72,200 KB
testcase_13 AC 534 ms
72,312 KB
testcase_14 AC 592 ms
74,008 KB
testcase_15 AC 511 ms
72,540 KB
testcase_16 AC 583 ms
74,224 KB
testcase_17 AC 517 ms
72,188 KB
testcase_18 AC 515 ms
74,264 KB
testcase_19 AC 507 ms
72,116 KB
testcase_20 AC 544 ms
73,620 KB
testcase_21 AC 519 ms
72,128 KB
testcase_22 AC 511 ms
72,188 KB
testcase_23 AC 499 ms
72,328 KB
testcase_24 AC 500 ms
72,288 KB
testcase_25 AC 510 ms
72,220 KB
testcase_26 AC 507 ms
72,108 KB
testcase_27 AC 500 ms
72,112 KB
testcase_28 AC 574 ms
74,320 KB
testcase_29 AC 516 ms
72,128 KB
testcase_30 AC 504 ms
72,372 KB
testcase_31 AC 507 ms
72,120 KB
testcase_32 AC 522 ms
72,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	static int n;
	static long ng;
	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]);
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			sa = br.readLine().split(" ");
			int a = Integer.parseInt(sa[0]) - 1;
			int b = Integer.parseInt(sa[1]) - 1;
			list.get(a).add(b);
			list.get(b).add(a);
		}
		br.close();

		int mod = 998244353;
		dfs(0, -1);
		long total = (long) n * (n - 1) / 2 * (n - 1);
		long ok = total - ng;
		total %= mod;
		ok %= mod;
		long mt = modinv(total, mod);
		long ans = ok * mt % mod;
		System.out.println(ans);
	}

	static int dfs(int x, int p) {
		int ret = 1;
		for (int i : list.get(x)) {
			if (i != p) {
				int res = dfs(i, x);
				ret += res;
			}
		}
		ng += (long) ret * (n - ret);
		return ret;
	}

	static long modinv(long a, int m) {
		long b = m;
		long u = 1;
		long v = 0;
		long tmp = 0;

		while (b > 0) {
			long t = a / b;
			a -= t * b;
			tmp = a;
			a = b;
			b = tmp;

			u -= t * v;
			tmp = u;
			u = v;
			v = tmp;
		}

		u %= m;
		if (u < 0) u += m;
		return u;
	}
}
0