12462022-03-28 22:06:55k_balintMultiplikátoros telebabrátorcpp14Time limit exceeded 20/100598ms113208 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];
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
1Accepted4ms6540 KiB
2Accepted13ms11976 KiB
subtask220/20
3Accepted13ms11912 KiB
4Accepted12ms11960 KiB
5Accepted12ms12000 KiB
6Accepted13ms12036 KiB
7Accepted13ms12076 KiB
8Accepted13ms12120 KiB
9Accepted13ms12160 KiB
10Accepted12ms12196 KiB
11Accepted13ms12244 KiB
12Accepted13ms12296 KiB
13Accepted10ms12480 KiB
14Accepted12ms12504 KiB
15Accepted12ms12544 KiB
16Accepted12ms12440 KiB
subtask30/80
17Time limit exceeded560ms95648 KiB
18Time limit exceeded593ms97668 KiB
19Time limit exceeded517ms99748 KiB
20Time limit exceeded509ms101972 KiB
21Time limit exceeded536ms104100 KiB
22Time limit exceeded536ms106164 KiB
23Time limit exceeded535ms108320 KiB
24Time limit exceeded564ms110404 KiB
25Time limit exceeded552ms112480 KiB
26Time limit exceeded509ms103712 KiB
27Time limit exceeded523ms98824 KiB
28Time limit exceeded531ms98784 KiB
29Time limit exceeded509ms99580 KiB
30Time limit exceeded521ms101620 KiB
31Time limit exceeded518ms103880 KiB
32Time limit exceeded542ms101608 KiB
33Time limit exceeded508ms101180 KiB
34Time limit exceeded514ms105120 KiB
35Time limit exceeded528ms103960 KiB
36Time limit exceeded595ms106092 KiB
37Time limit exceeded595ms108060 KiB
38Time limit exceeded598ms110192 KiB
39Time limit exceeded589ms112172 KiB
40Time limit exceeded577ms113208 KiB