12472022-03-28 22:08:50k_balintMultiplikátoros telebabrátorcpp14Time limit exceeded 20/100601ms130880 KiB
#include <bits/stdc++.h>
using namespace std;
const int c=1e5+5;
const int K=31;

struct edge{
    int v,w;
    edge(int vv, int ww){
        v=vv; w=ww;
    }
    edge(){
        v=w=0;
    }
};

struct node{
    vector<int> to;
    node(){
        to.resize(2);
        to[0]=to[1]=-1; 
    }
};

vector<node> trie=vector<node>(1);

void add(int k){
    int cur=0;
    for(int i=K-1;i>=0;i--){
        int j=(k>>i)&1;
        if(trie[cur].to[j]==-1){
            trie[cur].to[j]=trie.size();
            trie.push_back(node());
        }
        cur=trie[cur].to[j];
    }
}

int maxi(int k){
    int res=0; int cur=0;
    for(int i=K-1;i>=0;i--){
        int j=(k>>i)&1;
        if(trie[cur].to[j^1]!=-1){
            res |= 1<<i;
            cur=trie[cur].to[j^1];
        }
        else cur=trie[cur].to[j];
    }
    return res^k;
}

vector<edge> adj[c];
int d[c];
unordered_map<int,int> mp;

void dfs(int v, int p){
    mp[d[v]]=v; add(d[v]);
    for(edge x:adj[v]){
        if(x.v != p){
            d[x.v]=d[v]^x.w;
            dfs(x.v,v);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin>>n;
    for(int i=1;i<n;i++){
        int a,b,w; cin>>a>>b>>w;
        adj[a].push_back(edge(b,w));
        adj[b].push_back(edge(a,w));
    }

    dfs(1,0);
    for(int i=1;i<=n;i++){
        cout << mp[maxi(d[i])] << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6580 KiB
2Accepted12ms11860 KiB
subtask220/20
3Accepted12ms11968 KiB
4Accepted12ms12024 KiB
5Accepted12ms12068 KiB
6Accepted14ms12032 KiB
7Accepted12ms12100 KiB
8Accepted12ms12124 KiB
9Accepted10ms12176 KiB
10Accepted10ms12224 KiB
11Accepted10ms12316 KiB
12Accepted13ms12332 KiB
13Accepted10ms12488 KiB
14Accepted12ms12420 KiB
15Accepted10ms12452 KiB
16Accepted13ms12512 KiB
subtask30/80
17Time limit exceeded558ms93656 KiB
18Time limit exceeded547ms95572 KiB
19Time limit exceeded521ms96532 KiB
20Time limit exceeded507ms98564 KiB
21Time limit exceeded513ms100724 KiB
22Time limit exceeded573ms102812 KiB
23Time limit exceeded583ms104900 KiB
24Time limit exceeded596ms107200 KiB
25Time limit exceeded597ms109192 KiB
26Time limit exceeded519ms111116 KiB
27Time limit exceeded527ms108568 KiB
28Time limit exceeded509ms110636 KiB
29Time limit exceeded529ms112528 KiB
30Time limit exceeded592ms114808 KiB
31Time limit exceeded566ms114296 KiB
32Time limit exceeded601ms114636 KiB
33Time limit exceeded597ms117636 KiB
34Time limit exceeded600ms121692 KiB
35Time limit exceeded600ms120612 KiB
36Time limit exceeded596ms122600 KiB
37Time limit exceeded575ms124544 KiB
38Time limit exceeded538ms126724 KiB
39Time limit exceeded579ms128696 KiB
40Time limit exceeded591ms130880 KiB