結果

問題 No.899 γatheree
ユーザー xuzijian629xuzijian629
提出日時 2019-10-04 21:53:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,423 bytes
コンパイル時間 4,338 ms
コンパイル使用メモリ 209,684 KB
実行使用メモリ 44,452 KB
最終ジャッジ日時 2024-04-14 10:34:24
合計ジャッジ時間 17,611 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,888 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 674 ms
37,184 KB
testcase_07 AC 658 ms
37,328 KB
testcase_08 AC 678 ms
37,332 KB
testcase_09 AC 666 ms
37,396 KB
testcase_10 AC 680 ms
37,184 KB
testcase_11 AC 676 ms
37,332 KB
testcase_12 AC 674 ms
37,304 KB
testcase_13 AC 667 ms
37,328 KB
testcase_14 AC 671 ms
37,400 KB
testcase_15 AC 662 ms
37,464 KB
testcase_16 AC 659 ms
37,184 KB
testcase_17 AC 668 ms
37,332 KB
testcase_18 AC 679 ms
37,388 KB
testcase_19 AC 675 ms
37,384 KB
testcase_20 AC 658 ms
37,328 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    vector<vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    vector<int> as(n);
    for (int i = 0; i < n; i++) cin >> as[i];

    vector<set<int>> ns(n), nsinv(n);
    for (int i = 0; i < n; i++) {
        for (int a : adj[i]) {
            if (as[a]) {
                ns[i].insert(a);
                nsinv[a].insert(i);
            }
        }
    }

    auto mv = [&](int v, int to) {
        // vには必ず石があるとしてよい
        if (!as[to]) {
            for (int a : adj[to]) {
                ns[a].insert(to);
                nsinv[to].insert(a);
            }
        }
        as[to] += as[v];
        as[v] = 0;
        for (int s : nsinv[v]) {
            ns[s].erase(v);
        }
    };

    function<void(int, int, int, int)> process = [&](int v, int p, int d, int x) {
        if (d == 1 || d == 2) {
            mv(v, x);
        }
        if (d == 2) return;
        for (int s : adj[v]) {
            if (s != p) {
                process(s, v, d + 1, x);
            }
        }
    };

    int q;
    cin >> q;
    while (q--) {
        int x;
        cin >> x;
        process(x, -1, 0, x);
        cout << as[x] << '\n';
    }
}
0