2832021-06-22 15:43:57mraronMultiplikátoros telebabrátorcpp14Accepted 100/100483ms278768 KiB
#include<bits/stdc++.h>
using namespace std;

const int SIGMA=2;
const int MAXN=30*100100;

struct trie {
	int id=1;
	
	int tree[MAXN][SIGMA];
	int sz[MAXN];
	trie() {
		clear();
	}
	
	void clear() {
		id=1;
		memset(tree,0,sizeof tree);
		memset(sz,0,sizeof sz);
	}
	
	int size() {
		return id;
	}
	
	#define HASH(x) ((x)-'a')
	
	void insert(string& t) {
		int akt=1;
		for(auto i:t) {
			if(tree[akt][HASH(i)]==0) {
				tree[akt][HASH(i)]=++id;
			}
			
			sz[akt]++;
			akt=tree[akt][HASH(i)];
		}
		
		sz[akt]++;
	}
	
	bool query(string &t) {
		int akt=1;
		for(auto i:t) {
			if(sz[tree[akt][HASH(i)]]==0) return false;
			akt=tree[akt][HASH(i)];
		}
		
		return sz[akt]>0;
	}
	
	void remove(string& t) {
		int akt=1;
		for(auto i:t) {
			if(sz[tree[akt][HASH(i)]]==0) {
				return ; //nincs benne
			}
			
			sz[akt]--;
			akt=tree[akt][HASH(i)];
		}
		
		sz[akt]--;
	}
	
	#define BITS 30
	void insert_bits(int x) {
		int akt=1;
		for(int i=BITS;i>=0;i--) {
			int bit=(x&(1<<i))!=0;
			if(tree[akt][bit]==0) {
				tree[akt][bit]=++id;
			}
			sz[akt]++;
			akt=tree[akt][bit];
		}		
		sz[akt]++;
	}
	
	void remove_bits(int x) {
		int akt=1;
		for(int i=BITS;i>=0;i--) {
			int bit=(x&(1<<i))!=0;
			if(tree[akt][bit]==0) {
				return ;
			}
			sz[akt]--;
			akt=tree[akt][bit];
		}		
		sz[akt]--;
	}
	
	int biggest_xor_with(int x) {
		int opt=0;
		int akt=1;
		for(int i=BITS;i>=0;i--){
			int bit=(x&(1<<i))!=0;
			if(bit==1 && sz[tree[akt][0]]) {
				akt=tree[akt][0];
			}else if(bit==0 && sz[tree[akt][1]]) {
				opt|=1<<i;
				akt=tree[akt][1];
			}else {
				if(sz[tree[akt][0]]) akt=tree[akt][0];
				else if(sz[tree[akt][1]]) {
					opt|=1<<i;
					akt=tree[akt][1];
				}
				else return x;
			}
		}
		return opt;
	}
};

vector<pair<int,int>> adj[MAXN];

#define xx first
#define yy second
int dist[MAXN];
int st[MAXN];
void dfs(int x, int akt) {
    dist[x]=akt;
    st[x]=1;
    for(auto i:adj[x]) {
        if(!st[i.xx]) {
            dfs(i.xx, i.yy^akt);
        }
    }
}

trie tr;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n;
    cin>>n;
    for(int i=1;i<n;++i) {
        int a,b,c;
        cin>>a>>b>>c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }
    
    dfs(1,0);
    
    
    map<int,int> inv;
    for(int i=1;i<=n;++i) inv[dist[i]]=i, tr.insert_bits(dist[i]);
    for(int i=1;i<=n;++i) cout<<inv[tr.biggest_xor_with(dist[i])]<<" ";
    cout<<"\n";
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted94ms213092 KiB
2Accepted93ms213684 KiB
subtask220/20
3Accepted94ms213612 KiB
4Accepted135ms213660 KiB
5Accepted94ms213736 KiB
6Accepted94ms213780 KiB
7Accepted94ms213816 KiB
8Accepted93ms213860 KiB
9Accepted103ms213900 KiB
10Accepted103ms213944 KiB
11Accepted103ms213992 KiB
12Accepted97ms214108 KiB
13Accepted96ms214108 KiB
14Accepted94ms214120 KiB
15Accepted96ms214156 KiB
16Accepted96ms214188 KiB
subtask380/80
17Accepted393ms234436 KiB
18Accepted405ms236552 KiB
19Accepted481ms238112 KiB
20Accepted455ms239404 KiB
21Accepted455ms241532 KiB
22Accepted483ms243660 KiB
23Accepted428ms245788 KiB
24Accepted400ms247896 KiB
25Accepted412ms249932 KiB
26Accepted384ms252360 KiB
27Accepted398ms254448 KiB
28Accepted402ms255380 KiB
29Accepted421ms257124 KiB
30Accepted412ms259236 KiB
31Accepted414ms261528 KiB
32Accepted414ms265044 KiB
33Accepted432ms267832 KiB
34Accepted428ms272408 KiB
35Accepted395ms268940 KiB
36Accepted419ms270888 KiB
37Accepted402ms272660 KiB
38Accepted411ms274808 KiB
39Accepted416ms276652 KiB
40Accepted391ms278768 KiB