結果
問題 | No.1637 Easy Tree Query |
ユーザー | tenten |
提出日時 | 2022-09-29 10:47:33 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 640 ms / 2,000 ms |
コード長 | 1,951 bytes |
コンパイル時間 | 3,588 ms |
コンパイル使用メモリ | 74,716 KB |
実行使用メモリ | 97,640 KB |
最終ジャッジ日時 | 2023-08-24 04:56:15 |
合計ジャッジ時間 | 19,605 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
49,488 KB |
testcase_01 | AC | 43 ms
49,168 KB |
testcase_02 | AC | 640 ms
82,404 KB |
testcase_03 | AC | 43 ms
49,000 KB |
testcase_04 | AC | 298 ms
62,792 KB |
testcase_05 | AC | 408 ms
68,200 KB |
testcase_06 | AC | 359 ms
62,220 KB |
testcase_07 | AC | 217 ms
55,244 KB |
testcase_08 | AC | 257 ms
62,448 KB |
testcase_09 | AC | 453 ms
71,008 KB |
testcase_10 | AC | 288 ms
61,208 KB |
testcase_11 | AC | 526 ms
73,600 KB |
testcase_12 | AC | 485 ms
71,044 KB |
testcase_13 | AC | 216 ms
59,936 KB |
testcase_14 | AC | 402 ms
67,816 KB |
testcase_15 | AC | 529 ms
73,672 KB |
testcase_16 | AC | 524 ms
73,672 KB |
testcase_17 | AC | 249 ms
60,816 KB |
testcase_18 | AC | 383 ms
66,452 KB |
testcase_19 | AC | 404 ms
65,248 KB |
testcase_20 | AC | 493 ms
71,572 KB |
testcase_21 | AC | 352 ms
64,708 KB |
testcase_22 | AC | 589 ms
76,912 KB |
testcase_23 | AC | 254 ms
60,688 KB |
testcase_24 | AC | 337 ms
64,956 KB |
testcase_25 | AC | 404 ms
65,292 KB |
testcase_26 | AC | 489 ms
74,368 KB |
testcase_27 | AC | 619 ms
78,968 KB |
testcase_28 | AC | 352 ms
62,864 KB |
testcase_29 | AC | 439 ms
66,932 KB |
testcase_30 | AC | 328 ms
64,904 KB |
testcase_31 | AC | 315 ms
62,820 KB |
testcase_32 | AC | 305 ms
62,680 KB |
testcase_33 | AC | 379 ms
64,768 KB |
testcase_34 | AC | 470 ms
97,640 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); static int[] counts; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int q = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; graph.get(a).add(b); graph.get(b).add(a); } counts = new int[n]; getCount(0, 0); long ans = 0; StringBuilder sb = new StringBuilder(); while (q-- > 0) { int idx = sc.nextInt() - 1; long x = sc.nextInt(); ans += counts[idx] * x; sb.append(ans).append("\n"); } System.out.print(sb); } static int getCount(int idx, int p) { counts[idx] = 1; for (int x : graph.get(idx)) { if (x == p) { continue; } counts[idx] += getCount(x, idx); } return counts[idx]; } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }