結果

問題 No.1637 Easy Tree Query
ユーザー ks2mks2m
提出日時 2021-08-06 21:25:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 710 ms / 2,000 ms
コード長 1,326 bytes
コンパイル時間 4,422 ms
コンパイル使用メモリ 77,888 KB
実行使用メモリ 89,156 KB
最終ジャッジ日時 2023-10-17 04:47:50
合計ジャッジ時間 23,029 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
53,588 KB
testcase_01 AC 57 ms
53,596 KB
testcase_02 AC 644 ms
77,812 KB
testcase_03 AC 53 ms
53,580 KB
testcase_04 AC 340 ms
65,500 KB
testcase_05 AC 520 ms
66,084 KB
testcase_06 AC 391 ms
63,948 KB
testcase_07 AC 274 ms
60,588 KB
testcase_08 AC 320 ms
65,196 KB
testcase_09 AC 599 ms
71,824 KB
testcase_10 AC 345 ms
63,440 KB
testcase_11 AC 706 ms
76,868 KB
testcase_12 AC 626 ms
69,036 KB
testcase_13 AC 266 ms
62,624 KB
testcase_14 AC 520 ms
69,748 KB
testcase_15 AC 677 ms
75,420 KB
testcase_16 AC 656 ms
76,188 KB
testcase_17 AC 300 ms
62,896 KB
testcase_18 AC 497 ms
66,192 KB
testcase_19 AC 526 ms
66,112 KB
testcase_20 AC 626 ms
68,784 KB
testcase_21 AC 428 ms
67,632 KB
testcase_22 AC 700 ms
75,856 KB
testcase_23 AC 309 ms
63,332 KB
testcase_24 AC 394 ms
67,508 KB
testcase_25 AC 510 ms
66,244 KB
testcase_26 AC 653 ms
69,944 KB
testcase_27 AC 710 ms
75,692 KB
testcase_28 AC 433 ms
65,632 KB
testcase_29 AC 569 ms
68,408 KB
testcase_30 AC 377 ms
67,504 KB
testcase_31 AC 394 ms
60,848 KB
testcase_32 AC 371 ms
65,484 KB
testcase_33 AC 473 ms
64,120 KB
testcase_34 AC 430 ms
89,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	static List<List<Integer>> list;
	static int[] dp;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int q = 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 - 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);
		}
		int[] p = new int[q];
		int[] x = new int[q];
		for (int i = 0; i < q; i++) {
			sa = br.readLine().split(" ");
			p[i] = Integer.parseInt(sa[0]) - 1;
			x[i] = Integer.parseInt(sa[1]);
		}
		br.close();

		dp = new int[n];
		dfs(0, -1);
		long ans = 0;
		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < q; i++) {
			ans += (long) dp[p[i]] * x[i];
			pw.println(ans);
		}
		pw.flush();
	}

	static int dfs(int x, int p) {
		int ret = 1;
		for (int c : list.get(x)) {
			if (c != p) {
				ret += dfs(c, x);
			}
		}
		return dp[x] = ret;
	}
}
0