-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF.cpp
870 lines (712 loc) · 24.9 KB
/
F.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html
template<class Fun> class y_combinator_result {
Fun fun_;
public:
template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
template<typename T, bool maximum_mode = false>
struct RMQ {
int n = 0;
vector<T> values;
vector<vector<int>> range_low;
RMQ(const vector<T> &_values = {}) {
if (!_values.empty())
build(_values);
}
static int highest_bit(int x) {
return x == 0 ? -1 : 31 - __builtin_clz(x);
}
// Note: when `values[a] == values[b]`, returns b.
int better_index(int a, int b) const {
return (maximum_mode ? values[b] < values[a] : values[a] < values[b]) ? a : b;
}
void build(const vector<T> &_values) {
values = _values;
n = int(values.size());
int levels = highest_bit(n) + 1;
range_low.resize(levels);
for (int k = 0; k < levels; k++)
range_low[k].resize(n - (1 << k) + 1);
for (int i = 0; i < n; i++)
range_low[0][i] = i;
for (int k = 1; k < levels; k++)
for (int i = 0; i <= n - (1 << k); i++)
range_low[k][i] = better_index(range_low[k - 1][i], range_low[k - 1][i + (1 << (k - 1))]);
}
// Note: breaks ties by choosing the largest index.
int query_index(int a, int b) const {
assert(0 <= a && a < b && b <= n);
int level = highest_bit(b - a);
return better_index(range_low[level][a], range_low[level][b - (1 << level)]);
}
T query_value(int a, int b) const {
return values[query_index(a, b)];
}
};
struct LCA {
int n = 0;
vector<vector<int>> adj;
vector<int> parent, depth, subtree_size;
vector<int> euler, first_occurrence;
vector<int> tour_start, tour_end, postorder;
vector<int> tour_list, rev_tour_list;
vector<int> heavy_root;
RMQ<int> rmq;
bool built;
LCA(int _n = 0) {
init(_n);
}
// Warning: this does not call build().
LCA(const vector<vector<int>> &_adj) {
init(_adj);
}
void init(int _n) {
n = _n;
adj.assign(n, {});
parent.resize(n);
depth.resize(n);
subtree_size.resize(n);
first_occurrence.resize(n);
tour_start.resize(n);
tour_end.resize(n);
postorder.resize(n);
tour_list.resize(n);
heavy_root.resize(n);
built = false;
}
// Warning: this does not call build().
void init(const vector<vector<int>> &_adj) {
init(int(_adj.size()));
adj = _adj;
}
void add_edge(int a, int b) {
adj[a].push_back(b);
adj[b].push_back(a);
}
int degree(int v) const {
return int(adj[v].size()) + (built && parent[v] >= 0);
}
void dfs(int node, int par) {
parent[node] = par;
depth[node] = par < 0 ? 0 : depth[par] + 1;
subtree_size[node] = 1;
// Erase the edge to parent.
adj[node].erase(remove(adj[node].begin(), adj[node].end(), par), adj[node].end());
for (int child : adj[node]) {
dfs(child, node);
subtree_size[node] += subtree_size[child];
}
// Heavy-light subtree reordering.
sort(adj[node].begin(), adj[node].end(), [&](int a, int b) {
return subtree_size[a] > subtree_size[b];
});
}
int tour, post_tour;
void tour_dfs(int node, bool heavy) {
heavy_root[node] = heavy ? heavy_root[parent[node]] : node;
first_occurrence[node] = int(euler.size());
euler.push_back(node);
tour_list[tour] = node;
tour_start[node] = tour++;
bool heavy_child = true;
for (int child : adj[node]) {
tour_dfs(child, heavy_child);
euler.push_back(node);
heavy_child = false;
}
tour_end[node] = tour;
postorder[node] = post_tour++;
}
void build(int root = -1, bool build_rmq = true) {
parent.assign(n, -1);
if (0 <= root && root < n)
dfs(root, -1);
for (int i = 0; i < n; i++)
if (i != root && parent[i] < 0)
dfs(i, -1);
tour = post_tour = 0;
euler.clear();
euler.reserve(2 * n);
for (int i = 0; i < n; i++)
if (parent[i] < 0) {
tour_dfs(i, false);
// Add a -1 in between connected components to help us detect when nodes aren't connected.
euler.push_back(-1);
}
rev_tour_list = tour_list;
reverse(rev_tour_list.begin(), rev_tour_list.end());
assert(int(euler.size()) == 2 * n);
vector<int> euler_depths;
euler_depths.reserve(euler.size());
for (int node : euler)
euler_depths.push_back(node < 0 ? node : depth[node]);
if (build_rmq)
rmq.build(euler_depths);
built = true;
}
pair<int, array<int, 2>> get_diameter() const {
assert(built);
// We find the maximum of depth[u] - 2 * depth[x] + depth[v] where u, x, v occur in order in the Euler tour.
pair<int, int> u_max = {-1, -1};
pair<int, int> ux_max = {-1, -1};
pair<int, array<int, 2>> uxv_max = {-1, {-1, -1}};
for (int node : euler) {
if (node < 0) break;
u_max = max(u_max, {depth[node], node});
ux_max = max(ux_max, {u_max.first - 2 * depth[node], u_max.second});
uxv_max = max(uxv_max, {ux_max.first + depth[node], {ux_max.second, node}});
}
return uxv_max;
}
// Note: returns -1 if `a` and `b` aren't connected.
int get_lca(int a, int b) const {
a = first_occurrence[a];
b = first_occurrence[b];
if (a > b)
swap(a, b);
return euler[rmq.query_index(a, b + 1)];
}
bool is_ancestor(int a, int b) const {
return tour_start[a] <= tour_start[b] && tour_start[b] < tour_end[a];
}
bool on_path(int x, int a, int b) const {
return (is_ancestor(x, a) || is_ancestor(x, b)) && is_ancestor(get_lca(a, b), x);
}
int get_dist(int a, int b) const {
return depth[a] + depth[b] - 2 * depth[get_lca(a, b)];
}
// Returns the child of `a` that is an ancestor of `b`. Assumes `a` is a strict ancestor of `b`.
int child_ancestor(int a, int b) const {
assert(a != b);
assert(is_ancestor(a, b));
// Note: this depends on RMQ breaking ties by latest index.
int child = euler[rmq.query_index(first_occurrence[a], first_occurrence[b] + 1) + 1];
assert(parent[child] == a);
assert(is_ancestor(child, b));
return child;
}
int get_kth_ancestor(int a, int k) const {
while (a >= 0) {
int root = heavy_root[a];
if (depth[root] <= depth[a] - k)
return tour_list[tour_start[a] - k];
k -= depth[a] - depth[root] + 1;
a = parent[root];
}
return a;
}
int get_kth_node_on_path(int a, int b, int k) const {
int anc = get_lca(a, b);
int first_half = depth[a] - depth[anc];
int second_half = depth[b] - depth[anc];
assert(0 <= k && k <= first_half + second_half);
if (k < first_half)
return get_kth_ancestor(a, k);
else
return get_kth_ancestor(b, first_half + second_half - k);
}
// Note: this is the LCA of any two nodes out of three when the third node is the root.
// It is also the node with the minimum sum of distances to all three nodes (the centroid of the three nodes).
int get_common_node(int a, int b, int c) const {
// Return the deepest node among lca(a, b), lca(b, c), and lca(c, a).
int x = get_lca(a, b);
int y = get_lca(b, c);
int z = get_lca(c, a);
x = depth[y] > depth[x] ? y : x;
x = depth[z] > depth[x] ? z : x;
return x;
}
// Given a subset of k tree nodes, computes the minimal subtree that contains all the nodes (at most 2k - 1 nodes).
// Returns a list of {node, parent} for every node in the subtree. Runs in O(k log k).
vector<pair<int, int>> compress_tree(vector<int> nodes) const {
if (nodes.empty())
return {};
auto &&compare_tour = [&](int a, int b) { return tour_start[a] < tour_start[b]; };
sort(nodes.begin(), nodes.end(), compare_tour);
int k = int(nodes.size());
for (int i = 0; i < k - 1; i++)
nodes.push_back(get_lca(nodes[i], nodes[i + 1]));
sort(nodes.begin() + k, nodes.end(), compare_tour);
inplace_merge(nodes.begin(), nodes.begin() + k, nodes.end(), compare_tour);
nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());
vector<pair<int, int>> result = {{nodes[0], -1}};
for (int i = 1; i < int(nodes.size()); i++)
result.emplace_back(nodes[i], get_lca(nodes[i], nodes[i - 1]));
return result;
}
};
int highest_bit(int x) {
return x == 0 ? -1 : 31 - __builtin_clz(x);
}
struct segment_change {
int64_t to_set;
// Make sure that the default constructor is the identity segment_change
segment_change(int64_t _to_set = -1) : to_set(_to_set) {}
void reset() {
to_set = -1;
}
bool has_change() const {
return to_set != -1;
}
// Return the combined result of applying this segment change followed by `other`.
segment_change combine(const segment_change &other) const {
return segment_change(other.to_set);
}
};
struct segment {
int64_t sum;
// Make sure that the default constructor is the identity segment
segment(int64_t _sum = 0) : sum(_sum) {}
bool empty() const {
return sum == 0;
}
// lazy propagation apply to the particular node
void apply(int length, const segment_change &change) {
if(change.has_change()) {
sum = change.to_set;
}
}
void join(const segment &other) {
sum = max(sum, other.sum);
}
// merge function
void join(const segment &a, const segment &b) {
*this = a;
join(b);
}
};
pair<int, int> right_half[32];
struct seg_tree {
int tree_n = 0;
vector<segment> tree;
vector<segment_change> changes;
seg_tree(int n = -1) {
if (n >= 0)
init(n);
}
void init(int n) {
tree_n = 1;
while (tree_n < n)
tree_n *= 2;
tree.assign(2 * tree_n, segment());
changes.assign(tree_n, segment_change());
}
// Builds our tree from an array in O(n).
void build(const vector<segment> &initial) {
int n = int(initial.size());
init(n);
assert(n <= tree_n);
for (int i = 0; i < n; i++)
tree[tree_n + i] = initial[i];
for (int position = tree_n - 1; position > 0; position--)
tree[position].join(tree[2 * position], tree[2 * position + 1]);
}
void apply_and_combine(int position, int length, const segment_change &change) {
tree[position].apply(length, change);
if (position < tree_n) changes[position] = changes[position].combine(change);
}
void push_down(int position, int length) {
if (changes[position].has_change()) {
apply_and_combine(2 * position, length / 2, changes[position]);
apply_and_combine(2 * position + 1, length / 2, changes[position]);
changes[position].reset();
}
}
// Calls push_down for all necessary nodes in order to query the range [a, b).
void push_all(int a, int b) {
assert(0 <= a && a < b && b <= tree_n);
a += tree_n;
b += tree_n - 1;
for (int up = highest_bit(tree_n); up > 0; up--) {
int x = a >> up, y = b >> up;
push_down(x, 1 << up);
if (x != y) push_down(y, 1 << up);
}
}
void join_and_apply(int position, int length) {
tree[position].join(tree[2 * position], tree[2 * position + 1]);
tree[position].apply(length, changes[position]);
}
// Calls join for all necessary nodes after updating the range [a, b).
void join_all(int a, int b) {
assert(0 <= a && a < b && b <= tree_n);
a += tree_n;
b += tree_n - 1;
int length = 1;
while (a > 1) {
a /= 2;
b /= 2;
length *= 2;
join_and_apply(a, length);
if (a != b) join_and_apply(b, length);
}
}
template<typename T_range_op>
void process_range(int a, int b, bool needs_join, T_range_op &&range_op) {
if (a == b) return;
push_all(a, b);
int original_a = a, original_b = b;
int length = 1, r_size = 0;
for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2, length *= 2) {
if (a & 1)
range_op(a++, length);
if (b & 1)
right_half[r_size++] = {--b, length};
}
for (int i = r_size - 1; i >= 0; i--)
range_op(right_half[i].first, right_half[i].second);
if (needs_join)
join_all(original_a, original_b);
}
segment query(int a, int b) {
assert(0 <= a && a <= b && b <= tree_n);
segment answer;
process_range(a, b, false, [&](int position, int) {
answer.join(tree[position]);
});
return answer;
}
void update(int a, int b, const segment_change &change) {
assert(0 <= a && a <= b && b <= tree_n);
process_range(a, b, true, [&](int position, int length) {
apply_and_combine(position, length, change);
});
}
vector<segment> to_array() {
for (int i = 1; i < tree_n; i++)
push_down(i, tree_n >> highest_bit(i));
vector<segment> segs(tree_n);
for (int i = 0; i < tree_n; i++)
segs[i] = tree[tree_n + i];
return segs;
}
void update_single(int index, const segment &seg) {
assert(0 <= index && index < tree_n);
int position = tree_n + index;
for (int up = highest_bit(tree_n); up > 0; up--)
push_down(position >> up, 1 << up);
tree[position] = seg;
while (position > 1) {
position /= 2;
tree[position].join(tree[2 * position], tree[2 * position + 1]);
}
}
// Finds the last subarray starting at `first` that satisifes `should_join` via binary search in O(log n).
template<typename T_bool>
int find_last_subarray(T_bool &&should_join, int n, int first = 0) {
assert(0 <= first && first <= n);
segment current;
// Check the degenerate case.
if (!should_join(current, current))
return first - 1;
return y_combinator([&](auto search, int position, int start, int end) -> int {
if (end <= first) {
return end;
} else if (first <= start && end <= n && should_join(current, tree[position])) {
current.join(tree[position]);
return end;
} else if (end - start == 1) {
return start;
}
push_down(position, end - start);
int mid = (start + end) / 2;
int left = search(2 * position, start, mid);
return left < mid ? left : search(2 * position + 1, mid, end);
})(1, 0, tree_n);
}
};
// Uses segment tree template
struct heavy_light_decomposition {
vector<int> heavy_child, parent, head, depth, subtree_size, label, order;
vector<vector<int>> adj;
vector<pair<int,int>> subtree_segment;
seg_tree tree;
int N;
void init(int n) {
N = n;
heavy_child = vector<int>(n);
parent = vector<int>(n);
head = vector<int>(n);
subtree_size = vector<int>(n, 1);
label = vector<int>(n);
depth = vector<int>(n);
subtree_segment = vector<pair<int,int>>(n);
adj = vector<vector<int>>(n, vector<int>());
iota(head.begin(), head.end(), 0);
iota(parent.begin(), parent.end(), 0);
tree.init(n);
}
// Add a bi-directional edge from `from` to `to`
void add_bidirectional_edge(const int &from, const int &to) {
adj[from].push_back(to);
adj[to].push_back(from);
}
// Finds the heavy child of a node and precomputes the depth of each node
void find_heavy_child(int node, int par) {
parent[node] = par;
int max_child_subtree_size = 0;
heavy_child[node] = node;
for(int child: adj[node]) {
if(child == par) {
continue;
}
depth[child] = depth[node] + 1;
find_heavy_child(child, node);
if(subtree_size[child] > max_child_subtree_size) {
max_child_subtree_size = subtree_size[child];
heavy_child[node] = child;
}
subtree_size[node] += subtree_size[child];
}
}
// relabels the heavy nodes in a continuous segment so that later one segment tree
// can do the job. The head of each heavy segment is stored in the heavy chain nodes.
void accumulate_heavy_segments_and_relabel(int node) {
order.push_back(node);
label[node] = int(order.size()) - 1;
subtree_segment[node].first = label[node];
head[heavy_child[node]] = head[node];
if(heavy_child[node] != node)
accumulate_heavy_segments_and_relabel(heavy_child[node]);
for(int child: adj[node]) {
if(child != heavy_child[node] && child != parent[node]) {
accumulate_heavy_segments_and_relabel(child);
}
}
subtree_segment[node].second = int(order.size());
}
/** O(N) build of the segment tree for heavy light decomposition structure
@param roots -> contains the roots of the forest, if its empty, it would take random roots from the forest
@param values -> contains the initial values of the nodes
*/
void build(vector<int> roots, const vector<int64_t> &values) {
assert(int(values.size()) == N);
if(roots.empty()) {
for(int i = 0; i < N; i++) {
if(parent[i] == i) {
find_heavy_child(i, i);
accumulate_heavy_segments_and_relabel(i);
}
}
} else {
for(int root: roots) {
find_heavy_child(root, root);
accumulate_heavy_segments_and_relabel(root);
}
}
// do segment tree building
vector<segment> reordered_values(int(values.size()));
for(int i=0;i<N;i++) {
reordered_values[i] = segment(values[order[i]]);
}
tree.build(reordered_values);
}
// Calculate the defined function along the path from node `u` to node `v`
// It takes O(log^2(N)) in the worst case
int64_t query (int u, int v) {
segment res;
while(true) {
int x = head[u];
int y = head[v];
if(x == y) {
int label1 = label[u];
int label2 = label[v];
if(label1 > label2) {
swap(label1, label2);
}
segment tmp;
// tmp = tree.query(label1, label2 + 1);
// for edge queries comment above and uncomment below
tmp = tree.query(label1 + 1, label2 + 1);
res.join(tmp);
break;
}
if(depth[x] > depth[y]) {
int label1 = label[u];
int label2 = label[x];
if(label1 > label2) {
swap(label1, label2);
}
segment tmp = tree.query(label1, label2 + 1);
res.join(tmp);
u = parent[x];
} else {
int label1 = label[v];
int label2 = label[y];
if(label1 > label2) {
swap(label1, label2);
}
segment tmp = tree.query(label1, label2 + 1);
res.join(tmp);
v = parent[y];
}
}
return res.sum;
}
// Update the nodes along the path from node `u` to node `v` based on the defined function
// It takes O(log^2(N)) in the worst case
void update(int u, int v, const int64_t &val) {
while(true) {
int x = head[u];
int y = head[v];
if(x == y) {
int label1 = label[u];
int label2 = label[v];
if(label1 > label2) {
swap(label1, label2);
}
// tree.update(label1, label2 + 1, segment_change(val));
// for edge queries comment above and uncomment below
tree.update(label1 + 1, label2 + 1, segment_change(val));
break;
}
if(depth[x] > depth[y]) {
int label1 = label[u];
int label2 = label[x];
if(label1 > label2) {
swap(label1, label2);
}
tree.update(label1, label2 + 1, segment_change(val));
u = parent[x];
} else {
int label1 = label[v];
int label2 = label[y];
if(label1 > label2) {
swap(label1, label2);
}
tree.update(label1, label2 + 1, segment_change(val));
v = parent[y];
}
}
}
void update_entire_subtree(int node, const int64_t &val) {
tree.update(subtree_segment[node].first, subtree_segment[node].second, segment_change(val));
}
};
class DSU {
public:
vector<int> p;
int n;
DSU(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
inline int find(int x) {
return (x == p[x] ? x : (p[x] = find(p[x])));
}
inline bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
p[x] = y;
return true;
}
return false;
}
};
void run_cases() {
int N, Q;
cin >> N >> Q;
vector<array<int, 4>> tree_edges, cycle_edges;
DSU dsu(N);
LCA lca(N);
heavy_light_decomposition tree;
tree.init(N);
for(int i = 0; i < Q; i++) {
int u, v, x;
cin >> u >> v >> x;
--u, --v;
if(dsu.find(u) == dsu.find(v)) {
cycle_edges.push_back({u, v, x, i});
} else {
tree.add_bidirectional_edge(u, v);
lca.add_edge(u, v);
tree_edges.push_back({u, v, x, i});
dsu.unite(u, v);
}
}
vector<string> ans(Q);
vector<int> depth(N);
vector<int64_t> values(N);
vector<int> xor_vals(N);
vector<vector<int>> adj(N);
for(auto edges: tree_edges) {
int u = edges[0];
int v = edges[1];
int index = edges[3];
adj[u].push_back(v);
adj[v].push_back(u);
ans[index] = "YES";
}
function<void(int,int)> dfs = [&](int node, int par) -> void {
if(par != -1) {
depth[node] = depth[par] + 1;
}
for(int child: adj[node]) {
if(child != par) {
dfs(child, node);
}
}
};
function<void(int,int)> dfs2 = [&](int node, int par) -> void {
if(par != -1) {
xor_vals[node] ^= xor_vals[par];
}
for(int child: adj[node]) {
if(child != par) {
dfs2(child, node);
}
}
};
vector<int> roots;
for(int i = 0; i < N; i++) {
if(dsu.find(i) == i) {
roots.push_back(i);
dfs(i, -1);
}
}
for(auto edges: tree_edges) {
int u = edges[0];
int v = edges[1];
int x = edges[2];
if(depth[u] > depth[v]) {
swap(u, v);
}
values[v] = x;
xor_vals[v] = x;
}
for(int root: roots) {
dfs2(root, -1);
}
tree.build(roots, values);
lca.build();
for(auto edges: cycle_edges) {
int u = edges[0];
int v = edges[1];
int x = edges[2];
int index = edges[3];
bool ok = false;
int cycle_xor = xor_vals[u] ^ xor_vals[v] ^ x;
if(cycle_xor == 1 && tree.query(u, v) <= 1) {
ok = true;
tree.update(u, v, 2);
}
ans[index] = ok ? "YES" : "NO";
}
for(auto u: ans) {
cout << u << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1;test <= tests;test++) {
run_cases();
}
}