50912023-04-16 16:07:26szilMultiplikátoros telebabrátorcpp14Wrong answer 0/100328ms123420 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 100001;

vector<pair<int, int>> g[MAXN];
int d[MAXN];

struct Node {
    Node *a[2] = {nullptr, nullptr};
    int u = -1;

    void add(int i, int n, int v) {
        if (i >= 0) {
            int bit = (n >> i) & 1;
            if (a[bit] == nullptr) {
                a[bit] = new Node();
            }
            a[bit]->add(i-1, n, v);
        } else {
            u = v;
        }
    }

    int qry(int i, int n) {
        if (u != -1) return u;
        int bit = (n >> i) & 1;
        if (a[!bit] != nullptr) {
            return a[!bit]->qry(i-1, n);
        } else {
            assert(a[bit] != nullptr);
            return a[bit]->qry(i-1, n);
        }
    }
};

Node *root;

void dfs(int u, int p = 0) {
    for (auto [v, w] : g[u]) {
        if (v != p) {
            d[v] = d[u] ^ w;
            root->add(31, d[v], 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++) {
        int ans = root->qry(31, d[i]);
        assert(ans != -1);
        cout << ans << " ";
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6672 KiB
2Accepted8ms9492 KiB
subtask20/20
3Wrong answer8ms9724 KiB
4Accepted8ms9948 KiB
5Wrong answer8ms10172 KiB
6Accepted8ms10304 KiB
7Wrong answer8ms10376 KiB
8Wrong answer8ms10492 KiB
9Accepted8ms10704 KiB
10Accepted8ms10924 KiB
11Accepted8ms11284 KiB
12Wrong answer8ms11500 KiB
13Accepted8ms11668 KiB
14Wrong answer8ms11560 KiB
15Wrong answer8ms11732 KiB
16Wrong answer8ms11732 KiB
subtask30/80
17Wrong answer238ms108048 KiB
18Accepted287ms108044 KiB
19Wrong answer279ms108264 KiB
20Accepted233ms108544 KiB
21Accepted238ms108492 KiB
22Wrong answer277ms108476 KiB
23Accepted275ms108504 KiB
24Accepted286ms108692 KiB
25Wrong answer237ms108568 KiB
26Wrong answer282ms109180 KiB
27Accepted233ms109196 KiB
28Accepted287ms109060 KiB
29Wrong answer328ms108588 KiB
30Wrong answer298ms108612 KiB
31Accepted286ms108920 KiB
32Accepted298ms111728 KiB
33Accepted273ms114696 KiB
34Accepted308ms123420 KiB
35Wrong answer264ms109932 KiB
36Accepted268ms109740 KiB
37Accepted257ms109500 KiB
38Accepted216ms109580 KiB
39Wrong answer219ms109272 KiB
40Wrong answer218ms108660 KiB