13062022-04-18 16:58:56Valaki2Multiplikátoros telebabrátorcpp14Accepted 100/100246ms111320 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6628 KiB
2Accepted7ms8576 KiB
subtask220/20
3Accepted7ms8620 KiB
4Accepted6ms8680 KiB
5Accepted7ms8708 KiB
6Accepted6ms8756 KiB
7Accepted6ms8796 KiB
8Accepted7ms8844 KiB
9Accepted7ms8868 KiB
10Accepted6ms8912 KiB
11Accepted6ms8960 KiB
12Accepted7ms9016 KiB
13Accepted6ms9236 KiB
14Accepted6ms9072 KiB
15Accepted6ms9144 KiB
16Accepted7ms9160 KiB
subtask380/80
17Accepted246ms66876 KiB
18Accepted232ms68992 KiB
19Accepted237ms71116 KiB
20Accepted221ms73268 KiB
21Accepted229ms75332 KiB
22Accepted224ms77484 KiB
23Accepted209ms79584 KiB
24Accepted188ms81688 KiB
25Accepted200ms83776 KiB
26Accepted189ms86176 KiB
27Accepted187ms88324 KiB
28Accepted196ms90016 KiB
29Accepted201ms90248 KiB
30Accepted209ms92392 KiB
31Accepted217ms94648 KiB
32Accepted216ms98728 KiB
33Accepted224ms102824 KiB
34Accepted224ms109136 KiB
35Accepted195ms103044 KiB
36Accepted193ms103596 KiB
37Accepted216ms103916 KiB
38Accepted197ms105972 KiB
39Accepted199ms108028 KiB
40Accepted185ms111320 KiB