12502022-03-28 22:27:38k_balintMultiplikátoros telebabrátorcpp14Time limit exceeded 20/100611ms357856 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;
    }
};

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

node trie[32*c];
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].to[j]==-1){
            trie[cur].to[j]=sz++;
        }
        cur=trie[cur].to[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].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];
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
1Accepted370ms357020 KiB
2Accepted377ms357460 KiB
subtask220/20
3Accepted323ms357484 KiB
4Accepted277ms357348 KiB
5Accepted319ms357308 KiB
6Accepted284ms357452 KiB
7Accepted287ms357456 KiB
8Accepted293ms357488 KiB
9Accepted270ms357524 KiB
10Accepted275ms357632 KiB
11Accepted277ms357580 KiB
12Accepted282ms357696 KiB
13Accepted284ms357760 KiB
14Accepted277ms357752 KiB
15Accepted296ms357856 KiB
16Accepted303ms357772 KiB
subtask30/80
17Time limit exceeded561ms188232 KiB
18Time limit exceeded589ms190312 KiB
19Time limit exceeded528ms192324 KiB
20Time limit exceeded513ms193340 KiB
21Time limit exceeded595ms191140 KiB
22Time limit exceeded546ms193220 KiB
23Time limit exceeded597ms194444 KiB
24Time limit exceeded531ms194304 KiB
25Time limit exceeded523ms189336 KiB
26Time limit exceeded611ms191700 KiB
27Time limit exceeded598ms193708 KiB
28Time limit exceeded597ms195656 KiB
29Time limit exceeded569ms192252 KiB
30Time limit exceeded523ms192136 KiB
31Time limit exceeded521ms186848 KiB
32Time limit exceeded522ms186552 KiB
33Time limit exceeded537ms188160 KiB
34Time limit exceeded533ms192968 KiB
35Time limit exceeded523ms189312 KiB
36Time limit exceeded569ms189072 KiB
37Time limit exceeded560ms187584 KiB
38Time limit exceeded532ms187548 KiB
39Time limit exceeded528ms187592 KiB
40Time limit exceeded513ms187016 KiB