5314 2023. 04. 25 20:10:59 Catt Multiplikátoros telebabrátor cpp17 Time limit exceeded 20/100 587ms 215308 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;
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 3ms 1828 KiB
2 Accepted 10ms 8352 KiB
subtask2 20/20
3 Accepted 9ms 8564 KiB
4 Accepted 9ms 8784 KiB
5 Accepted 9ms 8732 KiB
6 Accepted 9ms 8828 KiB
7 Accepted 9ms 8988 KiB
8 Accepted 9ms 8956 KiB
9 Accepted 9ms 9216 KiB
10 Accepted 9ms 9128 KiB
11 Accepted 9ms 9136 KiB
12 Accepted 9ms 9492 KiB
13 Accepted 9ms 9744 KiB
14 Accepted 9ms 9868 KiB
15 Accepted 8ms 9708 KiB
16 Accepted 9ms 9696 KiB
subtask3 0/80
17 Time limit exceeded 561ms 107684 KiB
18 Accepted 463ms 213452 KiB
19 Time limit exceeded 574ms 108016 KiB
20 Accepted 463ms 213660 KiB
21 Accepted 458ms 213864 KiB
22 Time limit exceeded 564ms 108544 KiB
23 Time limit exceeded 564ms 108440 KiB
24 Time limit exceeded 563ms 108432 KiB
25 Accepted 469ms 213764 KiB
26 Time limit exceeded 564ms 108664 KiB
27 Time limit exceeded 570ms 108788 KiB
28 Accepted 463ms 214568 KiB
29 Accepted 467ms 214084 KiB
30 Time limit exceeded 573ms 108700 KiB
31 Time limit exceeded 565ms 108848 KiB
32 Time limit exceeded 587ms 109444 KiB
33 Time limit exceeded 574ms 110156 KiB
34 Time limit exceeded 555ms 112296 KiB
35 Time limit exceeded 559ms 109516 KiB
36 Time limit exceeded 561ms 215308 KiB
37 Time limit exceeded 574ms 109032 KiB
38 Accepted 479ms 214820 KiB
39 Accepted 460ms 214576 KiB
40 Accepted 446ms 214576 KiB