12482022-03-28 22:13:55k_balintMultiplikátoros telebabrátorcpp14Time limit exceeded 20/100596ms128432 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.emplace_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];
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);
        }
    }
}

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);
    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
1Accepted4ms6564 KiB
2Accepted13ms11900 KiB
subtask220/20
3Accepted10ms11872 KiB
4Accepted12ms11892 KiB
5Accepted10ms11988 KiB
6Accepted10ms12060 KiB
7Accepted12ms12056 KiB
8Accepted12ms12156 KiB
9Accepted12ms12124 KiB
10Accepted10ms12192 KiB
11Accepted12ms12212 KiB
12Accepted12ms12296 KiB
13Accepted12ms12412 KiB
14Accepted12ms12372 KiB
15Accepted12ms12448 KiB
16Accepted10ms12436 KiB
subtask30/80
17Time limit exceeded569ms91492 KiB
18Time limit exceeded560ms93588 KiB
19Time limit exceeded513ms95724 KiB
20Time limit exceeded584ms97824 KiB
21Time limit exceeded578ms100028 KiB
22Time limit exceeded589ms102060 KiB
23Time limit exceeded538ms104180 KiB
24Time limit exceeded505ms106300 KiB
25Time limit exceeded577ms108528 KiB
26Time limit exceeded591ms110708 KiB
27Time limit exceeded593ms112892 KiB
28Time limit exceeded569ms114896 KiB
29Time limit exceeded582ms116732 KiB
30Time limit exceeded583ms118980 KiB
31Time limit exceeded588ms121184 KiB
32Time limit exceeded563ms124444 KiB
33Time limit exceeded566ms122440 KiB
34Time limit exceeded593ms124436 KiB
35Time limit exceeded595ms122168 KiB
36Time limit exceeded596ms124420 KiB
37Time limit exceeded591ms126336 KiB
38Time limit exceeded574ms128400 KiB
39Time limit exceeded588ms128432 KiB
40Time limit exceeded584ms123580 KiB