53132023-04-25 20:10:21CattMultiplikátoros telebabrátorcpp17Time limit exceeded 20/100586ms223656 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() {
    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
1Accepted3ms1816 KiB
2Accepted10ms8292 KiB
subtask220/20
3Accepted12ms8660 KiB
4Accepted10ms8796 KiB
5Accepted12ms8792 KiB
6Accepted12ms9092 KiB
7Accepted12ms9340 KiB
8Accepted10ms9332 KiB
9Accepted10ms9380 KiB
10Accepted12ms9428 KiB
11Accepted12ms9716 KiB
12Accepted12ms10100 KiB
13Accepted12ms10268 KiB
14Accepted12ms10388 KiB
15Accepted10ms10556 KiB
16Accepted12ms11052 KiB
subtask30/80
17Time limit exceeded551ms216368 KiB
18Time limit exceeded570ms112980 KiB
19Time limit exceeded555ms220580 KiB
20Time limit exceeded565ms220532 KiB
21Time limit exceeded578ms115292 KiB
22Time limit exceeded544ms220876 KiB
23Time limit exceeded542ms220880 KiB
24Time limit exceeded586ms115352 KiB
25Time limit exceeded555ms220632 KiB
26Time limit exceeded545ms220908 KiB
27Time limit exceeded542ms220904 KiB
28Time limit exceeded550ms220908 KiB
29Time limit exceeded552ms220536 KiB
30Time limit exceeded563ms115192 KiB
31Time limit exceeded554ms115196 KiB
32Time limit exceeded561ms222176 KiB
33Time limit exceeded560ms223656 KiB
34Time limit exceeded564ms118904 KiB
35Time limit exceeded538ms222016 KiB
36Time limit exceeded532ms221876 KiB
37Time limit exceeded550ms116128 KiB
38Time limit exceeded550ms221824 KiB
39Time limit exceeded531ms221576 KiB
40Time limit exceeded569ms116004 KiB