結果

問題 No.899 γatheree
ユーザー xuzijian629xuzijian629
提出日時 2019-10-04 23:22:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,880 bytes
コンパイル時間 2,541 ms
コンパイル使用メモリ 210,588 KB
実行使用メモリ 40,436 KB
最終ジャッジ日時 2024-04-14 11:50:20
合計ジャッジ時間 15,300 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,812 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 359 ms
39,848 KB
testcase_07 AC 363 ms
40,000 KB
testcase_08 AC 362 ms
39,744 KB
testcase_09 AC 362 ms
39,868 KB
testcase_10 AC 369 ms
39,840 KB
testcase_11 AC 371 ms
39,840 KB
testcase_12 AC 369 ms
39,740 KB
testcase_13 AC 361 ms
39,664 KB
testcase_14 AC 360 ms
39,688 KB
testcase_15 AC 361 ms
39,824 KB
testcase_16 AC 359 ms
39,756 KB
testcase_17 AC 364 ms
39,744 KB
testcase_18 AC 360 ms
39,868 KB
testcase_19 AC 364 ms
39,728 KB
testcase_20 AC 359 ms
39,828 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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::milliseconds>(now - start).count() > 1500 && q > 90000);
        cout << as[x] << '\n';
    }
}
0