Submission #1175458

#TimeUsernameProblemLanguageResultExecution timeMemory
1175458_callmelucianSynchronization (JOI13_synchronization)C++17
100 / 100
101 ms18504 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pl = pair<ll,ll>;
using pii = pair<int,int>;
using tpl = tuple<int,int,int>;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

struct BIT {
    vector<int> tr;
    BIT (int sz) : tr(sz + 1) {}

    int p (int k) { return k & -k; }

    void upd (int k, int val) {
        for (; k < tr.size(); k += p(k)) tr[k] += val;
    }

    void update (int l, int r, int incr) {
        upd(l, incr), upd(r + 1, -incr);
    }

    int query (int k) {
        int ans = 0;
        for (; k; k -= p(k)) ans += tr[k];
        return ans;
    }
};

const int mn = 1e5 + 5;
int tail[mn], chain[mn], depth[mn], num[mn], sz[mn], par[mn];
int curNode[mn], toParent[mn], timeDfs;
vector<int> adj[mn];
bool isUsing[mn];
pii edge[mn];
BIT tree(mn);

int szDfs (int u, int p) {
    sz[u] = 1;
    for (int v : adj[u])
        if (v != p) sz[u] += szDfs(v, u);
    return sz[u];
}

void dfs (int u, int p, int d, bool toP) {
    if (u == 1) szDfs(u, p);
    chain[u] = (toP ? chain[p] : u), par[u] = p;
    num[u] = tail[u] = ++timeDfs, depth[u] = d;
    sort(all(adj[u]), [&] (int a, int b) { return sz[a] > sz[b]; });

    bool heavy = 1;
    for (int v : adj[u])
        if (v != p) dfs(v, u, d + 1, heavy), heavy = 0;
}

int getRoot (int a) {
    int nxt = tree.query(num[a]);
    while (a != nxt)
        a = nxt, nxt = tree.query(num[a]);
    return a;
}

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

    /// read input and perform pre-calculations
    int n, m, q; cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        edge[i] = {a, b};
    }
    dfs(1, 0, 1, 0);

    for (int i = 1; i < n; i++)
        if (depth[edge[i].first] > depth[edge[i].second])
            swap(edge[i].first, edge[i].second);

    /// initialize node values
    for (int i = 1; i <= n; i++) {
        tree.update(num[i], num[i], i);
        curNode[i] = toParent[i] = 1;
    }

    /// process actions
    while (m--) {
        int k; cin >> k;
        int a, b; tie(a, b) = edge[k];
        int root = getRoot(a);

        if (isUsing[k]) { // erase edge
            curNode[b] = curNode[root], toParent[b] = 0;
            if (chain[a] != chain[b])
                tree.update(num[b], tail[b], -a + b); // b is the new root
            else {
                if (chain[a] == chain[root]) {
                    tail[b] = tail[root], tail[root] = num[a];
                    tree.update(num[b], tail[b], -root + b);
                }
                else {
                    tail[b] = tail[chain[a]], tail[chain[a]] = num[a];
                    tree.update(num[b], tail[b], -par[chain[a]] + b);
                }
            }
            isUsing[k] = 0;
        }

        else { // add edge
            curNode[root] += toParent[b], toParent[root] += toParent[b];
            curNode[b] = toParent[b] = 0;
            if (chain[a] != chain[b])
                tree.update(num[b], tail[b], -b + a);
            else {
                if (chain[a] == chain[root]) {
                    tail[root] = tail[b];
                    tree.update(num[b], tail[b], -b + root);
                }
                else {
                    tail[chain[a]] = tail[b];
                    tree.update(num[b], tail[b], -b + par[chain[a]]);
                }
            }
            isUsing[k] = 1;
        }
    }

    /// process actions
    while  (q--) {
        int u; cin >> u;
        cout << curNode[getRoot(u)] << "\n";
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...