#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
struct Data {
int dp[2][2];
Data() {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) dp[i][j] = INF;
}
}
Data(int sum1, int sum2, int type) {
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
dp[i][j] = INF;
}
}
if (type != 2) dp[0][0] = sum1;
if (type != 1) dp[1][1] = sum2;
}
};
Data merge(const Data& l, const Data& r) {
Data res = Data{};
for (int pl = 0; pl < 2; ++pl) {
for (int ql = 0; ql < 2; ++ql) {
if (l.dp[pl][ql] == INF) continue;
for (int pr = 0; pr < 2; ++pr) {
for (int qr = 0; qr < 2; ++qr) {
if (r.dp[pr][qr] == INF) continue;
int cur = l.dp[pl][ql] + r.dp[pr][qr];
if (ql != pr) ++cur;
res.dp[pl][qr] = min(res.dp[pl][qr], cur);
}
}
}
}
return res;
}
const int N = (int)1e5 + 7;
Data t[4 * N];
void upd(int v, int vl, int vr, int pos, int sum1, int sum2, int type) {
if (vr - vl == 1) {
t[v] = Data{sum1, sum2, type};
} else {
int vm = (vl + vr) / 2;
if (pos < vm) {
upd(2 * v, vl, vm, pos, sum1, sum2, type);
} else {
upd(2 * v + 1, vm, vr, pos, sum1, sum2, type);
}
t[v] = merge(t[2 * v], t[2 * v + 1]);
}
}
Data query(int v, int vl, int vr, int l, int r) {
if (vl >= l && vr <= r) return t[v];
int vm = (vl + vr) / 2;
if (r <= vm) return query(2 * v, vl, vm, l, r);
if (vm <= l) return query(2 * v + 1, vm, vr, l, r);
return merge(query(2 * v, vl, vm, l, r), query(2 * v + 1, vm, vr, l, r));
}
void tbuild(int v, int vl, int vr) {
if (vr - vl == 1) {
t[v] = Data{0, 0, 0};
} else {
int vm = (vl + vr) / 2;
tbuild(2 * v, vl, vm);
tbuild(2 * v + 1, vm, vr);
t[v] = merge(t[2 * v], t[2 * v + 1]);
}
}
int n;
vector<int> g[N];
int a[N], b[N];
int sz[N];
int head[N];
int in[N]/*, out[N]*/, T = 0;
int cnt[N];
int type[N], sum1[N], sum2[N];
int par[N];
int find_size(int v, int p = -1) {
par[v] = p;
sz[v] = 1;
vector<int> gg;
for (int u : g[v]) {
if (u != p) {
sz[v] += find_size(u, v);
gg.push_back(u);
}
}
g[v] = gg;
return sz[v];
}
void build_hld(int v) {
for (int& u : g[v]) {
if (sz[u] > sz[g[v][0]]) swap(u, g[v][0]);
}
++cnt[head[v]];
in[v] = T++;
for (int u : g[v]) {
head[u] = u == g[v][0] ? head[v] : u;
build_hld(u);
}
//out[v] = T;
}
Data get_dp(int v) {
return query(1, 0, n, in[v], in[head[v]] + cnt[head[v]]);
}
void actual(int v) {
upd(1, 0, n, in[v], sum1[v], sum2[v], type[v]);
}
void init() {
for (int i = 0; i < n; ++i) g[i] = {};
for (int i = 0; i < n - 1; ++i) {
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
}
find_size(0);
head[0] = 0;
for (int i = 0; i < n; ++i) cnt[i] = 0;
build_hld(0);
tbuild(1, 0, n);
for (int i = 0; i < n; ++i) {
type[i] = sum1[i] = sum2[i] = 0;
}
}
int get_ans() {
auto res1 = get_dp(0);
int res = INF;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) res = min(res, res1.dp[i][j]);
}
return res;
}
void change_type(int V, int ntype) {
int v = V;
while (v != -1) {
if (v == head[v]) {
if (par[v] == -1) break;
auto cur = get_dp(v);
int dp1 = min(cur.dp[0][0], cur.dp[0][1]);
int dp2 = min(cur.dp[1][0], cur.dp[1][1]);
sum1[par[v]] -= min(dp1, dp2 + 1);
sum2[par[v]] -= min(dp2, dp1 + 1);
v = par[v];
} else {
v = head[v];
}
}
type[V] = ntype;
actual(V);
v = V;
while (v != -1) {
if (v == head[v]) {
if (par[v] == -1) break;
auto cur = get_dp(v);
int dp1 = min(cur.dp[0][0], cur.dp[0][1]);
int dp2 = min(cur.dp[1][0], cur.dp[1][1]);
sum1[par[v]] += min(dp1, dp2 + 1);
sum2[par[v]] += min(dp2, dp1 + 1);
actual(par[v]);
v = par[v];
} else {
v = head[v];
}
}
}
void initialize(int initn, vector<int> inita, vector<int> initb) {
n = initn;
for (int i = 0; i < n - 1; ++i) {
a[i] = inita[i] - 1;
b[i] = initb[i] - 1;
}
init();
}
int cat(int v) {
--v;
change_type(v, 1);
return get_ans();
}
int dog(int v) {
--v;
change_type(v, 2);
return get_ans();
}
int neighbor(int v) {
--v;
change_type(v, 0);
return get_ans();
}
#ifndef EVAL
int main() {
freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int initn;
cin >> initn;
vector<int> inita(initn - 1), initb(initn - 1);
for (int i = 0; i < initn - 1; ++i) {
cin >> inita[i] >> initb[i];
}
initialize(initn, inita, initb);
int qqq;
cin >> qqq;
while (qqq--) {
int kektype, kekv;
cin >> kektype >> kekv;
if (kektype == 1) {
cout << cat(kekv);
}
if (kektype == 2) {
cout << dog(kekv);
}
if (kektype == 3) {
cout << neighbor(kekv);
}
cout << '\n';
}
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8940 KB |
Output is correct |
2 |
Correct |
6 ms |
8940 KB |
Output is correct |
3 |
Correct |
6 ms |
8940 KB |
Output is correct |
4 |
Correct |
6 ms |
9068 KB |
Output is correct |
5 |
Correct |
6 ms |
8940 KB |
Output is correct |
6 |
Correct |
6 ms |
8940 KB |
Output is correct |
7 |
Correct |
6 ms |
8940 KB |
Output is correct |
8 |
Correct |
8 ms |
8940 KB |
Output is correct |
9 |
Correct |
6 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
6 ms |
8940 KB |
Output is correct |
12 |
Correct |
6 ms |
8940 KB |
Output is correct |
13 |
Correct |
6 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
6 ms |
8940 KB |
Output is correct |
16 |
Correct |
6 ms |
8940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8940 KB |
Output is correct |
2 |
Correct |
6 ms |
8940 KB |
Output is correct |
3 |
Correct |
6 ms |
8940 KB |
Output is correct |
4 |
Correct |
6 ms |
9068 KB |
Output is correct |
5 |
Correct |
6 ms |
8940 KB |
Output is correct |
6 |
Correct |
6 ms |
8940 KB |
Output is correct |
7 |
Correct |
6 ms |
8940 KB |
Output is correct |
8 |
Correct |
8 ms |
8940 KB |
Output is correct |
9 |
Correct |
6 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
6 ms |
8940 KB |
Output is correct |
12 |
Correct |
6 ms |
8940 KB |
Output is correct |
13 |
Correct |
6 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
6 ms |
8940 KB |
Output is correct |
16 |
Correct |
6 ms |
8940 KB |
Output is correct |
17 |
Correct |
9 ms |
9068 KB |
Output is correct |
18 |
Correct |
8 ms |
9068 KB |
Output is correct |
19 |
Correct |
7 ms |
9068 KB |
Output is correct |
20 |
Correct |
6 ms |
8940 KB |
Output is correct |
21 |
Correct |
8 ms |
8940 KB |
Output is correct |
22 |
Correct |
7 ms |
9068 KB |
Output is correct |
23 |
Correct |
9 ms |
9068 KB |
Output is correct |
24 |
Correct |
9 ms |
9068 KB |
Output is correct |
25 |
Correct |
8 ms |
9068 KB |
Output is correct |
26 |
Correct |
7 ms |
9068 KB |
Output is correct |
27 |
Correct |
7 ms |
9068 KB |
Output is correct |
28 |
Correct |
8 ms |
9068 KB |
Output is correct |
29 |
Correct |
8 ms |
9196 KB |
Output is correct |
30 |
Correct |
7 ms |
8940 KB |
Output is correct |
31 |
Correct |
7 ms |
9068 KB |
Output is correct |
32 |
Correct |
8 ms |
9068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8940 KB |
Output is correct |
2 |
Correct |
6 ms |
8940 KB |
Output is correct |
3 |
Correct |
6 ms |
8940 KB |
Output is correct |
4 |
Correct |
6 ms |
9068 KB |
Output is correct |
5 |
Correct |
6 ms |
8940 KB |
Output is correct |
6 |
Correct |
6 ms |
8940 KB |
Output is correct |
7 |
Correct |
6 ms |
8940 KB |
Output is correct |
8 |
Correct |
8 ms |
8940 KB |
Output is correct |
9 |
Correct |
6 ms |
8940 KB |
Output is correct |
10 |
Correct |
7 ms |
8940 KB |
Output is correct |
11 |
Correct |
6 ms |
8940 KB |
Output is correct |
12 |
Correct |
6 ms |
8940 KB |
Output is correct |
13 |
Correct |
6 ms |
8940 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
6 ms |
8940 KB |
Output is correct |
16 |
Correct |
6 ms |
8940 KB |
Output is correct |
17 |
Correct |
9 ms |
9068 KB |
Output is correct |
18 |
Correct |
8 ms |
9068 KB |
Output is correct |
19 |
Correct |
7 ms |
9068 KB |
Output is correct |
20 |
Correct |
6 ms |
8940 KB |
Output is correct |
21 |
Correct |
8 ms |
8940 KB |
Output is correct |
22 |
Correct |
7 ms |
9068 KB |
Output is correct |
23 |
Correct |
9 ms |
9068 KB |
Output is correct |
24 |
Correct |
9 ms |
9068 KB |
Output is correct |
25 |
Correct |
8 ms |
9068 KB |
Output is correct |
26 |
Correct |
7 ms |
9068 KB |
Output is correct |
27 |
Correct |
7 ms |
9068 KB |
Output is correct |
28 |
Correct |
8 ms |
9068 KB |
Output is correct |
29 |
Correct |
8 ms |
9196 KB |
Output is correct |
30 |
Correct |
7 ms |
8940 KB |
Output is correct |
31 |
Correct |
7 ms |
9068 KB |
Output is correct |
32 |
Correct |
8 ms |
9068 KB |
Output is correct |
33 |
Correct |
547 ms |
15360 KB |
Output is correct |
34 |
Correct |
183 ms |
15560 KB |
Output is correct |
35 |
Correct |
491 ms |
13924 KB |
Output is correct |
36 |
Correct |
931 ms |
19508 KB |
Output is correct |
37 |
Correct |
36 ms |
11884 KB |
Output is correct |
38 |
Correct |
940 ms |
20508 KB |
Output is correct |
39 |
Correct |
966 ms |
20520 KB |
Output is correct |
40 |
Correct |
1441 ms |
20644 KB |
Output is correct |
41 |
Correct |
892 ms |
20640 KB |
Output is correct |
42 |
Correct |
857 ms |
20540 KB |
Output is correct |
43 |
Correct |
1052 ms |
20444 KB |
Output is correct |
44 |
Correct |
805 ms |
20720 KB |
Output is correct |
45 |
Correct |
952 ms |
20572 KB |
Output is correct |
46 |
Correct |
1047 ms |
20544 KB |
Output is correct |
47 |
Correct |
953 ms |
20508 KB |
Output is correct |
48 |
Correct |
230 ms |
17360 KB |
Output is correct |
49 |
Correct |
231 ms |
19452 KB |
Output is correct |
50 |
Correct |
80 ms |
11640 KB |
Output is correct |
51 |
Correct |
93 ms |
13276 KB |
Output is correct |
52 |
Correct |
43 ms |
11176 KB |
Output is correct |
53 |
Correct |
352 ms |
19308 KB |
Output is correct |
54 |
Correct |
289 ms |
13676 KB |
Output is correct |
55 |
Correct |
731 ms |
17508 KB |
Output is correct |
56 |
Correct |
435 ms |
14484 KB |
Output is correct |
57 |
Correct |
570 ms |
18924 KB |
Output is correct |
58 |
Correct |
40 ms |
13384 KB |
Output is correct |
59 |
Correct |
93 ms |
12992 KB |
Output is correct |
60 |
Correct |
186 ms |
18472 KB |
Output is correct |
61 |
Correct |
201 ms |
18972 KB |
Output is correct |
62 |
Correct |
129 ms |
16856 KB |
Output is correct |
63 |
Correct |
75 ms |
18624 KB |
Output is correct |
64 |
Correct |
74 ms |
21100 KB |
Output is correct |
65 |
Correct |
106 ms |
27628 KB |
Output is correct |
66 |
Correct |
104 ms |
13804 KB |
Output is correct |
67 |
Correct |
120 ms |
22296 KB |
Output is correct |
68 |
Correct |
218 ms |
27684 KB |
Output is correct |
69 |
Correct |
54 ms |
10852 KB |
Output is correct |
70 |
Correct |
19 ms |
9324 KB |
Output is correct |
71 |
Correct |
90 ms |
18156 KB |
Output is correct |
72 |
Correct |
142 ms |
25820 KB |
Output is correct |
73 |
Correct |
342 ms |
32492 KB |
Output is correct |
74 |
Correct |
483 ms |
26924 KB |
Output is correct |
75 |
Correct |
245 ms |
32492 KB |
Output is correct |
76 |
Correct |
229 ms |
30444 KB |
Output is correct |
77 |
Correct |
470 ms |
27564 KB |
Output is correct |