-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK_Path_Query.cpp
More file actions
398 lines (313 loc) · 11.7 KB
/
Copy pathK_Path_Query.cpp
File metadata and controls
398 lines (313 loc) · 11.7 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <stack>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <functional>
#define infinity 999999999999999999
#define float long double
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define MOD_DEFINE const int MOD = 1e9 + 7;
#define And &&
#define Or ||
#define endl '\n'
#define space " "
#define int long long
#define pii pair<int, int>
#define vi vector<int>
#define pb(n) push_back(n)
#define mii map<int, int>
#define umii unordered_map<int, int>
#define test_cases_loop int t; cin >> t; while(t--)
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define loop(var, initial, final) for(int var=initial; var < final; var++)
using namespace std;
MOD_DEFINE
typedef long long ll;
template<typename T>
void print(array<T,2> &arr);
template<typename T>
void print(vector<T> &v);
template<typename T>
void print(deque<T> &v);
template<typename T>
T _gcd(T a, T b);
template<typename T>
vector<T> sieve(T n);
template<typename T>
T power(T x, T y, ll p = LLONG_MAX);
ll nxt();
// // Returns n^(-1) mod p
template<typename T>
T modInverse(T n, T p);
ll gcd(vector<int> &diff);
bool isPowerof2(ll x);
vector<int> divisors(ll n){
vector<int> ans;
for(int i = 2;i<=sqrt(n);i++){
if(n % i ==0){
ans.pb(i);
if(n/i != i) ans.pb(n/i);
}
}
ans.pb(1);
return ans;
}
int binomial_cofficient(int n, int r){
if(r == 0) return 1;
return n*binomial_cofficient(n-1, r-1)/r;
}
int power_modulus(int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
{
int chloe = power_modulus(x, y/2);
return (chloe*chloe)%MOD;
}
else
{
int mermaid = power_modulus(x, y/2);
return (((mermaid*mermaid)%MOD)*x)%MOD;
}
}
// ░░░░░░░░░░░░▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄░░░░░░░░░░░░░
// ░░░░░▄▄▄▄█▀▀▀░░░░░░░░░░░░▀▀██░░░░░░░░░░░
// ░░▄███▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█▄▄▄░░░░░░░
// ▄▀▀░█░░░░▀█▄▀▄▀██████░▀█▄▀▄▀████▀░░░░░░░
// █░░░█░░░░░░▀█▄█▄███▀░░░░▀▀▀▀▀▀▀░▀▀▄░░░░░
// █░░░█░▄▄▄░░░░░░░░░░░░░░░░░░░░░▀▀░░░█░░░░
// █░░░▀█░░█░░░░▄░░░░▄░░░░░▀███▀░░░░░░░█░░░
// █░░░░█░░▀▄░░░░░░▄░░░░░░░░░█░░░░░░░░█▀▄░░
// ░▀▄▄▀░░░░░▀▀▄▄▄░░░░░░░▄▄▄▀░▀▄▄▄▄▄▀▀░░█░░
// ░█▄░░░░░░░░░░░░▀▀▀▀▀▀▀░░░░░░░░░░░░░░█░░░
// ░░█░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▄██░░░░
// ░░▀█▄░░░░░░░░░░░░░░░░░░░░░░░░░▄▀▀░░░▀█░░
// █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
// █░░█▀▄ █▀▀ █▀█ █░░░░█░▄░█ █ ▀█▀ █░█░░█ ▀█▀░█
// █░░█░█ █▀▀ █▀█ █░░░░▀▄▀▄▀ █ ░█░ █▀█░░█ ░█░░█
// █░░▀▀░ ▀▀▀ ▀░▀ ▀▀▀░░░▀░▀░ ▀ ░▀░ ▀░▀░░▀ ░▀░░█
// ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
int min_(int a, int b){
int ans;
a < b ? ans = a : ans = b;
return ans;
}
void dfs(int node, int parent, int depth, vi &depth_map, vector<vector<pii> > &directions, vector< vi> &adjacency_list){
// cout << "debug " << node << endl;
depth_map[node] = depth;
// directions[node] = directions[parent];
loop(i, 0, sz(directions[parent])) directions[node].push_back(directions[parent][i]);
if(node != 1 and sz(adjacency_list[node]) == 1){
return;
}
if(sz(adjacency_list[node]) == 2 and node != 1){
// linear chain
int child;
adjacency_list[node][0] == parent ? child = adjacency_list[node][1] : child = adjacency_list[node][0];
dfs(child, node, depth+1, depth_map, directions, adjacency_list);
}
else{
loop(i, 0, sz(adjacency_list[node])){
if(adjacency_list[node][i] != parent){
int child = adjacency_list[node][i];
pii zero; zero.first = node; zero.second = i;
directions[child].push_back(zero);
dfs(child, node, depth+1, depth_map, directions, adjacency_list);
}
}
}
return;
}
int32_t main(){
#ifndef ONLINE_JUDGE
// For getting input from input.txt file
freopen("input.txt", "r", stdin);
// Printing the Output to output.txt file
freopen("output.txt", "w", stdout);
#endif
FIO
// time complexity = O(nlogn + klogn)
test_cases_loop{
int n; cin >> n;
if(n == 1){
int q; cin >> q;
loop(i, 0, q){
int a; cin >> a; int t; cin >> t;
}
cout << "YES" << endl;
continue;
}
vector<vi> adjacency_list(n+1);
loop(i, 0, n-1)
{
int parent, child; cin >> parent >> child;
adjacency_list[parent].pb(child); adjacency_list[child].pb(parent);
}
vector<vector<pii> > directions(1+n);
pii zero;
zero.first = 0;
zero.second = 0;
directions[0].pb(zero);
vi depth_map(n+1);
int q; cin >> q;
dfs(1, 0, 0, depth_map, directions, adjacency_list);
// loop(i, 1, n+1){
// cout << i << space << ":" << endl;
// loop(j, 0, sz(directions[i])){
// cout << directions[i][j].first << space << directions[i][j].second << endl;
// }
// cout << endl;
// }
loop(i, 0, q){
int a; cin >> a;
vector<pii> node_direction_parity(n+1);
// multimap<int, int> node_direction_parity;
bool partition = false;
bool answer = true;
int dep_of_partition = -infinity;
int least_depth = infinity;
loop(j, 0, a){
int node; cin >> node;
least_depth = min_(least_depth, depth_map[node]);
loop(i, 0, sz(directions[node])){
int key, val;
pii key_val = directions[node][i];
key = key_val.first, val = key_val.second;
// cout << "debug " << key << space << val << endl;
if(node_direction_parity[key].first != 1){
if(val != node_direction_parity[key].first and val != node_direction_parity[key].second){
// partition spotted;
if(!partition) {
partition = true;
dep_of_partition = depth_map[node];
node_direction_parity[key].second = val;
}
else answer = false;
}
}
else{
pii zero; zero.first = val; zero.second = -1;
node_direction_parity[key] = zero;
}
}
}
if(answer){
// only one partiion or no partition was found
if(least_depth >= dep_of_partition){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
else cout << "NO" << endl;
}
}
return 0;
}
// ▄ ▄
// ▌▒█ ▄▀▒▌
// ▌▒▒█ ▄▀▒▒▒▐
// ▐▄█▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
// ▄▄▀▒▒▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
// ▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
// ▐▒▒▒▄▄▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀▄▒▌
// ▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
// ▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌
// ▌░▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌
// ▌▒▒▒▄██▄▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▐
// ▐▒▒▐▄█▄█▌▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
// ▐▒▒▐▀▐▀▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▐
// ▌▒▒▀▄▄▄▄▄▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▌
// ▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▒▄▒▒▐
// ▀▄▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▄▒▒▒▒▌
// ▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀
// ▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀
// ▀▀▀▀▀▀▀▀▀▀▀▀
template<typename T>
T power(T x, T y, ll p) {
T res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
// cout <<"x= "<< x << endl;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
// cout << "res" << res << endl;
return res;
}
template<typename T>
vector<T> sieve(T n){
vector<int> prime(n+1, 1);
for(int i = 2; i*i<=n; i++){
if(prime[i]){
for(int j = 2; j*i<=n; j++){
prime[i*j] = 0;
}
}
}
vector<T> ans;
for(int i = 2; i<=n;i++){
if(prime[i]) ans.pb(i);
}
return prime;
}
template<typename T>
T _gcd(T a, T b){
T temp1 = max(a,b), temp2 = min(a,b);
a = temp1, b = temp2;
if(a == 0) return b;
if(b == 0) return a;
if(a%b ==0) return b;
return _gcd(b, a%b);
}
template<typename T>
void print(array<T,2> &arr){
cout << arr[0] << " " << arr[1] << endl;
}
template<typename T>
void print(vector<T> &v){
for(T x: v) cout << x << " ";
cout << endl;
}
template<typename T>
T modInverse(T n, T p){
return power(n, p-2, p)%p;
}
template<typename T>
void print(deque<T> &v){
for(auto x: v) cout << x << " ";
cout << endl;
}
ll nxt(){
ll x;
cin >> x;
return x;
}
bool isPowerof2(ll x){
return !(x && (x & (x-1)));
}