50932023-04-16 16:23:39szilMultiplikátoros telebabrátorcpp14Wrong answer 0/100358ms125212 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 = 29; 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 = 29; 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
1Accepted7ms11320 KiB
2Accepted10ms14336 KiB
subtask20/20
3Wrong answer9ms14480 KiB
4Accepted10ms14432 KiB
5Wrong answer10ms14428 KiB
6Accepted9ms14688 KiB
7Wrong answer10ms14884 KiB
8Wrong answer9ms15112 KiB
9Accepted9ms15216 KiB
10Accepted9ms15168 KiB
11Accepted9ms15184 KiB
12Wrong answer9ms15456 KiB
13Accepted9ms15600 KiB
14Wrong answer9ms15656 KiB
15Wrong answer9ms15620 KiB
16Wrong answer9ms15592 KiB
subtask30/80
17Wrong answer254ms111960 KiB
18Accepted312ms112464 KiB
19Wrong answer330ms112412 KiB
20Accepted314ms112636 KiB
21Accepted319ms112624 KiB
22Wrong answer261ms112612 KiB
23Accepted259ms112648 KiB
24Accepted256ms112896 KiB
25Wrong answer317ms113036 KiB
26Wrong answer298ms113476 KiB
27Accepted275ms113268 KiB
28Accepted358ms113256 KiB
29Wrong answer314ms112788 KiB
30Wrong answer270ms112924 KiB
31Accepted316ms113320 KiB
32Accepted263ms115612 KiB
33Accepted263ms118088 KiB
34Accepted263ms125212 KiB
35Wrong answer298ms114484 KiB
36Accepted239ms114304 KiB
37Accepted238ms114052 KiB
38Accepted291ms113920 KiB
39Wrong answer237ms113772 KiB
40Wrong answer231ms113180 KiB