結果

問題 No.899 γatheree
ユーザー xuzijian629xuzijian629
提出日時 2019-10-04 21:56:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,454 bytes
コンパイル時間 4,154 ms
コンパイル使用メモリ 208,292 KB
実行使用メモリ 44,460 KB
最終ジャッジ日時 2024-04-14 10:38:43
合計ジャッジ時間 13,727 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 407 ms
37,320 KB
testcase_07 AC 405 ms
37,388 KB
testcase_08 AC 403 ms
37,188 KB
testcase_09 AC 402 ms
37,416 KB
testcase_10 AC 402 ms
37,188 KB
testcase_11 AC 407 ms
37,248 KB
testcase_12 AC 398 ms
37,360 KB
testcase_13 AC 401 ms
37,500 KB
testcase_14 AC 412 ms
37,316 KB
testcase_15 AC 383 ms
37,360 KB
testcase_16 AC 400 ms
37,304 KB
testcase_17 AC 428 ms
37,396 KB
testcase_18 AC 410 ms
37,372 KB
testcase_19 AC 411 ms
37,292 KB
testcase_20 AC 409 ms
37,300 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);
        }
        nsinv[v] = set<int>();
    };

    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