## C - エックスオア多橋君

### 解説

$O(n \log n)$。

### 実装

#include <iostream>
#include <vector>
#include <map>
#include <cstdint>
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat(i,n) repeat_from(i,0,n)
typedef long long ll;
using namespace std;
struct edge_t { int to; uint32_t cost; };
void dfs(int v, int p, uint32_t value, vector<vector<edge_t> > const & g, map<uint32_t,ll> & acc) {
acc[value] += 1;
for (auto e : g[v]) if (e.to != p) {
dfs(e.to, v, value ^ e.cost, g, acc);
}
}
int main() {
int n; uint32_t x; cin >> n >> x;
vector<vector<edge_t> > g(n);
repeat (i,n-1) {
int x, y; uint32_t c; cin >> x >> y >> c;
-- x; -- y;
g[x].push_back((edge_t){ y, c });
g[y].push_back((edge_t){ x, c });
}
map<uint32_t,ll> acc;
dfs(0, -1, 0, g, acc);
ll result = 0;
for (auto it : acc) {
if (it.first < (it.first ^ x)) {
result += acc[it.first ^ x] * it.second;
} else if (it.first == (it.first ^ x)) { // means x == 0
result += it.second * (it.second - 1) / 2;
}
}
cout << result << endl;
return 0;
}