1306 | 2022-04-18 16:58:56 | Valaki2 | Multiplikátoros telebabrátor | cpp14 | Accepted 100/100 | 246ms | 111320 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int maxn = 1e5;
const int bits = 30;
int n;
vector<pair<int, int> > g[maxn + 1];
int dist[maxn + 1];
void dfs(int cur, int par) {
for(pair<int, int> p : g[cur]) {
if(p.fi != par) {
dist[p.fi] = dist[cur] ^ p.se;
dfs(p.fi, cur);
}
}
}
struct node {
int to[2];
int val;
node() {
to[0] = to[1] = val = -1;
}
};
vector<node> trie;
void add(int x, int idx) {
int cur = 0;
for(int b = bits - 1; b >= 0; b--) {
bool c = (x >> b) & 1;
if(trie[cur].to[c] == -1) {
trie[cur].to[c] = trie.size();
trie.pb(node());
}
cur = trie[cur].to[c];
}
trie[cur].val = idx;
}
int best(int x) {
int cur = 0;
for(int b = bits - 1; b >= 0; b--) {
bool c = (x >> b) & 1;
if(trie[cur].to[!c] == -1) {
cur = trie[cur].to[c];
} else {
cur = trie[cur].to[!c];
}
}
return trie[cur].val;
}
void solve() {
cin >> n;
for(int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
g[a].pb(mp(b, c));
g[b].pb(mp(a, c));
}
dfs(1, 0);
trie.assign(1, node());
for(int i = 1; i <= n; i++) {
add(dist[i], i);
}
for(int i = 1; i <= n; i++) {
cout << best(dist[i]) << " ";
}
cout << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 6628 KiB | ||||
2 | Accepted | 7ms | 8576 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 7ms | 8620 KiB | ||||
4 | Accepted | 6ms | 8680 KiB | ||||
5 | Accepted | 7ms | 8708 KiB | ||||
6 | Accepted | 6ms | 8756 KiB | ||||
7 | Accepted | 6ms | 8796 KiB | ||||
8 | Accepted | 7ms | 8844 KiB | ||||
9 | Accepted | 7ms | 8868 KiB | ||||
10 | Accepted | 6ms | 8912 KiB | ||||
11 | Accepted | 6ms | 8960 KiB | ||||
12 | Accepted | 7ms | 9016 KiB | ||||
13 | Accepted | 6ms | 9236 KiB | ||||
14 | Accepted | 6ms | 9072 KiB | ||||
15 | Accepted | 6ms | 9144 KiB | ||||
16 | Accepted | 7ms | 9160 KiB | ||||
subtask3 | 80/80 | ||||||
17 | Accepted | 246ms | 66876 KiB | ||||
18 | Accepted | 232ms | 68992 KiB | ||||
19 | Accepted | 237ms | 71116 KiB | ||||
20 | Accepted | 221ms | 73268 KiB | ||||
21 | Accepted | 229ms | 75332 KiB | ||||
22 | Accepted | 224ms | 77484 KiB | ||||
23 | Accepted | 209ms | 79584 KiB | ||||
24 | Accepted | 188ms | 81688 KiB | ||||
25 | Accepted | 200ms | 83776 KiB | ||||
26 | Accepted | 189ms | 86176 KiB | ||||
27 | Accepted | 187ms | 88324 KiB | ||||
28 | Accepted | 196ms | 90016 KiB | ||||
29 | Accepted | 201ms | 90248 KiB | ||||
30 | Accepted | 209ms | 92392 KiB | ||||
31 | Accepted | 217ms | 94648 KiB | ||||
32 | Accepted | 216ms | 98728 KiB | ||||
33 | Accepted | 224ms | 102824 KiB | ||||
34 | Accepted | 224ms | 109136 KiB | ||||
35 | Accepted | 195ms | 103044 KiB | ||||
36 | Accepted | 193ms | 103596 KiB | ||||
37 | Accepted | 216ms | 103916 KiB | ||||
38 | Accepted | 197ms | 105972 KiB | ||||
39 | Accepted | 199ms | 108028 KiB | ||||
40 | Accepted | 185ms | 111320 KiB |