53142023-04-25 20:10:59CattMultiplikátoros telebabrátorcpp17Time limit exceeded 20/100587ms215308 KiB
#include <bits/stdc++.h>
using namespace std;

const int k = 31;

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
1Accepted3ms1828 KiB
2Accepted10ms8352 KiB
subtask220/20
3Accepted9ms8564 KiB
4Accepted9ms8784 KiB
5Accepted9ms8732 KiB
6Accepted9ms8828 KiB
7Accepted9ms8988 KiB
8Accepted9ms8956 KiB
9Accepted9ms9216 KiB
10Accepted9ms9128 KiB
11Accepted9ms9136 KiB
12Accepted9ms9492 KiB
13Accepted9ms9744 KiB
14Accepted9ms9868 KiB
15Accepted8ms9708 KiB
16Accepted9ms9696 KiB
subtask30/80
17Time limit exceeded561ms107684 KiB
18Accepted463ms213452 KiB
19Time limit exceeded574ms108016 KiB
20Accepted463ms213660 KiB
21Accepted458ms213864 KiB
22Time limit exceeded564ms108544 KiB
23Time limit exceeded564ms108440 KiB
24Time limit exceeded563ms108432 KiB
25Accepted469ms213764 KiB
26Time limit exceeded564ms108664 KiB
27Time limit exceeded570ms108788 KiB
28Accepted463ms214568 KiB
29Accepted467ms214084 KiB
30Time limit exceeded573ms108700 KiB
31Time limit exceeded565ms108848 KiB
32Time limit exceeded587ms109444 KiB
33Time limit exceeded574ms110156 KiB
34Time limit exceeded555ms112296 KiB
35Time limit exceeded559ms109516 KiB
36Time limit exceeded561ms215308 KiB
37Time limit exceeded574ms109032 KiB
38Accepted479ms214820 KiB
39Accepted460ms214576 KiB
40Accepted446ms214576 KiB