From fdb7b7a070053ed34b2b21d334786650fa23e1fe Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 3 Oct 2021 11:13:48 +0800 Subject: [PATCH] =?UTF-8?q?144.=20=E6=9C=80=E9=95=BF=E5=BC=82=E6=88=96?= =?UTF-8?q?=E5=80=BC=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/8019318/ --- AcWing/144/144.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 AcWing/144/144.cpp diff --git a/AcWing/144/144.cpp b/AcWing/144/144.cpp new file mode 100644 index 00000000..39d35428 --- /dev/null +++ b/AcWing/144/144.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +int n, u, v, w, a[100005], ans; +vector> g[100005]; + +struct node { + int size; + node* next[2]; + + ~node() { + for (auto& i : next) { + delete i; + } + } +}; + +void dfs(int u, int fa, int sum) { + a[u] = sum; + for (auto i : g[u]) { + if (i.first != fa) { + dfs(i.first, u, sum ^ i.second); + } + } +} + +void insert(node* root, int x, int b) { + if (!x) { + root->size++; + return; + } + if (root->next[(x >> b) & 1] == nullptr) root->next[(x >> b) & 1] = new node(); + insert(root->next[(x >> b) & 1], x - (((x >> b) & 1) << b), b - 1); +} + +int query(node* root, int x) { + int res = 0; + for (int i = 31; i >= 0; i--) { + if (root == nullptr) break; + if (root->next[!((x >> i) & 1)] != nullptr) { + res += 1 << i; + root = root->next[!((x >> i) & 1)]; + } else { + root = root->next[(x >> i) & 1]; + } + } + return res; +} + +int main() { + node* root = new node(); + cin >> n; + for (int i = 1; i < n; i++) { + cin >> u >> v >> w; + g[u].push_back(make_pair(v, w)); + g[v].push_back(make_pair(u, w)); + } + dfs(0, -1, 0); + for (int i = 0; i < n; i++) { + insert(root, a[i], 31); + } + for (int i = 0; i < n; i++) { + ans = max(ans, query(root, a[i])); + } + cout << ans << endl; + return 0; +}