結果

問題 No.899 γatheree
ユーザー xuzijian629xuzijian629
提出日時 2019-10-04 23:21:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,889 bytes
コンパイル時間 2,594 ms
コンパイル使用メモリ 211,764 KB
実行使用メモリ 39,928 KB
最終ジャッジ日時 2024-04-14 11:48:32
合計ジャッジ時間 12,426 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,892 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 353 ms
39,732 KB
testcase_07 AC 353 ms
39,680 KB
testcase_08 AC 356 ms
39,684 KB
testcase_09 AC 356 ms
39,792 KB
testcase_10 AC 354 ms
39,672 KB
testcase_11 AC 362 ms
39,688 KB
testcase_12 AC 362 ms
39,840 KB
testcase_13 AC 356 ms
39,688 KB
testcase_14 AC 362 ms
39,928 KB
testcase_15 AC 346 ms
39,688 KB
testcase_16 AC 344 ms
39,852 KB
testcase_17 AC 358 ms
39,744 KB
testcase_18 AC 361 ms
39,736 KB
testcase_19 AC 355 ms
39,776 KB
testcase_20 AC 355 ms
39,740 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>();
    };

    vector<vector<int>> adj2(n);

    auto process = [&](int v, bool zuru = false) {
        vector<int> use;
        if (zuru) {
            use = adj2[v];
        } else {
            use = adj[v];
        }
        vector<int> memo;
        for (int s : use) {
            set<int> tmp = ns[s];
            if (tmp.size()) {
                memo.push_back(s);
            }
            for (int ss : tmp) {
                if (ss != v) mv(ss, v);
            }
            if (as[s]) mv(s, v);
        }
        swap(adj2[v], memo);
    };

    auto start = chrono::steady_clock::now();
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        auto now = chrono::steady_clock::now();
        process(x, chrono::duration_cast<chrono::microseconds>(now - start).count() > 1500 && q > 90000 && false);
        cout << as[x] << '\n';
    }
}
0