結果

問題 No.1507 Road Blocked
ユーザー ks2mks2m
提出日時 2021-05-14 22:32:59
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 498 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 1,614 ms
使用メモリ 83,420 KB
最終ジャッジ日時 2022-11-25 19:37:12
合計ジャッジ時間 17,320 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 51 ms
47,780 KB
testcase_01 AC 52 ms
49,780 KB
testcase_02 AC 52 ms
47,596 KB
testcase_03 AC 404 ms
83,420 KB
testcase_04 AC 431 ms
66,380 KB
testcase_05 AC 473 ms
66,264 KB
testcase_06 AC 437 ms
66,684 KB
testcase_07 AC 439 ms
67,428 KB
testcase_08 AC 480 ms
68,264 KB
testcase_09 AC 457 ms
68,048 KB
testcase_10 AC 450 ms
68,192 KB
testcase_11 AC 471 ms
67,180 KB
testcase_12 AC 476 ms
69,972 KB
testcase_13 AC 447 ms
68,220 KB
testcase_14 AC 451 ms
69,124 KB
testcase_15 AC 483 ms
70,292 KB
testcase_16 AC 480 ms
72,100 KB
testcase_17 AC 462 ms
68,640 KB
testcase_18 AC 461 ms
67,764 KB
testcase_19 AC 486 ms
68,196 KB
testcase_20 AC 445 ms
68,588 KB
testcase_21 AC 437 ms
68,536 KB
testcase_22 AC 459 ms
70,008 KB
testcase_23 AC 458 ms
68,048 KB
testcase_24 AC 402 ms
67,648 KB
testcase_25 AC 419 ms
67,312 KB
testcase_26 AC 497 ms
70,172 KB
testcase_27 AC 449 ms
68,028 KB
testcase_28 AC 471 ms
67,448 KB
testcase_29 AC 404 ms
67,228 KB
testcase_30 AC 479 ms
67,888 KB
testcase_31 AC 439 ms
67,592 KB
testcase_32 AC 498 ms
68,248 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