結果

問題 No.1637 Easy Tree Query
ユーザー kokatsukokatsu
提出日時 2021-10-20 00:30:04
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 714 bytes
コンパイル時間 2,547 ms
コンパイル使用メモリ 202,948 KB
実行使用メモリ 15,672 KB
最終ジャッジ日時 2023-09-04 14:31:07
合計ジャッジ時間 8,308 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 170 ms
10,292 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 62 ms
7,396 KB
testcase_05 AC 122 ms
5,092 KB
testcase_06 AC 81 ms
4,616 KB
testcase_07 AC 36 ms
4,376 KB
testcase_08 AC 48 ms
5,156 KB
testcase_09 AC 127 ms
7,504 KB
testcase_10 AC 58 ms
4,376 KB
testcase_11 AC 164 ms
8,252 KB
testcase_12 AC 149 ms
8,236 KB
testcase_13 AC 31 ms
4,592 KB
testcase_14 AC 101 ms
6,984 KB
testcase_15 AC 157 ms
7,296 KB
testcase_16 AC 142 ms
7,728 KB
testcase_17 AC 43 ms
4,616 KB
testcase_18 AC 115 ms
5,208 KB
testcase_19 AC 118 ms
5,400 KB
testcase_20 AC 153 ms
7,156 KB
testcase_21 AC 90 ms
6,980 KB
testcase_22 AC 185 ms
8,220 KB
testcase_23 AC 46 ms
4,596 KB
testcase_24 AC 79 ms
6,916 KB
testcase_25 AC 123 ms
4,944 KB
testcase_26 AC 160 ms
6,516 KB
testcase_27 AC 192 ms
8,844 KB
testcase_28 AC 91 ms
4,928 KB
testcase_29 AC 126 ms
6,200 KB
testcase_30 AC 65 ms
6,224 KB
testcase_31 AC 80 ms
4,380 KB
testcase_32 AC 70 ms
5,176 KB
testcase_33 AC 108 ms
4,380 KB
testcase_34 AC 88 ms
15,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main() {
    int N, Q;
    readf("%d %d\n", N, Q);

    auto tree = new int[][](N+1);
    foreach (i; 0 .. N-1) {
        int a, b;
        readf("%d %d\n", a, b);

        tree[a] ~= b;
        tree[b] ~= a;
    }

    auto seen = new bool[](N+1);
    auto part = new long[](N+1);
    void dfs(int root) {
        seen[root] = true;
        ++part[root];
        
        foreach (t; tree[root]) {
            if (seen[t]) {
                continue;
            }

            dfs(t);

            part[root] += part[t];
        }
    }

    dfs(1);

    long S;
    foreach (i; 0 .. Q) {
        long p, x;
        readf("%d %d\n", p, x);

        S += part[p] * x;

        S.writeln;
    }
}
0