10079 | 2024-03-26 16:40:56 | 111 | Multiplikátoros telebabrátor | cpp17 | Accepted 100/100 | 305ms | 131508 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("be2.txt","r",stdin);
#endif
int N;
cin>>N;
vector<vector<pair<int,int>>>g(N+1);
for(int i=0;i<N-1;i++){
int a,b,c;
cin>>a>>b>>c;
g[a].emplace_back(b,c);
g[b].emplace_back(a,c);
}
vector<int>v(N+1);
auto dfs=[&](auto self,int i,int j)->void{
for(auto [k,c]:g[j]){
if(k==i)continue;
v[k]=v[j]^c;
self(self,j,k);
}
};
dfs(dfs,0,1);
vector<array<int,2>>t(1);
vector<int>r(1,1);
for(int i=1;i<=N;i++){
int y=0;
for(int j=31;j>=0;j--){
int b=(v[i]>>j&1)^1;
if(!t[y][b]){
t[y][b]=t.size();
t.emplace_back();
r.push_back(i);
}
y=t[y][b];
}
}
vector<int>ans(N+1);
ans[1]=max_element(v.begin()+1,v.end())-v.begin();
int x=0;
auto dfs2=[&](auto self,int i,int j)->void{
for(auto [k,c]:g[j]){
if(k==i)continue;
x^=c;
int y=0;
for(int j=31;j>=0;j--){
int b=x>>j&1;
if(t[y][b]){
y=t[y][b];
continue;
}
if(t[y][b^1]){
y=t[y][b^1];
continue;
}
break;
}
ans[k]=r[y];
self(self,j,k);
x^=c;
}
};
dfs2(dfs2,0,1);
for(int i=1;i<=N;i++){
cout<<ans[i]<<' ';
}
cout<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 7ms | 5288 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 7ms | 5492 KiB | ||||
4 | Accepted | 6ms | 5688 KiB | ||||
5 | Accepted | 7ms | 5880 KiB | ||||
6 | Accepted | 7ms | 5836 KiB | ||||
7 | Accepted | 6ms | 6192 KiB | ||||
8 | Accepted | 7ms | 6068 KiB | ||||
9 | Accepted | 6ms | 6144 KiB | ||||
10 | Accepted | 7ms | 6456 KiB | ||||
11 | Accepted | 7ms | 6560 KiB | ||||
12 | Accepted | 6ms | 6776 KiB | ||||
13 | Accepted | 7ms | 6960 KiB | ||||
14 | Accepted | 6ms | 6844 KiB | ||||
15 | Accepted | 6ms | 6924 KiB | ||||
16 | Accepted | 6ms | 7164 KiB | ||||
subtask3 | 80/80 | ||||||
17 | Accepted | 287ms | 121424 KiB | ||||
18 | Accepted | 233ms | 121496 KiB | ||||
19 | Accepted | 231ms | 121404 KiB | ||||
20 | Accepted | 286ms | 121300 KiB | ||||
21 | Accepted | 237ms | 121420 KiB | ||||
22 | Accepted | 289ms | 121596 KiB | ||||
23 | Accepted | 233ms | 121572 KiB | ||||
24 | Accepted | 287ms | 121828 KiB | ||||
25 | Accepted | 234ms | 122120 KiB | ||||
26 | Accepted | 287ms | 122272 KiB | ||||
27 | Accepted | 234ms | 122324 KiB | ||||
28 | Accepted | 287ms | 122284 KiB | ||||
29 | Accepted | 238ms | 122336 KiB | ||||
30 | Accepted | 233ms | 122312 KiB | ||||
31 | Accepted | 293ms | 122568 KiB | ||||
32 | Accepted | 238ms | 124384 KiB | ||||
33 | Accepted | 240ms | 126276 KiB | ||||
34 | Accepted | 305ms | 131508 KiB | ||||
35 | Accepted | 231ms | 122756 KiB | ||||
36 | Accepted | 277ms | 122360 KiB | ||||
37 | Accepted | 273ms | 122012 KiB | ||||
38 | Accepted | 268ms | 121448 KiB | ||||
39 | Accepted | 221ms | 121540 KiB | ||||
40 | Accepted | 216ms | 121004 KiB |