53162023-04-25 20:12:41CattMultiplikátoros telebabrátorcpp17Time limit exceeded 20/100591ms221116 KiB
#include <bits/stdc++.h>
using namespace std;

const int k = 29;

vector<vector<pair<int, int> > > g;
vector<int> d;

void dfs(int x, int p = -1) {
    for(pair<int, int> sz : g[x]) {
        if(sz.first == p )continue;
        d[sz.first] = d[x] ^ sz.second;
        dfs(sz.first, x);
    }
}

struct node {
    vector<int> to;
    int end;

    node() {
        end = -1;
        to.assign(2, -1);
    }
};

vector<node> trie(1);

void add(int x, int ind) {
    int cur = 0;
    for(int i = k; i >= 0; i--) {
        int to = (((1<<i) & x) != 0);
        if(trie[cur].to[to] == -1) {
            trie[cur].to[to] = trie.size();
            trie.push_back(node());
        }
        cur = trie[cur].to[to];
    }

    trie[cur].end = ind;
}

int ask(int x) {
    int cur = 0;
    for(int i = k; i >= 0; i--) {
        int to = (((1<<i) & x) != 0);
        if(trie[cur].to[1-to] == -1) {
            cur = trie[cur].to[to];
        }
        else cur = trie[cur].to[1-to];
    }
    return trie[cur].end;
}

int main() {
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    int n;
    cin >>n;

    g.resize(n+1);
    d.resize(n+1, 0);
    for(int i = 0; i < n-1; i++) {
        int x,y,z;
        cin >> x >> y >> z;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }

    dfs(1);
    for(int i = 1; i <= n; i++) add(d[i], i);
    for(int i = 1; i <= n; i++) cout << ask(d[i]) << ' ';

    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1700 KiB
2Accepted9ms8184 KiB
subtask220/20
3Accepted9ms8260 KiB
4Accepted9ms8324 KiB
5Accepted9ms8528 KiB
6Accepted9ms8896 KiB
7Accepted10ms9052 KiB
8Accepted9ms9188 KiB
9Accepted9ms9400 KiB
10Accepted10ms9628 KiB
11Accepted9ms9588 KiB
12Accepted8ms9688 KiB
13Accepted8ms9612 KiB
14Accepted8ms9852 KiB
15Accepted8ms9904 KiB
16Accepted8ms10032 KiB
subtask30/80
17Accepted456ms213492 KiB
18Accepted456ms213548 KiB
19Time limit exceeded560ms108244 KiB
20Time limit exceeded568ms213896 KiB
21Accepted456ms213952 KiB
22Accepted458ms213896 KiB
23Accepted460ms214172 KiB
24Time limit exceeded578ms108424 KiB
25Accepted462ms213732 KiB
26Accepted479ms214164 KiB
27Accepted486ms214132 KiB
28Time limit exceeded582ms108544 KiB
29Time limit exceeded591ms108736 KiB
30Time limit exceeded560ms108820 KiB
31Time limit exceeded517ms214364 KiB
32Time limit exceeded578ms109632 KiB
33Time limit exceeded575ms110312 KiB
34Time limit exceeded509ms221116 KiB
35Accepted479ms215468 KiB
36Accepted446ms215264 KiB
37Time limit exceeded572ms109288 KiB
38Accepted458ms215316 KiB
39Accepted449ms215028 KiB
40Accepted444ms214956 KiB