Submission #171419

#TimeUsernameProblemLanguageResultExecution timeMemory
171419dolphingarlicSynchronization (JOI13_synchronization)C++14
100 / 100
529 ms21496 KiB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

int n, m, q;
vector<int> graph[100001];
pair<int, int> e[200001];

int info[100001], last[100001], t = 1, tin[100001], tout[100001], anc[100001][20];

void dfs(int node = 1, int parent = 0) {
    anc[node][0] = parent;
    for (int i = 1; i < 20 && anc[node][i - 1]; i++) anc[node][i] = anc[anc[node][i - 1]][i - 1];
    info[node] = 1;
    tin[node] = t++;
    for (int i : graph[node]) if (i != parent) dfs(i, node);
    tout[node] = t;
}

int bit[100001];
void update(int pos, int val) { for (; pos <= n; pos += (pos & (-pos))) bit[pos] += val; }
int query(int pos) {
    int ans = 0;
    for (; pos; pos -= (pos & (-pos))) ans += bit[pos];
    return ans;
}

int lca(int node) {
    int lca = node;
    for (int i = 19; ~i; i--) if (anc[lca][i] && query(tin[anc[lca][i]]) == query(tin[node])) lca = anc[lca][i];
    return lca;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m >> q;
    FOR(i, 1, n) {
        cin >> e[i].first >> e[i].second;
        graph[e[i].first].push_back(e[i].second);
        graph[e[i].second].push_back(e[i].first);
    }
    dfs();

    FOR(i, 1, n + 1) {
        update(tin[i], -1);
        update(tout[i], 1);
    }

    while (m--) {
        int x;
        cin >> x;
        int u = e[x].first, v = e[x].second;
        if (anc[u][0] == v) swap(u, v);

        if (lca(v) != v) {
            info[v] = last[v] = info[lca(u)];
            update(tin[v], -1);
            update(tout[v], 1);
        } else {
            info[lca(u)] += info[v] - last[v];
            update(tin[v], 1);
            update(tout[v], -1);
        }
    }

    while (q--) {
        int x;
        cin >> x;
        cout << info[lca(x)] << '\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...