#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define endl '\n'
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
const int maxn = 100001;
struct BIT{
int bit[maxn];
void clr(int x){
x++;
memset(bit, 0, sizeof(int) * x);
}
void add(int x, int v){
for(x++; x < maxn; x += x & -x) bit[x] += v;
}
int qry(int x){
int ret = 0;
for(x++; x; x -= x & -x) ret += bit[x];
return ret;
}
};
int n;
int col[maxn], hd[maxn], p[maxn], sz[maxn], d[maxn];
pi e[maxn];
vector<int> graph[maxn];
vector<pi> cur[maxn];
BIT bit;
int dfs(int c){
sz[c] = 1;
for(int i : graph[c]){
d[i] = d[c] + 1;
sz[c] += dfs(i);
}
return sz[c];
}
void hld(int c){
if(graph[c].empty()) return;
int b = graph[c][0];
for(int i : graph[c]) if(sz[i] > sz[b]) b = i;
for(int i : graph[c]){
hd[i] = (i == b ? hd[c] : i);
hld(i);
}
}
int solve(int a, int b){
vector<int> id;
vector<pi> val, tmp;
while(a != -1){
int c = d[a] - d[hd[a]] + 1;
tmp.clear();
for(int i = cur[hd[a]].size() - 1; i >= 0; i--){
if(cur[hd[a]][i].s < c){
tmp.pb(cur[hd[a]][i]);
c -= cur[hd[a]][i].s;
}else{
tmp.pb({cur[hd[a]][i].f, c});
break;
}
}
reverse(tmp.begin(), tmp.end());
for(pi i : tmp) val.pb(i), id.pb(i.first);
a = p[hd[a]];
}
sort(id.begin(), id.end());
id.erase(unique(id.begin(), id.end()), id.end());
bit.clr(id.size());
int ret = 0;
for(pi i : val){
int j = lower_bound(id.begin(), id.end(), i.f) - id.begin();
ret += i.s * bit.qry(j - 1);
bit.add(j, i.s);
}
int bcol = col[b];
while(b != -1){
int c = d[b] - d[hd[b]] + 1;
while(!cur[hd[b]].empty()){
if(cur[hd[b]].back().s <= c){
c -= cur[hd[b]].back().s;
cur[hd[b]].pop_back();
}else{
cur[hd[b]].back().s -= c;
break;
}
}
cur[hd[b]].pb({bcol, d[b] - d[hd[b]] + 1});
b = p[hd[b]];
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++) cin >> col[i];
p[0] = -1;
for(int i = 0; i < n - 1; i++){
cin >> e[i].f >> e[i].s;
e[i].f--, e[i].s--;
graph[e[i].f].pb(e[i].s);
p[e[i].s] = e[i].f;
}
dfs(0), hld(0);
cur[0].pb({col[0], 1});
for(int i = 0; i < n - 1; i++) cout << solve(e[i].f, e[i].s) << endl;;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
5116 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5108 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
5116 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
5112 KB |
Output is correct |
21 |
Correct |
6 ms |
5112 KB |
Output is correct |
22 |
Correct |
6 ms |
5112 KB |
Output is correct |
23 |
Correct |
6 ms |
5112 KB |
Output is correct |
24 |
Correct |
6 ms |
5112 KB |
Output is correct |
25 |
Correct |
6 ms |
5112 KB |
Output is correct |
26 |
Correct |
6 ms |
5112 KB |
Output is correct |
27 |
Correct |
6 ms |
5112 KB |
Output is correct |
28 |
Correct |
6 ms |
5112 KB |
Output is correct |
29 |
Correct |
6 ms |
5112 KB |
Output is correct |
30 |
Correct |
6 ms |
5112 KB |
Output is correct |
31 |
Correct |
7 ms |
5112 KB |
Output is correct |
32 |
Correct |
6 ms |
5112 KB |
Output is correct |
33 |
Correct |
6 ms |
5112 KB |
Output is correct |
34 |
Correct |
6 ms |
5112 KB |
Output is correct |
35 |
Correct |
6 ms |
5112 KB |
Output is correct |
36 |
Correct |
6 ms |
5112 KB |
Output is correct |
37 |
Correct |
6 ms |
5116 KB |
Output is correct |
38 |
Correct |
6 ms |
5240 KB |
Output is correct |
39 |
Correct |
6 ms |
5100 KB |
Output is correct |
40 |
Correct |
6 ms |
5116 KB |
Output is correct |
41 |
Correct |
6 ms |
5112 KB |
Output is correct |
42 |
Correct |
6 ms |
5112 KB |
Output is correct |
43 |
Correct |
6 ms |
5112 KB |
Output is correct |
44 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
5116 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5108 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
5116 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
5112 KB |
Output is correct |
21 |
Correct |
6 ms |
5112 KB |
Output is correct |
22 |
Correct |
6 ms |
5112 KB |
Output is correct |
23 |
Correct |
6 ms |
5112 KB |
Output is correct |
24 |
Correct |
6 ms |
5112 KB |
Output is correct |
25 |
Correct |
6 ms |
5112 KB |
Output is correct |
26 |
Correct |
6 ms |
5112 KB |
Output is correct |
27 |
Correct |
6 ms |
5112 KB |
Output is correct |
28 |
Correct |
6 ms |
5112 KB |
Output is correct |
29 |
Correct |
6 ms |
5112 KB |
Output is correct |
30 |
Correct |
6 ms |
5112 KB |
Output is correct |
31 |
Correct |
7 ms |
5112 KB |
Output is correct |
32 |
Correct |
6 ms |
5112 KB |
Output is correct |
33 |
Correct |
6 ms |
5112 KB |
Output is correct |
34 |
Correct |
6 ms |
5112 KB |
Output is correct |
35 |
Correct |
6 ms |
5112 KB |
Output is correct |
36 |
Correct |
6 ms |
5112 KB |
Output is correct |
37 |
Correct |
6 ms |
5116 KB |
Output is correct |
38 |
Correct |
6 ms |
5240 KB |
Output is correct |
39 |
Correct |
6 ms |
5100 KB |
Output is correct |
40 |
Correct |
6 ms |
5116 KB |
Output is correct |
41 |
Correct |
6 ms |
5112 KB |
Output is correct |
42 |
Correct |
6 ms |
5112 KB |
Output is correct |
43 |
Correct |
6 ms |
5112 KB |
Output is correct |
44 |
Correct |
6 ms |
5112 KB |
Output is correct |
45 |
Correct |
7 ms |
5240 KB |
Output is correct |
46 |
Correct |
12 ms |
5496 KB |
Output is correct |
47 |
Correct |
13 ms |
5496 KB |
Output is correct |
48 |
Correct |
12 ms |
5368 KB |
Output is correct |
49 |
Correct |
9 ms |
5624 KB |
Output is correct |
50 |
Correct |
9 ms |
5624 KB |
Output is correct |
51 |
Correct |
9 ms |
5624 KB |
Output is correct |
52 |
Correct |
9 ms |
5496 KB |
Output is correct |
53 |
Correct |
10 ms |
5496 KB |
Output is correct |
54 |
Correct |
9 ms |
5496 KB |
Output is correct |
55 |
Correct |
10 ms |
5496 KB |
Output is correct |
56 |
Correct |
10 ms |
5496 KB |
Output is correct |
57 |
Correct |
14 ms |
5372 KB |
Output is correct |
58 |
Correct |
15 ms |
5368 KB |
Output is correct |
59 |
Correct |
15 ms |
5368 KB |
Output is correct |
60 |
Correct |
15 ms |
5496 KB |
Output is correct |
61 |
Correct |
10 ms |
5496 KB |
Output is correct |
62 |
Correct |
10 ms |
5496 KB |
Output is correct |
63 |
Correct |
10 ms |
5496 KB |
Output is correct |
64 |
Correct |
12 ms |
5448 KB |
Output is correct |
65 |
Correct |
12 ms |
5368 KB |
Output is correct |
66 |
Correct |
12 ms |
5340 KB |
Output is correct |
67 |
Correct |
12 ms |
5368 KB |
Output is correct |
68 |
Correct |
9 ms |
5624 KB |
Output is correct |
69 |
Correct |
9 ms |
5596 KB |
Output is correct |
70 |
Correct |
9 ms |
5496 KB |
Output is correct |
71 |
Correct |
9 ms |
5496 KB |
Output is correct |
72 |
Correct |
14 ms |
5368 KB |
Output is correct |
73 |
Correct |
15 ms |
5368 KB |
Output is correct |
74 |
Correct |
9 ms |
5496 KB |
Output is correct |
75 |
Correct |
9 ms |
5424 KB |
Output is correct |
76 |
Correct |
10 ms |
5368 KB |
Output is correct |
77 |
Correct |
10 ms |
5472 KB |
Output is correct |
78 |
Correct |
9 ms |
5496 KB |
Output is correct |
79 |
Correct |
10 ms |
5368 KB |
Output is correct |
80 |
Correct |
10 ms |
5368 KB |
Output is correct |
81 |
Correct |
10 ms |
5372 KB |
Output is correct |
82 |
Correct |
10 ms |
5496 KB |
Output is correct |
83 |
Correct |
10 ms |
5424 KB |
Output is correct |
84 |
Correct |
9 ms |
5496 KB |
Output is correct |
85 |
Correct |
10 ms |
5496 KB |
Output is correct |
86 |
Correct |
10 ms |
5340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
5116 KB |
Output is correct |
9 |
Correct |
6 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5108 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
6 ms |
5116 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
5112 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
5112 KB |
Output is correct |
21 |
Correct |
6 ms |
5112 KB |
Output is correct |
22 |
Correct |
6 ms |
5112 KB |
Output is correct |
23 |
Correct |
6 ms |
5112 KB |
Output is correct |
24 |
Correct |
6 ms |
5112 KB |
Output is correct |
25 |
Correct |
6 ms |
5112 KB |
Output is correct |
26 |
Correct |
6 ms |
5112 KB |
Output is correct |
27 |
Correct |
6 ms |
5112 KB |
Output is correct |
28 |
Correct |
6 ms |
5112 KB |
Output is correct |
29 |
Correct |
6 ms |
5112 KB |
Output is correct |
30 |
Correct |
6 ms |
5112 KB |
Output is correct |
31 |
Correct |
7 ms |
5112 KB |
Output is correct |
32 |
Correct |
6 ms |
5112 KB |
Output is correct |
33 |
Correct |
6 ms |
5112 KB |
Output is correct |
34 |
Correct |
6 ms |
5112 KB |
Output is correct |
35 |
Correct |
6 ms |
5112 KB |
Output is correct |
36 |
Correct |
6 ms |
5112 KB |
Output is correct |
37 |
Correct |
6 ms |
5116 KB |
Output is correct |
38 |
Correct |
6 ms |
5240 KB |
Output is correct |
39 |
Correct |
6 ms |
5100 KB |
Output is correct |
40 |
Correct |
6 ms |
5116 KB |
Output is correct |
41 |
Correct |
6 ms |
5112 KB |
Output is correct |
42 |
Correct |
6 ms |
5112 KB |
Output is correct |
43 |
Correct |
6 ms |
5112 KB |
Output is correct |
44 |
Correct |
6 ms |
5112 KB |
Output is correct |
45 |
Correct |
7 ms |
5240 KB |
Output is correct |
46 |
Correct |
12 ms |
5496 KB |
Output is correct |
47 |
Correct |
13 ms |
5496 KB |
Output is correct |
48 |
Correct |
12 ms |
5368 KB |
Output is correct |
49 |
Correct |
9 ms |
5624 KB |
Output is correct |
50 |
Correct |
9 ms |
5624 KB |
Output is correct |
51 |
Correct |
9 ms |
5624 KB |
Output is correct |
52 |
Correct |
9 ms |
5496 KB |
Output is correct |
53 |
Correct |
10 ms |
5496 KB |
Output is correct |
54 |
Correct |
9 ms |
5496 KB |
Output is correct |
55 |
Correct |
10 ms |
5496 KB |
Output is correct |
56 |
Correct |
10 ms |
5496 KB |
Output is correct |
57 |
Correct |
14 ms |
5372 KB |
Output is correct |
58 |
Correct |
15 ms |
5368 KB |
Output is correct |
59 |
Correct |
15 ms |
5368 KB |
Output is correct |
60 |
Correct |
15 ms |
5496 KB |
Output is correct |
61 |
Correct |
10 ms |
5496 KB |
Output is correct |
62 |
Correct |
10 ms |
5496 KB |
Output is correct |
63 |
Correct |
10 ms |
5496 KB |
Output is correct |
64 |
Correct |
12 ms |
5448 KB |
Output is correct |
65 |
Correct |
12 ms |
5368 KB |
Output is correct |
66 |
Correct |
12 ms |
5340 KB |
Output is correct |
67 |
Correct |
12 ms |
5368 KB |
Output is correct |
68 |
Correct |
9 ms |
5624 KB |
Output is correct |
69 |
Correct |
9 ms |
5596 KB |
Output is correct |
70 |
Correct |
9 ms |
5496 KB |
Output is correct |
71 |
Correct |
9 ms |
5496 KB |
Output is correct |
72 |
Correct |
14 ms |
5368 KB |
Output is correct |
73 |
Correct |
15 ms |
5368 KB |
Output is correct |
74 |
Correct |
9 ms |
5496 KB |
Output is correct |
75 |
Correct |
9 ms |
5424 KB |
Output is correct |
76 |
Correct |
10 ms |
5368 KB |
Output is correct |
77 |
Correct |
10 ms |
5472 KB |
Output is correct |
78 |
Correct |
9 ms |
5496 KB |
Output is correct |
79 |
Correct |
10 ms |
5368 KB |
Output is correct |
80 |
Correct |
10 ms |
5368 KB |
Output is correct |
81 |
Correct |
10 ms |
5372 KB |
Output is correct |
82 |
Correct |
10 ms |
5496 KB |
Output is correct |
83 |
Correct |
10 ms |
5424 KB |
Output is correct |
84 |
Correct |
9 ms |
5496 KB |
Output is correct |
85 |
Correct |
10 ms |
5496 KB |
Output is correct |
86 |
Correct |
10 ms |
5340 KB |
Output is correct |
87 |
Correct |
23 ms |
5880 KB |
Output is correct |
88 |
Correct |
64 ms |
7532 KB |
Output is correct |
89 |
Correct |
232 ms |
13432 KB |
Output is correct |
90 |
Correct |
232 ms |
13604 KB |
Output is correct |
91 |
Correct |
227 ms |
13432 KB |
Output is correct |
92 |
Correct |
100 ms |
17912 KB |
Output is correct |
93 |
Correct |
101 ms |
17972 KB |
Output is correct |
94 |
Correct |
102 ms |
17912 KB |
Output is correct |
95 |
Correct |
111 ms |
17052 KB |
Output is correct |
96 |
Correct |
119 ms |
17456 KB |
Output is correct |
97 |
Correct |
121 ms |
17492 KB |
Output is correct |
98 |
Correct |
119 ms |
17496 KB |
Output is correct |
99 |
Correct |
114 ms |
15808 KB |
Output is correct |
100 |
Correct |
272 ms |
13304 KB |
Output is correct |
101 |
Correct |
330 ms |
13560 KB |
Output is correct |
102 |
Correct |
335 ms |
13688 KB |
Output is correct |
103 |
Correct |
400 ms |
13560 KB |
Output is correct |
104 |
Correct |
123 ms |
15720 KB |
Output is correct |
105 |
Correct |
117 ms |
15804 KB |
Output is correct |
106 |
Correct |
120 ms |
15736 KB |
Output is correct |
107 |
Correct |
214 ms |
12664 KB |
Output is correct |
108 |
Correct |
229 ms |
12892 KB |
Output is correct |
109 |
Correct |
233 ms |
13148 KB |
Output is correct |
110 |
Correct |
94 ms |
17400 KB |
Output is correct |
111 |
Correct |
108 ms |
17136 KB |
Output is correct |
112 |
Correct |
107 ms |
16748 KB |
Output is correct |
113 |
Correct |
106 ms |
15096 KB |
Output is correct |
114 |
Correct |
272 ms |
13304 KB |
Output is correct |
115 |
Correct |
331 ms |
13048 KB |
Output is correct |
116 |
Correct |
115 ms |
15224 KB |
Output is correct |
117 |
Correct |
116 ms |
14200 KB |
Output is correct |
118 |
Correct |
118 ms |
13816 KB |
Output is correct |
119 |
Correct |
120 ms |
13724 KB |
Output is correct |
120 |
Correct |
118 ms |
13804 KB |
Output is correct |
121 |
Correct |
115 ms |
13560 KB |
Output is correct |
122 |
Correct |
124 ms |
13376 KB |
Output is correct |
123 |
Correct |
127 ms |
14328 KB |
Output is correct |
124 |
Correct |
131 ms |
13944 KB |
Output is correct |
125 |
Correct |
141 ms |
13864 KB |
Output is correct |
126 |
Correct |
127 ms |
14068 KB |
Output is correct |
127 |
Correct |
128 ms |
13632 KB |
Output is correct |
128 |
Correct |
133 ms |
13432 KB |
Output is correct |