結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,764 KB
testcase_01 AC 2 ms
6,944 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 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
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);
        cout << as[x] << '\n';
    }
}
0