結果

問題 No.1637 Easy Tree Query
ユーザー tentententen
提出日時 2021-08-10 08:28:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 696 ms / 2,000 ms
コード長 1,763 bytes
コンパイル時間 3,151 ms
コンパイル使用メモリ 79,296 KB
実行使用メモリ 101,128 KB
最終ジャッジ日時 2023-10-22 08:18:13
合計ジャッジ時間 23,801 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,528 KB
testcase_01 AC 54 ms
53,528 KB
testcase_02 AC 614 ms
85,580 KB
testcase_03 AC 54 ms
53,536 KB
testcase_04 AC 346 ms
66,156 KB
testcase_05 AC 491 ms
70,280 KB
testcase_06 AC 389 ms
66,428 KB
testcase_07 AC 254 ms
58,700 KB
testcase_08 AC 308 ms
65,480 KB
testcase_09 AC 537 ms
72,860 KB
testcase_10 AC 309 ms
62,224 KB
testcase_11 AC 648 ms
81,428 KB
testcase_12 AC 535 ms
71,716 KB
testcase_13 AC 231 ms
61,140 KB
testcase_14 AC 468 ms
68,064 KB
testcase_15 AC 622 ms
77,100 KB
testcase_16 AC 583 ms
76,804 KB
testcase_17 AC 286 ms
61,284 KB
testcase_18 AC 426 ms
67,092 KB
testcase_19 AC 470 ms
69,516 KB
testcase_20 AC 535 ms
72,148 KB
testcase_21 AC 421 ms
69,316 KB
testcase_22 AC 696 ms
81,664 KB
testcase_23 AC 289 ms
61,772 KB
testcase_24 AC 398 ms
69,360 KB
testcase_25 AC 507 ms
69,876 KB
testcase_26 AC 564 ms
75,552 KB
testcase_27 AC 677 ms
80,448 KB
testcase_28 AC 401 ms
66,100 KB
testcase_29 AC 514 ms
69,736 KB
testcase_30 AC 356 ms
65,820 KB
testcase_31 AC 351 ms
65,812 KB
testcase_32 AC 351 ms
63,896 KB
testcase_33 AC 400 ms
66,368 KB
testcase_34 AC 524 ms
101,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
        for (int i = 0; i < q; i++) {
            ans += counts[sc.nextInt() - 1]  * sc.nextLong();
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getCount(int idx, int parent) {
        counts[idx] = 1;
        for (int x : graph.get(idx)) {
            if (x == parent) {
                continue;
            }
            counts[idx] += getCount(x, idx);
        }
        return counts[idx];
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0