5095 | 2023-04-16 16:29:05 | szil | Multiplikátoros telebabrátor | cpp14 | Accepted 100/100 | 379ms | 122232 KiB |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int MAXN = 200001;
vector<pair<int, int>> g[MAXN];
int d[MAXN];
struct Node {
Node *a[2];
int u = -1;
Node() {
a[0] = a[1] = nullptr;
}
};
Node *root;
void add(int u) {
Node *cur = root;
for (int i = 31; i >= 0; i--) {
int x = (d[u] >> i) & 1;
if (cur->a[x] == nullptr) cur->a[x] = new Node();
cur = cur->a[x];
}
cur->u = u;
}
void dfs(int u, int p = 0) {
add(u);
for (auto [v, w] : g[u]) {
if (v != p) {
d[v] = d[u] ^ w;
dfs(v, u);
}
}
}
void solve() {
root = new Node();
int n; cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, w; cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
dfs(1);
for (int i = 1; i <= n; i++) {
Node *cur = root;
for (int j = 31; j >= 0; j--) {
int x = (d[i] >> j) & 1;
if (cur->a[!x] != nullptr) cur = cur->a[!x];
else cur = cur->a[x];
}
cout << cur->u << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 6ms | 11148 KiB | ||||
2 | Accepted | 10ms | 14108 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 9ms | 14332 KiB | ||||
4 | Accepted | 9ms | 14532 KiB | ||||
5 | Accepted | 9ms | 14744 KiB | ||||
6 | Accepted | 10ms | 14956 KiB | ||||
7 | Accepted | 9ms | 15280 KiB | ||||
8 | Accepted | 9ms | 15228 KiB | ||||
9 | Accepted | 9ms | 15416 KiB | ||||
10 | Accepted | 9ms | 15324 KiB | ||||
11 | Accepted | 9ms | 15632 KiB | ||||
12 | Accepted | 10ms | 15852 KiB | ||||
13 | Accepted | 9ms | 15940 KiB | ||||
14 | Accepted | 9ms | 16020 KiB | ||||
15 | Accepted | 9ms | 16048 KiB | ||||
16 | Accepted | 9ms | 16040 KiB | ||||
subtask3 | 80/80 | ||||||
17 | Accepted | 254ms | 112324 KiB | ||||
18 | Accepted | 314ms | 112312 KiB | ||||
19 | Accepted | 314ms | 112328 KiB | ||||
20 | Accepted | 256ms | 112436 KiB | ||||
21 | Accepted | 314ms | 112396 KiB | ||||
22 | Accepted | 312ms | 112416 KiB | ||||
23 | Accepted | 314ms | 112364 KiB | ||||
24 | Accepted | 307ms | 112352 KiB | ||||
25 | Accepted | 317ms | 112224 KiB | ||||
26 | Accepted | 310ms | 112544 KiB | ||||
27 | Accepted | 252ms | 112656 KiB | ||||
28 | Accepted | 308ms | 112668 KiB | ||||
29 | Accepted | 317ms | 112008 KiB | ||||
30 | Accepted | 314ms | 112256 KiB | ||||
31 | Accepted | 280ms | 112652 KiB | ||||
32 | Accepted | 379ms | 114528 KiB | ||||
33 | Accepted | 370ms | 116516 KiB | ||||
34 | Accepted | 365ms | 122232 KiB | ||||
35 | Accepted | 270ms | 114016 KiB | ||||
36 | Accepted | 263ms | 113824 KiB | ||||
37 | Accepted | 266ms | 113568 KiB | ||||
38 | Accepted | 243ms | 113460 KiB | ||||
39 | Accepted | 237ms | 113248 KiB | ||||
40 | Accepted | 238ms | 112900 KiB |