/** * @author SCaffrey (srius.caffrey@gmail.com) * @date 2016-01-02 19:01:31 * @copyright MIT */ #include // NOLINT #include // NOLINT #include // NOLINT #include // NOLINT #define x1 x11 #define y1 y11 #define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x) #define g(x, y, z) for (int x = (y), __ = (z); x <= __; ++x) #define fd(x, y, z) for (int x = (y), __ = (z); x > __; --x) #define gd(x, y, z) for (int x = (y), __ = (z); x >= __; --x) #ifdef WIN32 #define LLD "%I64d" #define LLU "%I64u" #else #define LLD "%lld" #define LLU "%llu" #endif typedef long long LL;// NOLINT typedef long double real; const double INF = 1e100; const int oo = ~0u >> 2; const double pi = acos(-1.0); const double EPS = 1e-8; const int MAXN = 100033; int n, m; LL cur, ans; int T; int u, v, w; int fa[MAXN]; int cnt[MAXN]; inline int get(int x) { if (x == fa[x]) return x; return fa[x] = get(fa[x]); } // int sz[2][MAXN]; // int hasOne[MAXN]; int main() { #ifdef LOCAL freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); #endif scanf("%d", &T); while (T--) { scanf("%d", &n); // memset(sz, 0, sizeof sz); // memset(hasOne, 0, sizeof hasOne); g(i, 1, n) fa[i] = i; f(i, 1, n) { scanf("%d%d%d", &u, &v, &w); if (w == 0) fa[ get(u) ] = get(v); // ++sz[w][u]; ++sz[w][v]; // if (!w) hasOne[u] = hasOne[v] = 1; } memset(cnt, 0, sizeof cnt); g(i, 1, n) ++cnt[ get(i) ]; cur = 0LL; // g(i, 1, n) printf("%d\n", cnt[i]); g(i, 1, n) cur ^= cnt[ fa[i] ]; // f(i, 1, n) { // scanf("%d%d%d", &u, &v, &w); // ++sz[w][u]; ++sz[w][v]; // if (!w) hasOne[u] = hasOne[v] = 1; // } // cur = 0LL; // g(i, 1, n) { // // printf("%d\n", hasOne[i]?sz[0][i]+1:sz[1][i]); // if (hasOne[i]) cur ^= sz[0][i] + 1; // else cur ^= 1; // } printf(LLD, cur); puts(""); } #ifdef LOCAL fclose(stdin); fclose(stdout); #endif return 0; }