// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
template<class T, size_t SZ> using AR = array<T, SZ>;
struct DSU {
V<int> e, ans; V<AR<int, 2>> last; V<AR<int, 5>> his;
void init(int N) { e = V<int>(N, -1); ans = V<int>(N, 1); last = V<AR<int, 2>>(N, {0, 0}); his = {}; }
int get(int x) { return e[x] < 0 ? x : get(e[x]); }
int info(int x) { return ans[get(x)]; }
void upd(int x, int y, int i) {
// cout << x << " " << ans[x] << " " << y << " " << ans[y] << endl;
int dx = ans[y] - last[i][0];
int dy = ans[x] - last[i][1];
ans[x] += dx, ans[y] += dy;
last[i] = {ans[x], ans[y]};
}
void unite(int x, int y, int i) {
// cout << x << endl;
x = get(x), y = get(y);
// cout << x << " " << ans[x] << " " << y << " " << ans[y] << endl;
upd(x, y, i);
// if (x == y) { his.pb({-1, -1, -1, -1, i}); return 0; }
if (e[x] > e[y]) swap(x, y);
his.pb({x, e[x], y, e[y], i});
e[x] += e[y]; e[y] = x;
}
int rollback() {
if (sz(his) == 0) return -1;
auto [x, ex, y, ey, i] = his.back(); his.pop_back();
e[x] = ex, e[y] = ey;
upd(x, y, i);
return i;
}
};
const int BLK = 200;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M, Q; cin >> N >> M >> Q;
V<V<int>> adj(N);
V<AR<int, 2>> E(N-1);
for(int e = 0; e < N-1; e++) {
int u, v; cin >> u >> v; --u, --v;
E[e] = {u, v};
}
DSU D; D.init(N);
V<V<AR<int, 2>>> EV(N);
V<int> T(M);
for(int i = 0; i < M; i++) {
int x; cin >> x; --x;
T[i] = x;
// cout << x << " " << sz(EV[x]) << endl;
EV[x].pb({i, sz(EV[x]) % 2});
}
for(int i = 0; i < N; i++) reverse(begin(EV[i]), end(EV[i]));
set<int> ex;
auto rebuild = [&]() {
V<int> ed;
while(1) {
int r = D.rollback();
if (r == -1) break;
else ed.pb(r);
}
sort(begin(ed), end(ed), [&](int x, int y) {
return EV[x].back() < EV[y].back();
});
reverse(begin(ed), end(ed));
for(auto& e : ed) D.unite(E[e][0], E[e][1], e);
};
auto rem = [&](int x) {
V<int> add;
while(1) {
int r = D.rollback();
if (r == x) break;
else add.pb(r);
}
reverse(begin(add), end(add));
for(auto& e : add) D.unite(E[e][0], E[e][1], e);
};
for(int i = 0; i < M; i++) {
int e = T[i], t = EV[T[i]].back()[1]; EV[T[i]].pop_back();
// cout << e << " " << t << endl;
if (t == 0) {
ex.insert(e);
D.unite(E[e][0], E[e][1], e);
} else {
rem(e);
if (ex.find(e) != ex.end()) ex.erase(e);
}
if (size(ex) >= BLK) {
rebuild();
ex = {};
}
}
rem(-1);
while(Q--) {
int u; cin >> u; --u;
cout << D.ans[u] << nl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
224 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
308 ms |
1884 KB |
Output is correct |
8 |
Correct |
303 ms |
1884 KB |
Output is correct |
9 |
Correct |
356 ms |
1808 KB |
Output is correct |
10 |
Execution timed out |
8063 ms |
15440 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3202 ms |
15200 KB |
Output is correct |
2 |
Correct |
3121 ms |
16920 KB |
Output is correct |
3 |
Correct |
2577 ms |
15736 KB |
Output is correct |
4 |
Correct |
2655 ms |
15612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
420 KB |
Output is correct |
7 |
Correct |
301 ms |
1932 KB |
Output is correct |
8 |
Execution timed out |
8071 ms |
15832 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
8055 ms |
13272 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
314 ms |
1912 KB |
Output is correct |
7 |
Execution timed out |
8064 ms |
15760 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |