-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
123 lines (110 loc) · 2.8 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <algorithm>
#include <iostream>
#include <iterator>
#include <cstring>
#include <climits>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define Re register
#define LL long long
#define U unsigned
#define FOR(i,a,b) for(Re int i = a;i <= b;++i)
#define ROF(i,a,b) for(Re int i = a;i >= b;--i)
#define SFOR(i,a,b,c) for(Re int i = a;i <= b;i+=c)
#define SROF(i,a,b,c) for(Re int i = a;i >= b;i-=c)
#define CLR(i,a) memset(i,a,sizeof(i))
#define BR printf("--------------------\n")
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
#define int LL
const int MAXN = 100000+5;
struct Edge{
int to,w,next;
}e[MAXN<<1];
int head[MAXN],cnt,N,size[MAXN],max[MAXN],dist[MAXN],col[MAXN],sum[MAXN],pre[MAXN];
int zx;
inline void add(int u,int v,int w){
e[++cnt] = (Edge){v,w,head[u]};head[u] = cnt;
}
inline void dfs1(int v,int fa){
size[v] = 1;
for(int i = head[v];i;i = e[i].next){
if(e[i].to == fa) continue;
dfs1(e[i].to,v);
size[v] += size[e[i].to];
max[v] = std::max(max[v],size[e[i].to]);
}
max[v] = std::max(max[v],N-size[v]);
if(max[v] < max[zx]) zx = v;
}
int ans=0;
inline void dfs2(int v){
ans += dist[v];
for(int i = head[v];i;i = e[i].next){
if(e[i].to == pre[v]) continue;
dist[e[i].to] = dist[pre[e[i].to] = v] + e[i].w;
dfs2(e[i].to);
}
}
#define P std::pair<int,int>
#define MP std::make_pair
std::set<int> in[MAXN],min;
std::set<P> S;
inline void find(int x,int root){
in[col[x]=root].insert(x);
for(int i = head[x];i;i = e[i].next){
if(e[i].to == pre[x]) continue;
find(e[i].to,root);
}
}
inline void link(int x,int y){
int fx = col[x],fy = col[y];
min.erase(y);
if(fx){
S.erase(MP(sum[fx],fx));
S.insert(MP(--sum[fx],fx));
}
if(fy){
in[fy].erase(y);
if(!in[fy].empty()) min.insert(*in[fy].begin());
S.erase(MP(sum[fy],fy));
S.insert(MP(--sum[fy],fy));
}
}
inline int solve(int x){
int ans;
if(S.rbegin()->first == N-x+1 && S.rbegin()->second != col[x]){
ans = *in[S.rbegin()->second].begin();
}
else{
ans = (col[*min.begin()] != col[x] || x == zx) ? *min.begin() : *std::next(min.begin());
}
link(x,ans);
return ans;
}
signed main(){
scanf("%I64d",&N);max[zx] = LLONG_MAX;
FOR(i,1,N-1){
int u,v,w;scanf("%I64d%I64d%I64d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
dfs1(1,0);dfs2(zx);
//00 FOR(i,1,N) printf("%d ",max[i]);puts("");
printf("%I64d\n",ans*2ll);
if(N == 1) {puts("1");return 0;}
min.insert(zx);
for(int i = head[zx];i;i = e[i].next){
find(e[i].to,e[i].to);
min.insert(*in[e[i].to].begin());
S.insert(MP(sum[e[i].to] = in[e[i].to].size()*2,e[i].to));
}
FOR(i,1,N) printf("%I64d ",solve(i));
puts("");
return 0;
}