#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 100'010;
const int LG = 20;
int up[N][LG], depth[N], n, sz[N];
vector<int> g[N];
int c[N];
void dfs(int at, int p) {
up[at][0] = p;
sz[at]=1;
for(int i = 1;i < LG;i++) {
up[at][i] = up[up[at][i-1]][i-1];
}
for(int to:g[at]) {
if(to == p)continue;
depth[to] = depth[at]+1;
dfs(to, at);
sz[at]+=sz[to];
}
}
int lca(int a, int b) {
if(depth[a] < depth[b])swap(a, b);
int k = depth[a] - depth[b];
for(int i = LG-1;i>=0;i--) {
if(k&(1<<i)) {
a = up[a][i];
}
}
if(a==b)return a;
for(int i = LG-1;i>=0;i--) {
if(up[a][i] != up[b][i]) {
a=up[a][i];
b=up[b][i];
}
}
return up[a][0];
}
int main () {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1;i<=n;i++)cin >> c[i];
vector<pair<int,int>> e;
for(int i = 1;i<n;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
e.push_back({a, b});
}
dfs(1, 0);
for(auto [a, b]:e) {
int ans = 0;
ordered_set<int> s;
int cur=a;
do {
ans+=s.order_of_key(c[cur]);
s.insert(c[cur]);
cur = up[cur][0];
}while(cur!=0);
cur=a;
do {
c[cur] = c[b];
cur = up[cur][0];
}while(cur!=0);
cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4572 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
10 ms |
4652 KB |
Output is correct |
9 |
Correct |
10 ms |
4656 KB |
Output is correct |
10 |
Correct |
10 ms |
4444 KB |
Output is correct |
11 |
Correct |
5 ms |
4440 KB |
Output is correct |
12 |
Correct |
5 ms |
4444 KB |
Output is correct |
13 |
Correct |
5 ms |
4444 KB |
Output is correct |
14 |
Correct |
5 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4580 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
5 ms |
4444 KB |
Output is correct |
20 |
Correct |
6 ms |
4444 KB |
Output is correct |
21 |
Correct |
6 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
10 ms |
4444 KB |
Output is correct |
27 |
Correct |
5 ms |
4444 KB |
Output is correct |
28 |
Correct |
5 ms |
4440 KB |
Output is correct |
29 |
Correct |
5 ms |
4632 KB |
Output is correct |
30 |
Correct |
1 ms |
4440 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
5 ms |
4624 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4440 KB |
Output is correct |
35 |
Correct |
2 ms |
4444 KB |
Output is correct |
36 |
Correct |
4 ms |
4616 KB |
Output is correct |
37 |
Correct |
2 ms |
4444 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
39 |
Correct |
3 ms |
4444 KB |
Output is correct |
40 |
Correct |
2 ms |
4440 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
3 ms |
4444 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4572 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
10 ms |
4652 KB |
Output is correct |
9 |
Correct |
10 ms |
4656 KB |
Output is correct |
10 |
Correct |
10 ms |
4444 KB |
Output is correct |
11 |
Correct |
5 ms |
4440 KB |
Output is correct |
12 |
Correct |
5 ms |
4444 KB |
Output is correct |
13 |
Correct |
5 ms |
4444 KB |
Output is correct |
14 |
Correct |
5 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4580 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
5 ms |
4444 KB |
Output is correct |
20 |
Correct |
6 ms |
4444 KB |
Output is correct |
21 |
Correct |
6 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
10 ms |
4444 KB |
Output is correct |
27 |
Correct |
5 ms |
4444 KB |
Output is correct |
28 |
Correct |
5 ms |
4440 KB |
Output is correct |
29 |
Correct |
5 ms |
4632 KB |
Output is correct |
30 |
Correct |
1 ms |
4440 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
5 ms |
4624 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4440 KB |
Output is correct |
35 |
Correct |
2 ms |
4444 KB |
Output is correct |
36 |
Correct |
4 ms |
4616 KB |
Output is correct |
37 |
Correct |
2 ms |
4444 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
39 |
Correct |
3 ms |
4444 KB |
Output is correct |
40 |
Correct |
2 ms |
4440 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
3 ms |
4444 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4572 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
4 ms |
4700 KB |
Output is correct |
47 |
Correct |
5 ms |
4700 KB |
Output is correct |
48 |
Correct |
4 ms |
4648 KB |
Output is correct |
49 |
Correct |
841 ms |
5204 KB |
Output is correct |
50 |
Correct |
847 ms |
5460 KB |
Output is correct |
51 |
Correct |
899 ms |
5208 KB |
Output is correct |
52 |
Correct |
386 ms |
4988 KB |
Output is correct |
53 |
Correct |
385 ms |
5208 KB |
Output is correct |
54 |
Correct |
397 ms |
5052 KB |
Output is correct |
55 |
Correct |
379 ms |
5044 KB |
Output is correct |
56 |
Correct |
395 ms |
5204 KB |
Output is correct |
57 |
Correct |
4 ms |
4696 KB |
Output is correct |
58 |
Correct |
4 ms |
4696 KB |
Output is correct |
59 |
Correct |
4 ms |
4700 KB |
Output is correct |
60 |
Correct |
4 ms |
4700 KB |
Output is correct |
61 |
Correct |
433 ms |
5272 KB |
Output is correct |
62 |
Correct |
405 ms |
4956 KB |
Output is correct |
63 |
Correct |
408 ms |
4956 KB |
Output is correct |
64 |
Correct |
3 ms |
4700 KB |
Output is correct |
65 |
Correct |
4 ms |
4700 KB |
Output is correct |
66 |
Correct |
4 ms |
4700 KB |
Output is correct |
67 |
Correct |
3 ms |
4700 KB |
Output is correct |
68 |
Correct |
895 ms |
5212 KB |
Output is correct |
69 |
Correct |
382 ms |
4952 KB |
Output is correct |
70 |
Correct |
378 ms |
4976 KB |
Output is correct |
71 |
Correct |
379 ms |
4972 KB |
Output is correct |
72 |
Correct |
4 ms |
4700 KB |
Output is correct |
73 |
Correct |
4 ms |
4700 KB |
Output is correct |
74 |
Correct |
412 ms |
4984 KB |
Output is correct |
75 |
Correct |
173 ms |
4916 KB |
Output is correct |
76 |
Correct |
112 ms |
4952 KB |
Output is correct |
77 |
Correct |
78 ms |
4696 KB |
Output is correct |
78 |
Correct |
180 ms |
5068 KB |
Output is correct |
79 |
Correct |
107 ms |
4700 KB |
Output is correct |
80 |
Correct |
77 ms |
4696 KB |
Output is correct |
81 |
Correct |
183 ms |
4916 KB |
Output is correct |
82 |
Correct |
116 ms |
4700 KB |
Output is correct |
83 |
Correct |
91 ms |
4944 KB |
Output is correct |
84 |
Correct |
184 ms |
4696 KB |
Output is correct |
85 |
Correct |
131 ms |
4696 KB |
Output is correct |
86 |
Correct |
84 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4572 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
10 ms |
4652 KB |
Output is correct |
9 |
Correct |
10 ms |
4656 KB |
Output is correct |
10 |
Correct |
10 ms |
4444 KB |
Output is correct |
11 |
Correct |
5 ms |
4440 KB |
Output is correct |
12 |
Correct |
5 ms |
4444 KB |
Output is correct |
13 |
Correct |
5 ms |
4444 KB |
Output is correct |
14 |
Correct |
5 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4580 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
5 ms |
4444 KB |
Output is correct |
20 |
Correct |
6 ms |
4444 KB |
Output is correct |
21 |
Correct |
6 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
10 ms |
4444 KB |
Output is correct |
27 |
Correct |
5 ms |
4444 KB |
Output is correct |
28 |
Correct |
5 ms |
4440 KB |
Output is correct |
29 |
Correct |
5 ms |
4632 KB |
Output is correct |
30 |
Correct |
1 ms |
4440 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
5 ms |
4624 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4440 KB |
Output is correct |
35 |
Correct |
2 ms |
4444 KB |
Output is correct |
36 |
Correct |
4 ms |
4616 KB |
Output is correct |
37 |
Correct |
2 ms |
4444 KB |
Output is correct |
38 |
Correct |
2 ms |
4444 KB |
Output is correct |
39 |
Correct |
3 ms |
4444 KB |
Output is correct |
40 |
Correct |
2 ms |
4440 KB |
Output is correct |
41 |
Correct |
2 ms |
4444 KB |
Output is correct |
42 |
Correct |
3 ms |
4444 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4572 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
4 ms |
4700 KB |
Output is correct |
47 |
Correct |
5 ms |
4700 KB |
Output is correct |
48 |
Correct |
4 ms |
4648 KB |
Output is correct |
49 |
Correct |
841 ms |
5204 KB |
Output is correct |
50 |
Correct |
847 ms |
5460 KB |
Output is correct |
51 |
Correct |
899 ms |
5208 KB |
Output is correct |
52 |
Correct |
386 ms |
4988 KB |
Output is correct |
53 |
Correct |
385 ms |
5208 KB |
Output is correct |
54 |
Correct |
397 ms |
5052 KB |
Output is correct |
55 |
Correct |
379 ms |
5044 KB |
Output is correct |
56 |
Correct |
395 ms |
5204 KB |
Output is correct |
57 |
Correct |
4 ms |
4696 KB |
Output is correct |
58 |
Correct |
4 ms |
4696 KB |
Output is correct |
59 |
Correct |
4 ms |
4700 KB |
Output is correct |
60 |
Correct |
4 ms |
4700 KB |
Output is correct |
61 |
Correct |
433 ms |
5272 KB |
Output is correct |
62 |
Correct |
405 ms |
4956 KB |
Output is correct |
63 |
Correct |
408 ms |
4956 KB |
Output is correct |
64 |
Correct |
3 ms |
4700 KB |
Output is correct |
65 |
Correct |
4 ms |
4700 KB |
Output is correct |
66 |
Correct |
4 ms |
4700 KB |
Output is correct |
67 |
Correct |
3 ms |
4700 KB |
Output is correct |
68 |
Correct |
895 ms |
5212 KB |
Output is correct |
69 |
Correct |
382 ms |
4952 KB |
Output is correct |
70 |
Correct |
378 ms |
4976 KB |
Output is correct |
71 |
Correct |
379 ms |
4972 KB |
Output is correct |
72 |
Correct |
4 ms |
4700 KB |
Output is correct |
73 |
Correct |
4 ms |
4700 KB |
Output is correct |
74 |
Correct |
412 ms |
4984 KB |
Output is correct |
75 |
Correct |
173 ms |
4916 KB |
Output is correct |
76 |
Correct |
112 ms |
4952 KB |
Output is correct |
77 |
Correct |
78 ms |
4696 KB |
Output is correct |
78 |
Correct |
180 ms |
5068 KB |
Output is correct |
79 |
Correct |
107 ms |
4700 KB |
Output is correct |
80 |
Correct |
77 ms |
4696 KB |
Output is correct |
81 |
Correct |
183 ms |
4916 KB |
Output is correct |
82 |
Correct |
116 ms |
4700 KB |
Output is correct |
83 |
Correct |
91 ms |
4944 KB |
Output is correct |
84 |
Correct |
184 ms |
4696 KB |
Output is correct |
85 |
Correct |
131 ms |
4696 KB |
Output is correct |
86 |
Correct |
84 ms |
4700 KB |
Output is correct |
87 |
Correct |
9 ms |
7256 KB |
Output is correct |
88 |
Correct |
31 ms |
8664 KB |
Output is correct |
89 |
Correct |
105 ms |
18088 KB |
Output is correct |
90 |
Correct |
102 ms |
18096 KB |
Output is correct |
91 |
Correct |
107 ms |
18116 KB |
Output is correct |
92 |
Execution timed out |
2029 ms |
25796 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |