結果

問題 No.899 γatheree
ユーザー xuzijian629xuzijian629
提出日時 2019-10-04 23:15:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,687 bytes
コンパイル時間 2,571 ms
コンパイル使用メモリ 209,456 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 11:41:03
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

    int n;
    cin >> n;

    if (n < 50000) {
        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) {
            if (adj[v].size() >= 100) {
            }
            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';
        }
    } else {
        return 1;
    }
}
0