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
| #include <bits/stdc++.h> using namespace std; const int iol = 1024 * 1024; char buf[iol], *ioh, *iot, ioc; bool iosig; inline char read() { if (ioh == iot) { iot = (ioh = buf) + fread(buf, 1, iol, stdin); if (ioh == iot) return -1; } return *ioh++; } template<class T> inline bool read(T& x) { iosig = false; for (ioc = read(); !isdigit(ioc); ioc = read()) { if (ioc == -1) return false; if (ioc == '-') iosig = true; } x = 0; while (ioc == '0') ioc = read(); for (; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0'); ioh--; if (iosig) x = -x; return true; } const int MAXN = 100010; int n, tot; int father[MAXN], son[MAXN * 20][2], pre[MAXN * 20], dis[MAXN]; struct Node { int v, w; Node() {} Node(int v, int w) : v(v), w(w) {} }; std::vector<Node> edge[MAXN]; inline void addEdge(int u, int v, int w) { edge[u].push_back(Node(v, w)); edge[v].push_back(Node(u, w)); } inline void bfs(int u = 1) { queue<int> q; q.push(u); while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < edge[x].size(); i++) { Node *now = &edge[x][i]; if (now->v != father[x]) { father[now->v] = x; dis[now->v] = dis[x] ^ now->w; q.push(now->v); } } } } int main() { int maxw = 0; read(n); for (int i = 0, u, v, w; i < n; i++) read(u), read(v), read(w), addEdge(u, v, w), maxw = max(maxw, w); bfs(); int log = log2(maxw); for (int i = 1, tmp; i <= n; i++) { tmp = 0; for (int j = log, x; j >= 0; j--) { x = (dis[i] & (1 << j)) ? 1 : 0; if (!son[tmp][x]) son[tmp][x] = ++tot; tmp = son[tmp][x]; } pre[tmp] = i; } int ans = 0; for (int i = 1, tmp; i <= n; i++) { tmp = 0; for (int j = log, x; j >= 0; j--) { x = (dis[i] & (1 << j)) ? 1 : 0; if (son[tmp][!x]) tmp = son[tmp][!x]; else tmp = son[tmp][x]; } ans = max(ans, dis[i] ^ dis[pre[tmp]]); } cout << ans; return 0; }
|