50942023-04-16 16:25:26szilMultiplikátoros telebabrátorcpp14Wrong answer 0/100333ms125296 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) {
    for (auto [v, w] : g[u]) {
        if (v != p) {
            d[v] = d[u] ^ w;
            add(v);
            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
1Accepted7ms11196 KiB
2Accepted9ms14212 KiB
subtask20/20
3Wrong answer9ms14424 KiB
4Accepted9ms14728 KiB
5Wrong answer10ms14768 KiB
6Accepted9ms14972 KiB
7Wrong answer9ms15284 KiB
8Wrong answer9ms15144 KiB
9Accepted9ms15396 KiB
10Accepted9ms15604 KiB
11Accepted9ms15928 KiB
12Wrong answer10ms16148 KiB
13Accepted10ms16492 KiB
14Wrong answer9ms16732 KiB
15Wrong answer9ms16736 KiB
16Wrong answer8ms16712 KiB
subtask30/80
17Wrong answer312ms113092 KiB
18Accepted256ms113060 KiB
19Wrong answer254ms112904 KiB
20Accepted252ms112928 KiB
21Accepted312ms112988 KiB
22Wrong answer310ms112976 KiB
23Accepted310ms113012 KiB
24Accepted256ms113032 KiB
25Wrong answer256ms112980 KiB
26Wrong answer307ms113256 KiB
27Accepted256ms113284 KiB
28Accepted252ms113132 KiB
29Wrong answer263ms112652 KiB
30Wrong answer312ms113012 KiB
31Accepted314ms113268 KiB
32Accepted319ms115688 KiB
33Accepted324ms118172 KiB
34Accepted326ms125296 KiB
35Wrong answer254ms114444 KiB
36Accepted245ms114260 KiB
37Accepted241ms113980 KiB
38Accepted296ms113880 KiB
39Wrong answer291ms113720 KiB
40Wrong answer333ms113072 KiB