結果

問題 No.1637 Easy Tree Query
ユーザー tentententen
提出日時 2022-08-05 16:50:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 543 ms / 2,000 ms
コード長 1,918 bytes
コンパイル時間 1,876 ms
コンパイル使用メモリ 74,716 KB
実行使用メモリ 97,968 KB
最終ジャッジ日時 2023-10-13 16:26:04
合計ジャッジ時間 18,467 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
49,476 KB
testcase_01 AC 39 ms
49,808 KB
testcase_02 AC 543 ms
82,348 KB
testcase_03 AC 44 ms
49,384 KB
testcase_04 AC 249 ms
62,252 KB
testcase_05 AC 340 ms
68,420 KB
testcase_06 AC 268 ms
60,584 KB
testcase_07 AC 194 ms
55,812 KB
testcase_08 AC 222 ms
62,104 KB
testcase_09 AC 422 ms
70,468 KB
testcase_10 AC 243 ms
60,884 KB
testcase_11 AC 430 ms
73,884 KB
testcase_12 AC 391 ms
71,268 KB
testcase_13 AC 188 ms
58,360 KB
testcase_14 AC 340 ms
68,112 KB
testcase_15 AC 471 ms
76,228 KB
testcase_16 AC 419 ms
74,124 KB
testcase_17 AC 212 ms
59,808 KB
testcase_18 AC 345 ms
68,664 KB
testcase_19 AC 336 ms
64,996 KB
testcase_20 AC 394 ms
71,692 KB
testcase_21 AC 319 ms
65,364 KB
testcase_22 AC 450 ms
76,340 KB
testcase_23 AC 224 ms
59,644 KB
testcase_24 AC 277 ms
65,108 KB
testcase_25 AC 357 ms
65,380 KB
testcase_26 AC 411 ms
74,280 KB
testcase_27 AC 476 ms
78,284 KB
testcase_28 AC 302 ms
62,856 KB
testcase_29 AC 348 ms
67,072 KB
testcase_30 AC 261 ms
65,068 KB
testcase_31 AC 270 ms
62,368 KB
testcase_32 AC 254 ms
62,344 KB
testcase_33 AC 326 ms
65,332 KB
testcase_34 AC 401 ms
97,968 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);
        StringBuilder sb = new StringBuilder();
        long current = 0;
        while (q-- > 0) {
            int idx = sc.nextInt() - 1;
            long value = sc.nextInt();
            current += value * counts[idx];
            sb.append(current).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("");
    
    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();
    }
}
0