#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())
const int mn = 1e5 + 5;
int chain[mn], sz[mn], par[mn], num[mn], tail[mn], sync[mn], passed[mn], timeDfs;
bool stop[mn], toParent[mn], state[2 * mn];
vector<int> adj[mn];
pii edge[mn];
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 val) { upd(l, val), upd(r + 1, -val); }
int get (int k, int ans = 0) {
for (; k; k -= p(k)) ans += tr[k];
return ans;
}
} link(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);
num[u] = ++timeDfs, par[u] = p;
chain[u] = (toP ? chain[p] : u);
if (adj[u].empty()) return;
int heavy = *max_element(all(adj[u]), [&] (int a, int b) {
return (a == p ? INT_MIN : sz[a]) < (b == p ? INT_MIN : sz[b]);
});
if (heavy != p) dfs(heavy, u, d + 1, 1);
for (int v : adj[u])
if (v != p && v != heavy) dfs(v, u, d + 1, 0);
}
int getRoot (int u) {
while (!stop[u] || toParent[u]) {
if (toParent[u]) u = par[u];
else u = link.get(num[u]);
}
return u;
}
void disconnect (int u, int v) {
if (chain[u] != chain[v]) toParent[v] = 0;
else {
int t = link.get(num[u]);
link.update(num[v], tail[t], v - t);
tail[v] = tail[t], tail[t] = num[u], stop[v] = 1;
}
}
void connect (int u, int v) {
if (chain[u] != chain[v]) toParent[v] = 1;
else {
int t = link.get(num[u]);
link.update(num[v], tail[v], t - v);
tail[t] = tail[v], stop[v] = 0;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
/// read input and run dfs
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);
/// pre-process
for (int i = 1; i < n; i++) {
int a, b; tie(a, b) = edge[i];
if (num[a] > num[b]) edge[i] = {b, a};
}
for (int i = 1; i <= n; i++) {
sync[i] = stop[i] = 1, tail[i] = num[i];
link.update(num[i], num[i], i);
}
/// process updates & synchronizations
for (int i = 0; i < m; i++) {
int d; cin >> d;
int a, b; tie(a, b) = edge[d];
if (state[d]) {
sync[b] = passed[b] = sync[getRoot(a)];
disconnect(a, b), state[d] = 0;
}
else {
sync[getRoot(a)] += sync[b] - passed[b];
connect(a, b), state[d] = 1;
}
}
/// answer queries
for (int i = 0; i < q; i++) {
int u; cin >> u;
cout << sync[getRoot(u)] << " ";
}
return 0;
}
Compilation message (stderr)
synchronization.cpp:14:59: error: 'int sync [100005]' redeclared as different kind of entity
14 | int chain[mn], sz[mn], par[mn], num[mn], tail[mn], sync[mn], passed[mn], timeDfs;
| ^
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
from /usr/include/signal.h:328,
from /usr/include/c++/11/csignal:42,
from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:43,
from synchronization.cpp:1:
/usr/include/unistd.h:1005:13: note: previous declaration 'void sync()'
1005 | extern void sync (void) __THROW;
| ^~~~
synchronization.cpp:35:10: error: 'BIT link' redeclared as different kind of entity
35 | } link(mn);
| ^
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
from /usr/include/signal.h:328,
from /usr/include/c++/11/csignal:42,
from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:43,
from synchronization.cpp:1:
/usr/include/unistd.h:819:12: note: previous declaration 'int link(const char*, const char*)'
819 | extern int link (const char *__from, const char *__to)
| ^~~~
synchronization.cpp: In function 'int getRoot(int)':
synchronization.cpp:61:23: error: request for member 'get' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
61 | else u = link.get(num[u]);
| ^~~
synchronization.cpp: In function 'void disconnect(int, int)':
synchronization.cpp:70:22: error: request for member 'get' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
70 | int t = link.get(num[u]);
| ^~~
synchronization.cpp:71:14: error: request for member 'update' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
71 | link.update(num[v], tail[t], v - t);
| ^~~~~~
synchronization.cpp: In function 'void connect(int, int)':
synchronization.cpp:79:22: error: request for member 'get' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
79 | int t = link.get(num[u]);
| ^~~
synchronization.cpp:80:14: error: request for member 'update' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
80 | link.update(num[v], tail[v], t - v);
| ^~~~~~
synchronization.cpp: In function 'int main()':
synchronization.cpp:106:15: warning: pointer to a function used in arithmetic [-Wpointer-arith]
106 | sync[i] = stop[i] = 1, tail[i] = num[i];
| ^
synchronization.cpp:106:17: error: assignment of read-only location '*(sync + ((sizetype)i))'
106 | sync[i] = stop[i] = 1, tail[i] = num[i];
| ~~~~~~~~^~~~~~~~~~~~~
synchronization.cpp:107:14: error: request for member 'update' in 'link', which is of non-class type 'int(const char*, const char*) noexcept'
107 | link.update(num[i], num[i], i);
| ^~~~~~
synchronization.cpp:115:19: warning: pointer to a function used in arithmetic [-Wpointer-arith]
115 | sync[b] = passed[b] = sync[getRoot(a)];
| ^
synchronization.cpp:115:50: warning: pointer to a function used in arithmetic [-Wpointer-arith]
115 | sync[b] = passed[b] = sync[getRoot(a)];
| ^
synchronization.cpp:115:50: error: invalid conversion from 'void (*)() noexcept' to 'int' [-fpermissive]
115 | sync[b] = passed[b] = sync[getRoot(a)];
| ~~~~~~~~~~~~~~~^
| |
| void (*)() noexcept
synchronization.cpp:115:21: error: assignment of read-only location '*(sync + ((sizetype)b))'
115 | sync[b] = passed[b] = sync[getRoot(a)];
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:119:28: warning: pointer to a function used in arithmetic [-Wpointer-arith]
119 | sync[getRoot(a)] += sync[b] - passed[b];
| ^
synchronization.cpp:119:39: warning: pointer to a function used in arithmetic [-Wpointer-arith]
119 | sync[getRoot(a)] += sync[b] - passed[b];
| ^
synchronization.cpp:119:41: warning: pointer to a function used in arithmetic [-Wpointer-arith]
119 | sync[getRoot(a)] += sync[b] - passed[b];
| ~~~~~~~~^~~~~~~~~~~
synchronization.cpp:119:30: error: invalid operands of types 'void() noexcept' and 'void (*)() noexcept' to binary 'operator+'
119 | sync[getRoot(a)] += sync[b] - passed[b];
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:119:30: note: in evaluation of 'operator+=(void() noexcept, void (*)() noexcept)'
synchronization.cpp:127:32: warning: pointer to a function used in arithmetic [-Wpointer-arith]
127 | cout << sync[getRoot(u)] << " ";
| ^