#P17304. [ICPC 2026 Xi'an I] XOR and LCA

    ID: 17195 Type: RemoteJudge 4000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 8 Uploaded By: Tags>ICPC2026省赛/邀请赛西安

[ICPC 2026 Xi'an I] XOR and LCA

题目描述

Yuki has a tree with 2n2^n nodes, labeled from 00 to 2n−12^n - 1. The ii-th edge connects node uiu_i and node viv_i.

Let lca⁡r(u,v)\operatorname{lca}_{r}(u, v) denote the lowest common ancestor of nodes uu and vv when the tree is rooted at node rr. You need to help Yuki calculate:

$$\bigoplus_{0 \le u < v < 2^n} \operatorname{lca}_{u \oplus v}(u, v)$$

where ⊕\oplus denotes the bitwise XOR operation.

输入格式

This problem contains multiple test cases.

The first line contains a positive integer tt (1≤t≤104)(1 \le t \le 10^4), representing the number of test cases.

For each test case:

  • The first line contains a positive integer nn (1≤n≤21)(1 \le n \le 21).
  • The next 2n−12^n - 1 lines each contain two integers ui,viu_i, v_i (0≤ui,vi<2n, ui≠vi)(0 \le u_i, v_i < 2^n,\ u_i \ne v_i).

It is guaranteed that the input forms a tree, and the sum of 2n2^n over all test cases does not exceed 2212^{21}.

输出格式

For each test case, output a single line containing an integer representing the answer.

4
1
0 1
2
0 1
1 2
2 3
3
0 1
0 2
0 3
0 4
0 5
0 6
0 7
3
4 5
2 6
3 7
0 2
1 5
2 7
6 4
1
2
0
4

提示

For the first test case:

  • When the tree is rooted at node 11, the lowest common ancestor of nodes 00 and 11 is node 11, so the answer is lca⁡1(0,1)=1\operatorname{lca}_1(0, 1) = 1.

For the second test case:

  • We calculate lca⁡u⊕v(u,v)\operatorname{lca}_{u \oplus v}(u, v) for all pairs (u,v)(u, v):
    • (0,1)(0, 1): 0⊕1=10 \oplus 1 = 1, lca⁡1(0,1)=1\operatorname{lca}_1(0, 1) = 1.
    • (0,2)(0, 2): 0⊕2=20 \oplus 2 = 2, lca⁡2(0,2)=2\operatorname{lca}_2(0, 2) = 2.
    • (0,3)(0, 3): 0⊕3=30 \oplus 3 = 3, lca⁡3(0,3)=3\operatorname{lca}_3(0, 3) = 3.
    • (1,2)(1, 2): 1⊕2=31 \oplus 2 = 3, lca⁡3(1,2)=2\operatorname{lca}_3(1, 2) = 2.
    • (1,3)(1, 3): 1⊕3=21 \oplus 3 = 2, lca⁡2(1,3)=2\operatorname{lca}_2(1, 3) = 2.
    • (2,3)(2, 3): 2⊕3=12 \oplus 3 = 1, lca⁡1(2,3)=2\operatorname{lca}_1(2, 3) = 2.
  • The XOR sum is 1⊕2⊕3⊕2⊕2⊕2=21 \oplus 2 \oplus 3 \oplus 2 \oplus 2 \oplus 2 = 2.