#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef pair<int, int> pi;
const char nd = '\n';
void solve();
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t-->0) solve();
}
// ------------------- Code Starts here -------------------
// Union Find
struct UF{
vector<int> p, s;
UF(int n): p(n), s(n, 1){
iota(p.begin(), p.end(), 0);
}
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
void join(int x, int y){
x = find(x), y = find(y);
if(x == y) return;
if(s[x] < s[y]) swap(x, y);
p[y] = x;
s[x] += s[y];
}
};
const int N = 5e5+7;
vector<int> g[N], state[N];
int s[N], parent[N], level[N], jump[N];
UF group(N);
// dfs to get parent and level of each node
void dfs(int u, int p){
parent[u] = jump[u] = p;
level[u] = level[p]+1;
for (int v : g[u]){
if (v == p) continue;
dfs(v, u);
}
}
void solve(){
int n, k;
cin >> n >> k;
vector<pi> edges;
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
edges.pb({u, v});
g[u].pb(v), g[v].pb(u);
}
for (int i = 1; i <= n; i++){
cin >> s[i];
state[s[i]].pb(i);
}
dfs(1, 0);
for (int i = 1; i <= k; i++){
set<pi> st;
for(int u: state[i]) st.insert({-level[u], u});
while(st.size() > 1){
auto [l, u] = *st.begin();
st.erase(st.begin());
int v = parent[u];
group.join(s[u], s[v]);
st.insert({-level[v], v});
}
}
// degree of each state group
vector<int> deg(n+1, 0);
for(auto [u, v]: edges){
u = group.find(s[u]), v = group.find(s[v]);
if (u == v) continue;
deg[u]++, deg[v]++;
}
int leaf = 0;
for(int i = 1; i <= n; i++){
if(deg[i] == 1) leaf++;
}
cout << (leaf + 1) / 2 << nd;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33368 KB |
Output is correct |
2 |
Correct |
5 ms |
33372 KB |
Output is correct |
3 |
Correct |
5 ms |
33372 KB |
Output is correct |
4 |
Correct |
5 ms |
33372 KB |
Output is correct |
5 |
Correct |
6 ms |
33460 KB |
Output is correct |
6 |
Correct |
6 ms |
33368 KB |
Output is correct |
7 |
Correct |
5 ms |
33372 KB |
Output is correct |
8 |
Correct |
5 ms |
33348 KB |
Output is correct |
9 |
Correct |
5 ms |
33372 KB |
Output is correct |
10 |
Correct |
6 ms |
33372 KB |
Output is correct |
11 |
Correct |
5 ms |
33464 KB |
Output is correct |
12 |
Correct |
6 ms |
33368 KB |
Output is correct |
13 |
Correct |
5 ms |
33448 KB |
Output is correct |
14 |
Correct |
6 ms |
33372 KB |
Output is correct |
15 |
Correct |
5 ms |
33372 KB |
Output is correct |
16 |
Correct |
5 ms |
33452 KB |
Output is correct |
17 |
Correct |
5 ms |
33476 KB |
Output is correct |
18 |
Correct |
5 ms |
33324 KB |
Output is correct |
19 |
Correct |
5 ms |
33372 KB |
Output is correct |
20 |
Correct |
5 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
33372 KB |
Output is correct |
22 |
Correct |
5 ms |
33372 KB |
Output is correct |
23 |
Correct |
5 ms |
33372 KB |
Output is correct |
24 |
Correct |
5 ms |
33424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33368 KB |
Output is correct |
2 |
Correct |
5 ms |
33372 KB |
Output is correct |
3 |
Correct |
5 ms |
33372 KB |
Output is correct |
4 |
Correct |
5 ms |
33372 KB |
Output is correct |
5 |
Correct |
6 ms |
33460 KB |
Output is correct |
6 |
Correct |
6 ms |
33368 KB |
Output is correct |
7 |
Correct |
5 ms |
33372 KB |
Output is correct |
8 |
Correct |
5 ms |
33348 KB |
Output is correct |
9 |
Correct |
5 ms |
33372 KB |
Output is correct |
10 |
Correct |
6 ms |
33372 KB |
Output is correct |
11 |
Correct |
5 ms |
33464 KB |
Output is correct |
12 |
Correct |
6 ms |
33368 KB |
Output is correct |
13 |
Correct |
5 ms |
33448 KB |
Output is correct |
14 |
Correct |
6 ms |
33372 KB |
Output is correct |
15 |
Correct |
5 ms |
33372 KB |
Output is correct |
16 |
Correct |
5 ms |
33452 KB |
Output is correct |
17 |
Correct |
5 ms |
33476 KB |
Output is correct |
18 |
Correct |
5 ms |
33324 KB |
Output is correct |
19 |
Correct |
5 ms |
33372 KB |
Output is correct |
20 |
Correct |
5 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
33372 KB |
Output is correct |
22 |
Correct |
5 ms |
33372 KB |
Output is correct |
23 |
Correct |
5 ms |
33372 KB |
Output is correct |
24 |
Correct |
5 ms |
33424 KB |
Output is correct |
25 |
Correct |
5 ms |
33372 KB |
Output is correct |
26 |
Correct |
6 ms |
33628 KB |
Output is correct |
27 |
Correct |
7 ms |
33628 KB |
Output is correct |
28 |
Correct |
6 ms |
33628 KB |
Output is correct |
29 |
Correct |
6 ms |
33628 KB |
Output is correct |
30 |
Correct |
7 ms |
33628 KB |
Output is correct |
31 |
Correct |
6 ms |
33372 KB |
Output is correct |
32 |
Correct |
6 ms |
33884 KB |
Output is correct |
33 |
Correct |
5 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33628 KB |
Output is correct |
35 |
Correct |
7 ms |
33628 KB |
Output is correct |
36 |
Correct |
7 ms |
33628 KB |
Output is correct |
37 |
Correct |
7 ms |
33700 KB |
Output is correct |
38 |
Correct |
6 ms |
33372 KB |
Output is correct |
39 |
Correct |
6 ms |
33628 KB |
Output is correct |
40 |
Correct |
6 ms |
33628 KB |
Output is correct |
41 |
Correct |
6 ms |
33628 KB |
Output is correct |
42 |
Correct |
8 ms |
33628 KB |
Output is correct |
43 |
Correct |
6 ms |
33884 KB |
Output is correct |
44 |
Correct |
5 ms |
33324 KB |
Output is correct |
45 |
Correct |
6 ms |
33628 KB |
Output is correct |
46 |
Correct |
6 ms |
33628 KB |
Output is correct |
47 |
Correct |
5 ms |
33372 KB |
Output is correct |
48 |
Correct |
6 ms |
33608 KB |
Output is correct |
49 |
Correct |
6 ms |
33604 KB |
Output is correct |
50 |
Correct |
6 ms |
33628 KB |
Output is correct |
51 |
Correct |
7 ms |
33628 KB |
Output is correct |
52 |
Correct |
7 ms |
33600 KB |
Output is correct |
53 |
Correct |
6 ms |
33624 KB |
Output is correct |
54 |
Correct |
9 ms |
33624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33368 KB |
Output is correct |
2 |
Correct |
5 ms |
33372 KB |
Output is correct |
3 |
Correct |
5 ms |
33372 KB |
Output is correct |
4 |
Correct |
5 ms |
33372 KB |
Output is correct |
5 |
Correct |
6 ms |
33460 KB |
Output is correct |
6 |
Correct |
6 ms |
33368 KB |
Output is correct |
7 |
Correct |
5 ms |
33372 KB |
Output is correct |
8 |
Correct |
5 ms |
33348 KB |
Output is correct |
9 |
Correct |
5 ms |
33372 KB |
Output is correct |
10 |
Correct |
6 ms |
33372 KB |
Output is correct |
11 |
Correct |
5 ms |
33464 KB |
Output is correct |
12 |
Correct |
6 ms |
33368 KB |
Output is correct |
13 |
Correct |
5 ms |
33448 KB |
Output is correct |
14 |
Correct |
6 ms |
33372 KB |
Output is correct |
15 |
Correct |
5 ms |
33372 KB |
Output is correct |
16 |
Correct |
5 ms |
33452 KB |
Output is correct |
17 |
Correct |
5 ms |
33476 KB |
Output is correct |
18 |
Correct |
5 ms |
33324 KB |
Output is correct |
19 |
Correct |
5 ms |
33372 KB |
Output is correct |
20 |
Correct |
5 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
33372 KB |
Output is correct |
22 |
Correct |
5 ms |
33372 KB |
Output is correct |
23 |
Correct |
5 ms |
33372 KB |
Output is correct |
24 |
Correct |
5 ms |
33424 KB |
Output is correct |
25 |
Correct |
5 ms |
33372 KB |
Output is correct |
26 |
Correct |
36 ms |
40740 KB |
Output is correct |
27 |
Correct |
79 ms |
40644 KB |
Output is correct |
28 |
Correct |
6 ms |
33624 KB |
Output is correct |
29 |
Correct |
5 ms |
33372 KB |
Output is correct |
30 |
Correct |
5 ms |
33372 KB |
Output is correct |
31 |
Correct |
42 ms |
40344 KB |
Output is correct |
32 |
Correct |
6 ms |
33624 KB |
Output is correct |
33 |
Correct |
236 ms |
44600 KB |
Output is correct |
34 |
Correct |
71 ms |
40424 KB |
Output is correct |
35 |
Correct |
6 ms |
33624 KB |
Output is correct |
36 |
Correct |
45 ms |
40744 KB |
Output is correct |
37 |
Correct |
7 ms |
33624 KB |
Output is correct |
38 |
Correct |
6 ms |
33496 KB |
Output is correct |
39 |
Correct |
37 ms |
40640 KB |
Output is correct |
40 |
Correct |
6 ms |
33884 KB |
Output is correct |
41 |
Correct |
58 ms |
44444 KB |
Output is correct |
42 |
Correct |
156 ms |
41796 KB |
Output is correct |
43 |
Correct |
5 ms |
33368 KB |
Output is correct |
44 |
Correct |
68 ms |
48840 KB |
Output is correct |
45 |
Correct |
132 ms |
42616 KB |
Output is correct |
46 |
Correct |
7 ms |
33628 KB |
Output is correct |
47 |
Correct |
7 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
39300 KB |
Output is correct |
2 |
Correct |
40 ms |
41932 KB |
Output is correct |
3 |
Correct |
6 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33708 KB |
Output is correct |
5 |
Correct |
5 ms |
33368 KB |
Output is correct |
6 |
Correct |
6 ms |
33372 KB |
Output is correct |
7 |
Correct |
6 ms |
33628 KB |
Output is correct |
8 |
Correct |
55 ms |
41220 KB |
Output is correct |
9 |
Correct |
7 ms |
33484 KB |
Output is correct |
10 |
Correct |
42 ms |
40012 KB |
Output is correct |
11 |
Correct |
6 ms |
33372 KB |
Output is correct |
12 |
Correct |
41 ms |
40388 KB |
Output is correct |
13 |
Correct |
46 ms |
41672 KB |
Output is correct |
14 |
Correct |
45 ms |
43204 KB |
Output is correct |
15 |
Correct |
36 ms |
40648 KB |
Output is correct |
16 |
Correct |
7 ms |
33628 KB |
Output is correct |
17 |
Correct |
5 ms |
33464 KB |
Output is correct |
18 |
Correct |
39 ms |
43276 KB |
Output is correct |
19 |
Correct |
46 ms |
45516 KB |
Output is correct |
20 |
Correct |
6 ms |
33628 KB |
Output is correct |
21 |
Correct |
5 ms |
33368 KB |
Output is correct |
22 |
Correct |
47 ms |
41928 KB |
Output is correct |
23 |
Correct |
6 ms |
33628 KB |
Output is correct |
24 |
Correct |
43 ms |
40912 KB |
Output is correct |
25 |
Correct |
41 ms |
44776 KB |
Output is correct |
26 |
Correct |
6 ms |
33628 KB |
Output is correct |
27 |
Correct |
6 ms |
33628 KB |
Output is correct |
28 |
Correct |
6 ms |
33628 KB |
Output is correct |
29 |
Correct |
6 ms |
33480 KB |
Output is correct |
30 |
Correct |
7 ms |
33600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33368 KB |
Output is correct |
2 |
Correct |
5 ms |
33372 KB |
Output is correct |
3 |
Correct |
5 ms |
33372 KB |
Output is correct |
4 |
Correct |
5 ms |
33372 KB |
Output is correct |
5 |
Correct |
6 ms |
33460 KB |
Output is correct |
6 |
Correct |
6 ms |
33368 KB |
Output is correct |
7 |
Correct |
5 ms |
33372 KB |
Output is correct |
8 |
Correct |
5 ms |
33348 KB |
Output is correct |
9 |
Correct |
5 ms |
33372 KB |
Output is correct |
10 |
Correct |
6 ms |
33372 KB |
Output is correct |
11 |
Correct |
5 ms |
33464 KB |
Output is correct |
12 |
Correct |
6 ms |
33368 KB |
Output is correct |
13 |
Correct |
5 ms |
33448 KB |
Output is correct |
14 |
Correct |
6 ms |
33372 KB |
Output is correct |
15 |
Correct |
5 ms |
33372 KB |
Output is correct |
16 |
Correct |
5 ms |
33452 KB |
Output is correct |
17 |
Correct |
5 ms |
33476 KB |
Output is correct |
18 |
Correct |
5 ms |
33324 KB |
Output is correct |
19 |
Correct |
5 ms |
33372 KB |
Output is correct |
20 |
Correct |
5 ms |
33372 KB |
Output is correct |
21 |
Correct |
7 ms |
33372 KB |
Output is correct |
22 |
Correct |
5 ms |
33372 KB |
Output is correct |
23 |
Correct |
5 ms |
33372 KB |
Output is correct |
24 |
Correct |
5 ms |
33424 KB |
Output is correct |
25 |
Correct |
5 ms |
33372 KB |
Output is correct |
26 |
Correct |
6 ms |
33628 KB |
Output is correct |
27 |
Correct |
7 ms |
33628 KB |
Output is correct |
28 |
Correct |
6 ms |
33628 KB |
Output is correct |
29 |
Correct |
6 ms |
33628 KB |
Output is correct |
30 |
Correct |
7 ms |
33628 KB |
Output is correct |
31 |
Correct |
6 ms |
33372 KB |
Output is correct |
32 |
Correct |
6 ms |
33884 KB |
Output is correct |
33 |
Correct |
5 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33628 KB |
Output is correct |
35 |
Correct |
7 ms |
33628 KB |
Output is correct |
36 |
Correct |
7 ms |
33628 KB |
Output is correct |
37 |
Correct |
7 ms |
33700 KB |
Output is correct |
38 |
Correct |
6 ms |
33372 KB |
Output is correct |
39 |
Correct |
6 ms |
33628 KB |
Output is correct |
40 |
Correct |
6 ms |
33628 KB |
Output is correct |
41 |
Correct |
6 ms |
33628 KB |
Output is correct |
42 |
Correct |
8 ms |
33628 KB |
Output is correct |
43 |
Correct |
6 ms |
33884 KB |
Output is correct |
44 |
Correct |
5 ms |
33324 KB |
Output is correct |
45 |
Correct |
6 ms |
33628 KB |
Output is correct |
46 |
Correct |
6 ms |
33628 KB |
Output is correct |
47 |
Correct |
5 ms |
33372 KB |
Output is correct |
48 |
Correct |
6 ms |
33608 KB |
Output is correct |
49 |
Correct |
6 ms |
33604 KB |
Output is correct |
50 |
Correct |
6 ms |
33628 KB |
Output is correct |
51 |
Correct |
7 ms |
33628 KB |
Output is correct |
52 |
Correct |
7 ms |
33600 KB |
Output is correct |
53 |
Correct |
6 ms |
33624 KB |
Output is correct |
54 |
Correct |
9 ms |
33624 KB |
Output is correct |
55 |
Correct |
5 ms |
33372 KB |
Output is correct |
56 |
Correct |
36 ms |
40740 KB |
Output is correct |
57 |
Correct |
79 ms |
40644 KB |
Output is correct |
58 |
Correct |
6 ms |
33624 KB |
Output is correct |
59 |
Correct |
5 ms |
33372 KB |
Output is correct |
60 |
Correct |
5 ms |
33372 KB |
Output is correct |
61 |
Correct |
42 ms |
40344 KB |
Output is correct |
62 |
Correct |
6 ms |
33624 KB |
Output is correct |
63 |
Correct |
236 ms |
44600 KB |
Output is correct |
64 |
Correct |
71 ms |
40424 KB |
Output is correct |
65 |
Correct |
6 ms |
33624 KB |
Output is correct |
66 |
Correct |
45 ms |
40744 KB |
Output is correct |
67 |
Correct |
7 ms |
33624 KB |
Output is correct |
68 |
Correct |
6 ms |
33496 KB |
Output is correct |
69 |
Correct |
37 ms |
40640 KB |
Output is correct |
70 |
Correct |
6 ms |
33884 KB |
Output is correct |
71 |
Correct |
58 ms |
44444 KB |
Output is correct |
72 |
Correct |
156 ms |
41796 KB |
Output is correct |
73 |
Correct |
5 ms |
33368 KB |
Output is correct |
74 |
Correct |
68 ms |
48840 KB |
Output is correct |
75 |
Correct |
132 ms |
42616 KB |
Output is correct |
76 |
Correct |
7 ms |
33628 KB |
Output is correct |
77 |
Correct |
7 ms |
33628 KB |
Output is correct |
78 |
Correct |
36 ms |
39300 KB |
Output is correct |
79 |
Correct |
40 ms |
41932 KB |
Output is correct |
80 |
Correct |
6 ms |
33628 KB |
Output is correct |
81 |
Correct |
7 ms |
33708 KB |
Output is correct |
82 |
Correct |
5 ms |
33368 KB |
Output is correct |
83 |
Correct |
6 ms |
33372 KB |
Output is correct |
84 |
Correct |
6 ms |
33628 KB |
Output is correct |
85 |
Correct |
55 ms |
41220 KB |
Output is correct |
86 |
Correct |
7 ms |
33484 KB |
Output is correct |
87 |
Correct |
42 ms |
40012 KB |
Output is correct |
88 |
Correct |
6 ms |
33372 KB |
Output is correct |
89 |
Correct |
41 ms |
40388 KB |
Output is correct |
90 |
Correct |
46 ms |
41672 KB |
Output is correct |
91 |
Correct |
45 ms |
43204 KB |
Output is correct |
92 |
Correct |
36 ms |
40648 KB |
Output is correct |
93 |
Correct |
7 ms |
33628 KB |
Output is correct |
94 |
Correct |
5 ms |
33464 KB |
Output is correct |
95 |
Correct |
39 ms |
43276 KB |
Output is correct |
96 |
Correct |
46 ms |
45516 KB |
Output is correct |
97 |
Correct |
6 ms |
33628 KB |
Output is correct |
98 |
Correct |
5 ms |
33368 KB |
Output is correct |
99 |
Correct |
47 ms |
41928 KB |
Output is correct |
100 |
Correct |
6 ms |
33628 KB |
Output is correct |
101 |
Correct |
43 ms |
40912 KB |
Output is correct |
102 |
Correct |
41 ms |
44776 KB |
Output is correct |
103 |
Correct |
6 ms |
33628 KB |
Output is correct |
104 |
Correct |
6 ms |
33628 KB |
Output is correct |
105 |
Correct |
6 ms |
33628 KB |
Output is correct |
106 |
Correct |
6 ms |
33480 KB |
Output is correct |
107 |
Correct |
7 ms |
33600 KB |
Output is correct |
108 |
Correct |
220 ms |
73540 KB |
Output is correct |
109 |
Correct |
390 ms |
86456 KB |
Output is correct |
110 |
Correct |
462 ms |
105032 KB |
Output is correct |
111 |
Correct |
313 ms |
94900 KB |
Output is correct |
112 |
Correct |
334 ms |
89020 KB |
Output is correct |
113 |
Correct |
275 ms |
81308 KB |
Output is correct |
114 |
Correct |
338 ms |
75696 KB |
Output is correct |
115 |
Correct |
380 ms |
87736 KB |
Output is correct |
116 |
Correct |
436 ms |
70396 KB |
Output is correct |
117 |
Correct |
255 ms |
81340 KB |
Output is correct |
118 |
Correct |
229 ms |
67260 KB |
Output is correct |
119 |
Correct |
262 ms |
81324 KB |
Output is correct |
120 |
Correct |
303 ms |
89276 KB |
Output is correct |
121 |
Correct |
280 ms |
81336 KB |
Output is correct |
122 |
Correct |
437 ms |
74904 KB |
Output is correct |
123 |
Correct |
276 ms |
82872 KB |
Output is correct |
124 |
Correct |
300 ms |
80004 KB |
Output is correct |