5316 | 2023-04-25 20:12:41 | Catt | Multiplikátoros telebabrátor | cpp17 | Time limit exceeded 20/100 | 591ms | 221116 KiB |
#include <bits/stdc++.h>
using namespace std;
const int k = 29;
vector<vector<pair<int, int> > > g;
vector<int> d;
void dfs(int x, int p = -1) {
for(pair<int, int> sz : g[x]) {
if(sz.first == p )continue;
d[sz.first] = d[x] ^ sz.second;
dfs(sz.first, x);
}
}
struct node {
vector<int> to;
int end;
node() {
end = -1;
to.assign(2, -1);
}
};
vector<node> trie(1);
void add(int x, int ind) {
int cur = 0;
for(int i = k; i >= 0; i--) {
int to = (((1<<i) & x) != 0);
if(trie[cur].to[to] == -1) {
trie[cur].to[to] = trie.size();
trie.push_back(node());
}
cur = trie[cur].to[to];
}
trie[cur].end = ind;
}
int ask(int x) {
int cur = 0;
for(int i = k; i >= 0; i--) {
int to = (((1<<i) & x) != 0);
if(trie[cur].to[1-to] == -1) {
cur = trie[cur].to[to];
}
else cur = trie[cur].to[1-to];
}
return trie[cur].end;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
int n;
cin >>n;
g.resize(n+1);
d.resize(n+1, 0);
for(int i = 0; i < n-1; i++) {
int x,y,z;
cin >> x >> y >> z;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
dfs(1);
for(int i = 1; i <= n; i++) add(d[i], i);
for(int i = 1; i <= n; i++) cout << ask(d[i]) << ' ';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1700 KiB | ||||
2 | Accepted | 9ms | 8184 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 9ms | 8260 KiB | ||||
4 | Accepted | 9ms | 8324 KiB | ||||
5 | Accepted | 9ms | 8528 KiB | ||||
6 | Accepted | 9ms | 8896 KiB | ||||
7 | Accepted | 10ms | 9052 KiB | ||||
8 | Accepted | 9ms | 9188 KiB | ||||
9 | Accepted | 9ms | 9400 KiB | ||||
10 | Accepted | 10ms | 9628 KiB | ||||
11 | Accepted | 9ms | 9588 KiB | ||||
12 | Accepted | 8ms | 9688 KiB | ||||
13 | Accepted | 8ms | 9612 KiB | ||||
14 | Accepted | 8ms | 9852 KiB | ||||
15 | Accepted | 8ms | 9904 KiB | ||||
16 | Accepted | 8ms | 10032 KiB | ||||
subtask3 | 0/80 | ||||||
17 | Accepted | 456ms | 213492 KiB | ||||
18 | Accepted | 456ms | 213548 KiB | ||||
19 | Time limit exceeded | 560ms | 108244 KiB | ||||
20 | Time limit exceeded | 568ms | 213896 KiB | ||||
21 | Accepted | 456ms | 213952 KiB | ||||
22 | Accepted | 458ms | 213896 KiB | ||||
23 | Accepted | 460ms | 214172 KiB | ||||
24 | Time limit exceeded | 578ms | 108424 KiB | ||||
25 | Accepted | 462ms | 213732 KiB | ||||
26 | Accepted | 479ms | 214164 KiB | ||||
27 | Accepted | 486ms | 214132 KiB | ||||
28 | Time limit exceeded | 582ms | 108544 KiB | ||||
29 | Time limit exceeded | 591ms | 108736 KiB | ||||
30 | Time limit exceeded | 560ms | 108820 KiB | ||||
31 | Time limit exceeded | 517ms | 214364 KiB | ||||
32 | Time limit exceeded | 578ms | 109632 KiB | ||||
33 | Time limit exceeded | 575ms | 110312 KiB | ||||
34 | Time limit exceeded | 509ms | 221116 KiB | ||||
35 | Accepted | 479ms | 215468 KiB | ||||
36 | Accepted | 446ms | 215264 KiB | ||||
37 | Time limit exceeded | 572ms | 109288 KiB | ||||
38 | Accepted | 458ms | 215316 KiB | ||||
39 | Accepted | 449ms | 215028 KiB | ||||
40 | Accepted | 444ms | 214956 KiB |