-
Notifications
You must be signed in to change notification settings - Fork 0
/
2002D.cpp
273 lines (224 loc) · 5.49 KB
/
2002D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Created at: Mon Aug 12 00:10:08 CST 2024
/*
Problem Summary:
Given a tree and a series of vertex swaps in a permutation representing a DFS
order of the tree, determine if the permutation after each swap can still be a
valid DFS order.
Key Insight:
1. Calculate the size of each subtree.
2. Calculate the parent of each node.
3. Define a helper function to determine if a permutation is a valid DFS order.
4. Process swaps and determine if the new permutation is valid.
Step-by-Step Plan:
1. Parse the input and initialize the tree structure.
2. Calculate subtree sizes using DFS.
3. Determine the parent for each vertex.
4. Implement a helper function to validate the current permutation as a DFS
order.
5. For each query, swap the vertices in the permutation and validate using the
helper function.
6. Print results for each query.
*/
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#ifdef DBG
#include "debug.h"
#else
#define dbg(...)
#define dbg_export(...)
#endif
struct TreeTricks
{
int n;
vector<int> size, top;
/// parent[root] = -1;
vector<int> parent;
/// dep[root] = 0
vector<int> dep;
int cur;
vector<int> in, out, seq;
/// adj[u][0] is the "heavy" child of u
vector<std::vector<int>> adj;
TreeTricks() {}
TreeTricks(int n) { init(n); }
void init(int n)
{
this->n = n;
size.resize(n + 1);
top.resize(n + 1);
dep.resize(n + 1);
parent.resize(n + 1);
in.resize(n + 1);
out.resize(n + 1);
seq.resize(n + 1);
cur = 0;
adj.assign(n + 1, {});
}
void insert(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void work(int root = 1)
{
top[root] = root;
dep[root] = 0;
parent[root] = -1;
dfs1(root);
dfs2(root);
assert(cur == n);
}
void dfs1(int u)
{
if (parent[u] != -1) {
adj[u].erase(std::find(adj[u].begin(), adj[u].end(), parent[u]));
}
size[u] = 1;
for (auto &v : adj[u]) {
parent[v] = u;
dep[v] = dep[u] + 1;
dfs1(v);
size[u] += size[v];
if (size[v] > size[adj[u][0]]) {
std::swap(v, adj[u][0]);
}
}
}
void dfs2(int u)
{
cur += 1;
in[u] = cur;
seq[in[u]] = u;
for (auto v : adj[u]) {
top[v] = v == adj[u][0] ? top[u] : v;
dfs2(v);
}
out[u] = cur;
}
int lca(int u, int v)
{
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) {
u = parent[top[u]];
} else {
v = parent[top[v]];
}
}
return dep[u] < dep[v] ? u : v;
}
int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
/// Move up k steps
int jump(int u, int k)
{
if (dep[u] < k) {
return -1;
}
int d = dep[u] - k;
while (dep[top[u]] > d) {
u = parent[top[u]];
}
return seq[in[u] - dep[u] + d];
}
/// Is u an ancestor of v?
///
/// Note that it returns true if u == v.
bool is_ancester(int u, int v) { return in[u] <= in[v] && in[v] <= out[u]; }
};
int n, q;
vector<int> perm;
void reset_globals()
{
n = 0;
q = 0;
perm.clear();
}
TreeTricks tree;
i64 solve(vector<pair<int, int>> &queries)
{
auto value = [&](int i) -> bool {
if (i < 1 || i >= n)
return false;
int u = perm[i];
int v = tree.parent[perm[i + 1]];
if (v == -1)
return false;
if (tree.is_ancester(v, u)) {
return true;
}
return false;
};
int PP = 0;
vector<int> value_tmp(n + 1);
for (int i = 1; i < n; i++) {
value_tmp[i] = value(i);
PP += value_tmp[i];
}
auto update_value = [&](int i) -> int {
if (i < 1 || i >= n)
return 0;
int ret = -value_tmp[i];
value_tmp[i] = value(i);
ret += value_tmp[i];
return ret;
};
for (auto [x, y] : queries) {
dbg("====");
for (int i = 1; i < n; i++) {
dbg(value(i));
}
dbg(x, y, x - 1, y - 1);
swap(perm[x], perm[y]);
PP += update_value(x);
PP += update_value(y);
PP += update_value(x - 1);
PP += update_value(y - 1);
#ifdef DBG
dbg("----");
int PP_ = 0;
for (int i = 1; i < n; i++) {
PP_ += value(i);
dbg(value(i));
}
assert(PP == PP_);
#endif
if (PP == n - 1)
cout << "YES";
else
cout << "NO";
cout << endl;
}
return 0;
}
int main()
{
#ifndef DBG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int t;
cin >> t;
while (t--) {
reset_globals();
cin >> n >> q;
tree.init(n);
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
tree.insert(p, i);
}
perm.resize(n + 1);
for (int i = 1; i <= n; i++) {
cin >> perm[i];
}
tree.work(1);
vector<pair<int, int>> queries(q);
for (int i = 0; i < q; i++) {
cin >> queries[i].first >> queries[i].second;
}
solve(queries);
}
return 0;
}
// Time Complexity: O(n + q) per test case considering preprocessing and each
// query being O(1).