This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
int N, M, Q;
int timer = 1;
int anc[100005][20];
int tin[100005], tout[100005];
vector<int> g[100005];
int bit[100005], info[100005], last_sync[100005];
bool active[100005];
pair<int,int> edges[2*100005];
void dfs(int node = 1, int p = 0) {
anc[node][0] = p;
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] = timer++;
for (int u : g[node]){
if (u != p)
dfs(u, node);
}
tout[node] = timer;
}
void add(int i, int v) {
for (; i <= N; i += (i & (-i))) bit[i] += v;
}
int get(int i) {
int r = 0;
for (; i; i -= (i & (-i))) r += bit[i];
return r;
}
int find_anc(int node) {
int lca = node;
for (int i = 19; ~i; i--) {
if (anc[lca][i] && get(tin[anc[lca][i]]) == get(tin[node]))
lca = anc[lca][i];
}
return lca;
}
void solve(){
cin >> N >> M >> Q;
for (int i = 0; i < N; i++) {
int v, u; cin >> v >> u;
edges[i].first = v;
edges[i].second = u;
g[v].push_back(u);
g[u].push_back(v);
}
dfs();
for (int i = 1; i < N+1; i++) {
add(tin[i], -1);
add(tout[i], 1);
}
for (int i = 0; i < M; i++) {
int line; cin >> line;
int u = edges[line].first, v = edges[line].second;
if (anc[u][0] == v) swap(u,v);
if (active[line]) {
info[v] = last_sync[v] = info[find_anc(u)];
add(tin[v], -1);
add(tout[v], 1);
} else {
info[find_anc(u)] += info[v] - last_sync[v];
add(tin[v], 1);
add(tout[v], -1);
}
active[line] = !active[line];
}
for (int i = 0; i < Q; i++) {
int node; cin >> node;
cout << info[find_anc(node)] << '\n';
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t = 1;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |