結果

問題 No.1637 Easy Tree Query
ユーザー kokatsu
提出日時 2021-10-20 00:28:31
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 712 bytes
コンパイル時間 2,634 ms
コンパイル使用メモリ 207,156 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-06-22 12:50:16
合計ジャッジ時間 9,318 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 WA * 31
権限があれば一括ダウンロードができます

ソースコード

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 int[](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) {
        int p, x;
        readf("%d %d\n", p, x);

        S += part[p] * x;

        S.writeln;
    }
}
0