50952023-04-16 16:29:05szilMultiplikátoros telebabrátorcpp14Accepted 100/100379ms122232 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted6ms11148 KiB
2Accepted10ms14108 KiB
subtask220/20
3Accepted9ms14332 KiB
4Accepted9ms14532 KiB
5Accepted9ms14744 KiB
6Accepted10ms14956 KiB
7Accepted9ms15280 KiB
8Accepted9ms15228 KiB
9Accepted9ms15416 KiB
10Accepted9ms15324 KiB
11Accepted9ms15632 KiB
12Accepted10ms15852 KiB
13Accepted9ms15940 KiB
14Accepted9ms16020 KiB
15Accepted9ms16048 KiB
16Accepted9ms16040 KiB
subtask380/80
17Accepted254ms112324 KiB
18Accepted314ms112312 KiB
19Accepted314ms112328 KiB
20Accepted256ms112436 KiB
21Accepted314ms112396 KiB
22Accepted312ms112416 KiB
23Accepted314ms112364 KiB
24Accepted307ms112352 KiB
25Accepted317ms112224 KiB
26Accepted310ms112544 KiB
27Accepted252ms112656 KiB
28Accepted308ms112668 KiB
29Accepted317ms112008 KiB
30Accepted314ms112256 KiB
31Accepted280ms112652 KiB
32Accepted379ms114528 KiB
33Accepted370ms116516 KiB
34Accepted365ms122232 KiB
35Accepted270ms114016 KiB
36Accepted263ms113824 KiB
37Accepted266ms113568 KiB
38Accepted243ms113460 KiB
39Accepted237ms113248 KiB
40Accepted238ms112900 KiB