給你具有個結點條邊的無向無環連通圖,結點編號,每條邊上有一個數作爲他的邊權,定義函數爲連接的簡單路徑的所有邊權的異或值
求,爲按位異或運算,表示所有整數異或後的結果
輸入描述
輸入的第一行爲一個整數,代表測試用例的組數
接下來的組測試用例按照如下格式給出:
每組數據佔行,第一行有個整數,接下來的行,每行有個整數,分別表示每條邊的起點、終點、權值
輸出描述
對於每組測試數據,在新的一行中輸出答案
數據範圍
樣例輸入
2
1
2
1 2 3
樣例輸出
0
3
設爲節點到路徑上所有邊權的異或和,。
因爲,所以
將從到遍歷一遍求出答案。
複雜度爲。
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 2e5 + 10;
int head[N], ver[N << 1], Next[N << 1], edge[N << 1], tot;
int T, n, a[N], s[N];
bool v[N];
inline void add(int x, int y, int z) {
ver[++tot] = y;
Next[tot] = head[x];
edge[tot] = z;
head[x] = tot;
}
inline void bfs() {
repi(i, 1, n)v[i] = false, a[i] = s[i] = 0;
queue<int> q;
v[1] = true, q.push(1);
while (!q.empty()) {
int x = q.front();
q.pop();
reps(x) {
int y = ver[i], z = edge[i];
if (v[y])continue;
v[y] = true, a[y] = a[x] ^ z, q.push(y);
}
}
s[n] = a[n];
repd(i, n - 1, 1)s[i] = a[i] ^ s[i + 1];
}
int main() {
T = qr();
while (T--) {
n = qr();
repi(i, 1, n)head[i] = 0;
repi(i, 1, n - 1) {
int x = qr(), y = qr(), z = qr();
add(x, y, z), add(y, x, z);
}
bfs();
int ans = 0;
repi(i, 1, n) {
ans ^= s[i];
if ((n - i + 1) & 1)ans ^= a[i];
}
pi(ans);
}
return 0;
}