結果

問題 No.899 γatheree
ユーザー xuzijian629xuzijian629
提出日時 2019-10-04 22:03:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,374 bytes
コンパイル時間 2,661 ms
コンパイル使用メモリ 209,724 KB
実行使用メモリ 44,308 KB
最終ジャッジ日時 2024-04-14 10:47:10
合計ジャッジ時間 12,273 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,884 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 319 ms
37,332 KB
testcase_07 AC 317 ms
37,300 KB
testcase_08 AC 321 ms
37,364 KB
testcase_09 AC 318 ms
37,184 KB
testcase_10 AC 324 ms
37,344 KB
testcase_11 AC 322 ms
37,340 KB
testcase_12 AC 323 ms
37,440 KB
testcase_13 AC 313 ms
37,336 KB
testcase_14 AC 326 ms
37,320 KB
testcase_15 AC 323 ms
37,296 KB
testcase_16 AC 315 ms
37,328 KB
testcase_17 AC 318 ms
37,356 KB
testcase_18 AC 315 ms
37,324 KB
testcase_19 AC 323 ms
37,296 KB
testcase_20 AC 316 ms
37,468 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>();
    };

    auto process = [&](int v) {
        for (int s : adj[v]) {
            set<int> tmp = ns[s];
            for (int ss : tmp) {
                if (ss != v) mv(ss, v);
            }
            if (as[s]) mv(s, v);
        }
    };

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