50922023-04-16 16:21:30szilMultiplikátoros telebabrátorcpp14Wrong answer 0/100331ms125032 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 = 30; 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 = 30; 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
1Accepted7ms11188 KiB
2Accepted9ms14144 KiB
subtask20/20
3Wrong answer9ms14364 KiB
4Accepted10ms14564 KiB
5Wrong answer10ms14776 KiB
6Accepted10ms15096 KiB
7Wrong answer9ms15308 KiB
8Wrong answer9ms15524 KiB
9Accepted9ms15728 KiB
10Accepted9ms15584 KiB
11Accepted10ms15904 KiB
12Wrong answer9ms15960 KiB
13Accepted9ms16152 KiB
14Wrong answer9ms15908 KiB
15Wrong answer9ms15904 KiB
16Wrong answer8ms15884 KiB
subtask30/80
17Wrong answer254ms112264 KiB
18Accepted254ms112236 KiB
19Wrong answer254ms112264 KiB
20Accepted254ms112288 KiB
21Accepted256ms112268 KiB
22Wrong answer254ms112560 KiB
23Accepted256ms112504 KiB
24Accepted254ms112476 KiB
25Wrong answer256ms112372 KiB
26Wrong answer252ms112684 KiB
27Accepted257ms112716 KiB
28Accepted252ms112636 KiB
29Wrong answer261ms112280 KiB
30Wrong answer259ms112344 KiB
31Accepted319ms112896 KiB
32Accepted317ms115436 KiB
33Accepted324ms117920 KiB
34Accepted330ms125032 KiB
35Wrong answer250ms114288 KiB
36Accepted244ms114096 KiB
37Accepted243ms113832 KiB
38Accepted331ms113924 KiB
39Wrong answer331ms113724 KiB
40Wrong answer237ms113132 KiB