12492022-03-28 22:21:15k_balintMultiplikátoros telebabrátorcpp14Time limit exceeded 20/100575ms358592 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; 
    }
};

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];
vector<pair<int,int>> mp;

void dfs(int v, int p){
    mp.emplace_back(make_pair(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);
        }
    }
}

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);
    sort(mp.begin(),mp.end());
    for(int i=1;i<=n;i++){
        int cur=maxi(d[i]);
        auto it=lower_bound(mp.begin(),mp.end(),make_pair(cur,-1));
        cout << it->second << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted356ms356856 KiB
2Accepted300ms357204 KiB
subtask220/20
3Accepted293ms357264 KiB
4Accepted356ms357328 KiB
5Accepted282ms357368 KiB
6Accepted282ms357328 KiB
7Accepted287ms357464 KiB
8Accepted333ms357488 KiB
9Accepted291ms357452 KiB
10Accepted367ms357652 KiB
11Accepted300ms357428 KiB
12Accepted333ms358492 KiB
13Accepted305ms358592 KiB
14Accepted335ms358384 KiB
15Accepted317ms358452 KiB
16Accepted296ms358416 KiB
subtask30/80
17Time limit exceeded513ms185476 KiB
18Time limit exceeded524ms186284 KiB
19Time limit exceeded535ms184952 KiB
20Time limit exceeded528ms185356 KiB
21Time limit exceeded542ms186404 KiB
22Time limit exceeded515ms185604 KiB
23Time limit exceeded560ms185280 KiB
24Time limit exceeded547ms185796 KiB
25Time limit exceeded527ms185032 KiB
26Time limit exceeded570ms185864 KiB
27Time limit exceeded517ms186660 KiB
28Time limit exceeded532ms185384 KiB
29Time limit exceeded521ms185276 KiB
30Time limit exceeded564ms186424 KiB
31Time limit exceeded521ms186132 KiB
32Time limit exceeded565ms188172 KiB
33Time limit exceeded575ms187344 KiB
34Time limit exceeded519ms190848 KiB
35Time limit exceeded522ms185332 KiB
36Time limit exceeded560ms186028 KiB
37Time limit exceeded526ms187128 KiB
38Time limit exceeded560ms186116 KiB
39Time limit exceeded527ms185732 KiB
40Time limit exceeded541ms187424 KiB