-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathE.cpp
128 lines (102 loc) · 2.51 KB
/
E.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
124
125
126
127
128
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair <ll, ll>
vector <ll> graph[100009];
ll col[100009], sz[100009], TM = 1, st[100009], en[100009], nodeOf[400009], cnt[100009];
ll ans[100009];
void szdfs(ll u, ll p)
{
st[u] = TM;
nodeOf[TM] = u;
TM++;
sz[u] = 1;
for(ll i = 0; i < graph[u].size(); i++) {
ll nd = graph[u][i];
if(nd == p)
continue;
szdfs(nd, u);
sz[u] += sz[nd];
}
en[u] = TM++;
}
pii dfs(ll u, ll p, bool keep)
{
ll bigchild = -1, mx = -1;
for(ll i = 0; i < graph[u].size(); i++) {
ll nd = graph[u][i];
if(nd != p && sz[nd] > mx) {
mx = sz[nd];
bigchild = nd;
}
}
for(ll i = 0; i < graph[u].size(); i++) {
ll nd = graph[u][i];
if(nd != p && nd != bigchild)
dfs(nd, u, 0);
}
pii tmp = make_pair(0, 0);
if(bigchild != -1)
tmp = dfs(bigchild, u, 1);
ll sum = 0, curmx = 0;
for(ll i = 0; i < graph[u].size(); i++) {
ll nd = graph[u][i];
if(nd != p && nd != bigchild) {
for(ll t = st[nd]; t < en[nd]; t++) {
if(nodeOf[t] == 0)
continue;
cnt[ col[ nodeOf[t] ] ]++;
if(cnt[ col[ nodeOf[t] ] ] > curmx) {
curmx = cnt[ col[ nodeOf[t] ] ];
sum = col[ nodeOf[t] ];
}
else if(cnt[ col[ nodeOf[t] ] ] == curmx)
sum += col[ nodeOf[t] ];
}
}
}
cnt[ col[u] ]++;
if(cnt[ col[u] ] > curmx) {
curmx = cnt[ col[u] ];
sum = col[u];
}
else if(cnt[ col[u] ] == curmx)
sum += col[u];
if(tmp.first > curmx) {
sum = tmp.second;
curmx = tmp.first;
}
else if(tmp.first == curmx)
sum += tmp.second;
ans[u] = sum;
if(keep == 0) {
for(ll t = st[u]; t < en[u]; t++)
cnt[ col[ nodeOf[t] ] ]--;
}
return make_pair(curmx, sum);
}
int main()
{
ll n;
cin >> n;
for(ll i = 1; i <= n; i++) {
ll c;
scanf("%lld", &c);
col[i] = c;
}
for(ll i = 1; i < n; i++) {
ll u, v;
scanf("%lld %lld", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
szdfs(1, -1);
dfs(1, -1, 0);
for(ll i = 1; i <= n; i++) {
if(i != 1)
printf(" ");
printf("%lld", ans[i]);
}
cout << endl;
return 0;
}