12512022-03-28 22:34:48k_balintMultiplikátoros telebabrátorcpp14Accepted 100/100421ms98588 KiB
#include <bits/stdc++.h>
using namespace std;
const int c=1e5+5;
const int K=30;

struct edge{
    int v,w;
    edge(int vv, int ww){
        v=vv; w=ww;
    }
    edge(){
        v=w=0;
    }
};
int trie[31*c][2];
int sz=1;

inline void add(int k){
    int cur=0;
    for(int i=K-1;i>=0;i--){
        int j=(k>>i)&1;
        if(!trie[cur][j]){
            trie[cur][j]=sz++;
        }
        cur=trie[cur][j];
    }
}

inline 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][j^1]!=0){
            res |= 1<<i;
            cur=trie[cur][j^1];
        }
        else cur=trie[cur][j];
    }
    return res^k;
}

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

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

inline int read(){
    int res=0; char ch=getchar();
    while(!isdigit(ch)){
        ch=getchar();
    }

    while(isdigit(ch)){
        res=(res<<3)+(res<<1)+ch-'0';
        ch=getchar();
    }

    return res;
}

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

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

    dfs(1,0);
    
    for(int i=1;i<=n;i++){
        mp[d[i]]=i;
        add(d[i]);
    }

    for(int i=1;i<=n;i++){
        int cur=maxi(d[i]);
        cout << mp[cur] << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6600 KiB
2Accepted6ms7660 KiB
subtask220/20
3Accepted6ms7636 KiB
4Accepted7ms7676 KiB
5Accepted6ms7724 KiB
6Accepted7ms7760 KiB
7Accepted8ms7800 KiB
8Accepted8ms7848 KiB
9Accepted6ms7880 KiB
10Accepted6ms7928 KiB
11Accepted6ms7972 KiB
12Accepted6ms8148 KiB
13Accepted6ms8236 KiB
14Accepted6ms8228 KiB
15Accepted6ms8136 KiB
16Accepted6ms8164 KiB
subtask380/80
17Accepted368ms49732 KiB
18Accepted275ms51836 KiB
19Accepted277ms53972 KiB
20Accepted298ms56108 KiB
21Accepted296ms58228 KiB
22Accepted291ms60352 KiB
23Accepted335ms62472 KiB
24Accepted286ms64584 KiB
25Accepted291ms66604 KiB
26Accepted286ms69036 KiB
27Accepted305ms71148 KiB
28Accepted282ms73268 KiB
29Accepted284ms75012 KiB
30Accepted421ms77140 KiB
31Accepted314ms79476 KiB
32Accepted300ms83464 KiB
33Accepted349ms87584 KiB
34Accepted331ms95100 KiB
35Accepted308ms88988 KiB
36Accepted287ms90944 KiB
37Accepted287ms92668 KiB
38Accepted275ms94836 KiB
39Accepted272ms96704 KiB
40Accepted310ms98588 KiB