#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=5e5+7;
vector<int>V[LIM], P[LIM];
int T[LIM], odl[LIM], F[LIM], oc[LIM], deg[LIM];
void DFS(int x, int o) {
for(auto i : V[x]) if(i!=o) {
odl[i]=odl[x]+1;
oc[i]=x;
DFS(i, x);
}
}
int fnd(int x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
void uni(int a, int b) {
if(fnd(a)==fnd(b)) return;
F[fnd(b)]=fnd(a);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k;
cin >> n >> k;
rep(i, n-1) {
int a, b;
cin >> a >> b; --a; --b;
V[a].pb(b);
V[b].pb(a);
}
DFS(0, 0);
rep(i, n) {
cin >> T[i]; --T[i];
P[T[i]].pb(i);
F[i]=i;
}
rep(i, k) {
set<pair<int,int>>S;
for(auto j : P[i]) S.insert({odl[fnd(j)], fnd(j)});
while(S.size()>1) {
auto it=S.end(); --it;
auto p=*it;
S.erase(it);
uni(oc[p.nd], p.nd);
S.insert({odl[fnd(p.nd)], fnd(p.nd)});
}
}
rep(i, n) for(auto j : V[i]) if(fnd(i)!=fnd(j)) ++deg[fnd(j)];
int ans=0;
rep(i, n) if(deg[i]==1) ++ans;
cout << (ans+1)/2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23820 KB |
Output is correct |
8 |
Correct |
11 ms |
23728 KB |
Output is correct |
9 |
Correct |
11 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23820 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23812 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23776 KB |
Output is correct |
17 |
Correct |
11 ms |
23832 KB |
Output is correct |
18 |
Correct |
12 ms |
23844 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23820 KB |
Output is correct |
8 |
Correct |
11 ms |
23728 KB |
Output is correct |
9 |
Correct |
11 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23820 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23812 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23776 KB |
Output is correct |
17 |
Correct |
11 ms |
23832 KB |
Output is correct |
18 |
Correct |
12 ms |
23844 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23816 KB |
Output is correct |
25 |
Correct |
13 ms |
23820 KB |
Output is correct |
26 |
Correct |
12 ms |
24060 KB |
Output is correct |
27 |
Correct |
12 ms |
23988 KB |
Output is correct |
28 |
Correct |
13 ms |
24204 KB |
Output is correct |
29 |
Correct |
12 ms |
24148 KB |
Output is correct |
30 |
Correct |
14 ms |
24004 KB |
Output is correct |
31 |
Correct |
14 ms |
23892 KB |
Output is correct |
32 |
Correct |
13 ms |
24148 KB |
Output is correct |
33 |
Correct |
12 ms |
23792 KB |
Output is correct |
34 |
Correct |
13 ms |
23972 KB |
Output is correct |
35 |
Correct |
14 ms |
24080 KB |
Output is correct |
36 |
Correct |
13 ms |
23992 KB |
Output is correct |
37 |
Correct |
12 ms |
24048 KB |
Output is correct |
38 |
Correct |
11 ms |
23812 KB |
Output is correct |
39 |
Correct |
12 ms |
24020 KB |
Output is correct |
40 |
Correct |
13 ms |
24080 KB |
Output is correct |
41 |
Correct |
15 ms |
24020 KB |
Output is correct |
42 |
Correct |
13 ms |
24080 KB |
Output is correct |
43 |
Correct |
12 ms |
24296 KB |
Output is correct |
44 |
Correct |
11 ms |
23748 KB |
Output is correct |
45 |
Correct |
13 ms |
24100 KB |
Output is correct |
46 |
Correct |
12 ms |
24072 KB |
Output is correct |
47 |
Correct |
12 ms |
23816 KB |
Output is correct |
48 |
Correct |
13 ms |
23956 KB |
Output is correct |
49 |
Correct |
13 ms |
24036 KB |
Output is correct |
50 |
Correct |
12 ms |
24080 KB |
Output is correct |
51 |
Correct |
13 ms |
23992 KB |
Output is correct |
52 |
Correct |
13 ms |
23952 KB |
Output is correct |
53 |
Correct |
12 ms |
24088 KB |
Output is correct |
54 |
Correct |
12 ms |
23960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23820 KB |
Output is correct |
8 |
Correct |
11 ms |
23728 KB |
Output is correct |
9 |
Correct |
11 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23820 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23812 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23776 KB |
Output is correct |
17 |
Correct |
11 ms |
23832 KB |
Output is correct |
18 |
Correct |
12 ms |
23844 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23816 KB |
Output is correct |
25 |
Correct |
11 ms |
23764 KB |
Output is correct |
26 |
Correct |
49 ms |
30916 KB |
Output is correct |
27 |
Correct |
65 ms |
30524 KB |
Output is correct |
28 |
Correct |
13 ms |
24020 KB |
Output is correct |
29 |
Correct |
11 ms |
23764 KB |
Output is correct |
30 |
Correct |
13 ms |
23812 KB |
Output is correct |
31 |
Correct |
67 ms |
30744 KB |
Output is correct |
32 |
Correct |
16 ms |
24084 KB |
Output is correct |
33 |
Correct |
57 ms |
36220 KB |
Output is correct |
34 |
Correct |
65 ms |
30644 KB |
Output is correct |
35 |
Correct |
13 ms |
24020 KB |
Output is correct |
36 |
Correct |
67 ms |
31264 KB |
Output is correct |
37 |
Correct |
13 ms |
24152 KB |
Output is correct |
38 |
Correct |
13 ms |
24084 KB |
Output is correct |
39 |
Correct |
52 ms |
30804 KB |
Output is correct |
40 |
Correct |
13 ms |
24356 KB |
Output is correct |
41 |
Correct |
83 ms |
35036 KB |
Output is correct |
42 |
Correct |
59 ms |
32384 KB |
Output is correct |
43 |
Correct |
12 ms |
23764 KB |
Output is correct |
44 |
Correct |
91 ms |
40944 KB |
Output is correct |
45 |
Correct |
62 ms |
33504 KB |
Output is correct |
46 |
Correct |
12 ms |
24020 KB |
Output is correct |
47 |
Correct |
13 ms |
23992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
30928 KB |
Output is correct |
2 |
Correct |
52 ms |
34116 KB |
Output is correct |
3 |
Correct |
12 ms |
24104 KB |
Output is correct |
4 |
Correct |
12 ms |
24068 KB |
Output is correct |
5 |
Correct |
10 ms |
23812 KB |
Output is correct |
6 |
Correct |
11 ms |
23812 KB |
Output is correct |
7 |
Correct |
11 ms |
24020 KB |
Output is correct |
8 |
Correct |
64 ms |
32308 KB |
Output is correct |
9 |
Correct |
13 ms |
24020 KB |
Output is correct |
10 |
Correct |
67 ms |
31012 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
66 ms |
30996 KB |
Output is correct |
13 |
Correct |
80 ms |
32316 KB |
Output is correct |
14 |
Correct |
62 ms |
33880 KB |
Output is correct |
15 |
Correct |
51 ms |
30856 KB |
Output is correct |
16 |
Correct |
13 ms |
24008 KB |
Output is correct |
17 |
Correct |
11 ms |
23732 KB |
Output is correct |
18 |
Correct |
55 ms |
33860 KB |
Output is correct |
19 |
Correct |
63 ms |
36920 KB |
Output is correct |
20 |
Correct |
13 ms |
24020 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
53 ms |
32456 KB |
Output is correct |
23 |
Correct |
13 ms |
24020 KB |
Output is correct |
24 |
Correct |
74 ms |
31496 KB |
Output is correct |
25 |
Correct |
61 ms |
35852 KB |
Output is correct |
26 |
Correct |
12 ms |
24148 KB |
Output is correct |
27 |
Correct |
12 ms |
24148 KB |
Output is correct |
28 |
Correct |
12 ms |
24020 KB |
Output is correct |
29 |
Correct |
12 ms |
23960 KB |
Output is correct |
30 |
Correct |
12 ms |
24016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
10 ms |
23812 KB |
Output is correct |
5 |
Correct |
11 ms |
23816 KB |
Output is correct |
6 |
Correct |
11 ms |
23764 KB |
Output is correct |
7 |
Correct |
11 ms |
23820 KB |
Output is correct |
8 |
Correct |
11 ms |
23728 KB |
Output is correct |
9 |
Correct |
11 ms |
23784 KB |
Output is correct |
10 |
Correct |
11 ms |
23820 KB |
Output is correct |
11 |
Correct |
11 ms |
23816 KB |
Output is correct |
12 |
Correct |
11 ms |
23816 KB |
Output is correct |
13 |
Correct |
11 ms |
23812 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23776 KB |
Output is correct |
17 |
Correct |
11 ms |
23832 KB |
Output is correct |
18 |
Correct |
12 ms |
23844 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23820 KB |
Output is correct |
22 |
Correct |
11 ms |
23764 KB |
Output is correct |
23 |
Correct |
11 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23816 KB |
Output is correct |
25 |
Correct |
13 ms |
23820 KB |
Output is correct |
26 |
Correct |
12 ms |
24060 KB |
Output is correct |
27 |
Correct |
12 ms |
23988 KB |
Output is correct |
28 |
Correct |
13 ms |
24204 KB |
Output is correct |
29 |
Correct |
12 ms |
24148 KB |
Output is correct |
30 |
Correct |
14 ms |
24004 KB |
Output is correct |
31 |
Correct |
14 ms |
23892 KB |
Output is correct |
32 |
Correct |
13 ms |
24148 KB |
Output is correct |
33 |
Correct |
12 ms |
23792 KB |
Output is correct |
34 |
Correct |
13 ms |
23972 KB |
Output is correct |
35 |
Correct |
14 ms |
24080 KB |
Output is correct |
36 |
Correct |
13 ms |
23992 KB |
Output is correct |
37 |
Correct |
12 ms |
24048 KB |
Output is correct |
38 |
Correct |
11 ms |
23812 KB |
Output is correct |
39 |
Correct |
12 ms |
24020 KB |
Output is correct |
40 |
Correct |
13 ms |
24080 KB |
Output is correct |
41 |
Correct |
15 ms |
24020 KB |
Output is correct |
42 |
Correct |
13 ms |
24080 KB |
Output is correct |
43 |
Correct |
12 ms |
24296 KB |
Output is correct |
44 |
Correct |
11 ms |
23748 KB |
Output is correct |
45 |
Correct |
13 ms |
24100 KB |
Output is correct |
46 |
Correct |
12 ms |
24072 KB |
Output is correct |
47 |
Correct |
12 ms |
23816 KB |
Output is correct |
48 |
Correct |
13 ms |
23956 KB |
Output is correct |
49 |
Correct |
13 ms |
24036 KB |
Output is correct |
50 |
Correct |
12 ms |
24080 KB |
Output is correct |
51 |
Correct |
13 ms |
23992 KB |
Output is correct |
52 |
Correct |
13 ms |
23952 KB |
Output is correct |
53 |
Correct |
12 ms |
24088 KB |
Output is correct |
54 |
Correct |
12 ms |
23960 KB |
Output is correct |
55 |
Correct |
11 ms |
23764 KB |
Output is correct |
56 |
Correct |
49 ms |
30916 KB |
Output is correct |
57 |
Correct |
65 ms |
30524 KB |
Output is correct |
58 |
Correct |
13 ms |
24020 KB |
Output is correct |
59 |
Correct |
11 ms |
23764 KB |
Output is correct |
60 |
Correct |
13 ms |
23812 KB |
Output is correct |
61 |
Correct |
67 ms |
30744 KB |
Output is correct |
62 |
Correct |
16 ms |
24084 KB |
Output is correct |
63 |
Correct |
57 ms |
36220 KB |
Output is correct |
64 |
Correct |
65 ms |
30644 KB |
Output is correct |
65 |
Correct |
13 ms |
24020 KB |
Output is correct |
66 |
Correct |
67 ms |
31264 KB |
Output is correct |
67 |
Correct |
13 ms |
24152 KB |
Output is correct |
68 |
Correct |
13 ms |
24084 KB |
Output is correct |
69 |
Correct |
52 ms |
30804 KB |
Output is correct |
70 |
Correct |
13 ms |
24356 KB |
Output is correct |
71 |
Correct |
83 ms |
35036 KB |
Output is correct |
72 |
Correct |
59 ms |
32384 KB |
Output is correct |
73 |
Correct |
12 ms |
23764 KB |
Output is correct |
74 |
Correct |
91 ms |
40944 KB |
Output is correct |
75 |
Correct |
62 ms |
33504 KB |
Output is correct |
76 |
Correct |
12 ms |
24020 KB |
Output is correct |
77 |
Correct |
13 ms |
23992 KB |
Output is correct |
78 |
Correct |
60 ms |
30928 KB |
Output is correct |
79 |
Correct |
52 ms |
34116 KB |
Output is correct |
80 |
Correct |
12 ms |
24104 KB |
Output is correct |
81 |
Correct |
12 ms |
24068 KB |
Output is correct |
82 |
Correct |
10 ms |
23812 KB |
Output is correct |
83 |
Correct |
11 ms |
23812 KB |
Output is correct |
84 |
Correct |
11 ms |
24020 KB |
Output is correct |
85 |
Correct |
64 ms |
32308 KB |
Output is correct |
86 |
Correct |
13 ms |
24020 KB |
Output is correct |
87 |
Correct |
67 ms |
31012 KB |
Output is correct |
88 |
Correct |
11 ms |
23764 KB |
Output is correct |
89 |
Correct |
66 ms |
30996 KB |
Output is correct |
90 |
Correct |
80 ms |
32316 KB |
Output is correct |
91 |
Correct |
62 ms |
33880 KB |
Output is correct |
92 |
Correct |
51 ms |
30856 KB |
Output is correct |
93 |
Correct |
13 ms |
24008 KB |
Output is correct |
94 |
Correct |
11 ms |
23732 KB |
Output is correct |
95 |
Correct |
55 ms |
33860 KB |
Output is correct |
96 |
Correct |
63 ms |
36920 KB |
Output is correct |
97 |
Correct |
13 ms |
24020 KB |
Output is correct |
98 |
Correct |
11 ms |
23764 KB |
Output is correct |
99 |
Correct |
53 ms |
32456 KB |
Output is correct |
100 |
Correct |
13 ms |
24020 KB |
Output is correct |
101 |
Correct |
74 ms |
31496 KB |
Output is correct |
102 |
Correct |
61 ms |
35852 KB |
Output is correct |
103 |
Correct |
12 ms |
24148 KB |
Output is correct |
104 |
Correct |
12 ms |
24148 KB |
Output is correct |
105 |
Correct |
12 ms |
24020 KB |
Output is correct |
106 |
Correct |
12 ms |
23960 KB |
Output is correct |
107 |
Correct |
12 ms |
24016 KB |
Output is correct |
108 |
Correct |
383 ms |
67448 KB |
Output is correct |
109 |
Correct |
683 ms |
83424 KB |
Output is correct |
110 |
Correct |
804 ms |
104040 KB |
Output is correct |
111 |
Correct |
712 ms |
93716 KB |
Output is correct |
112 |
Correct |
747 ms |
85864 KB |
Output is correct |
113 |
Correct |
489 ms |
75296 KB |
Output is correct |
114 |
Correct |
796 ms |
69144 KB |
Output is correct |
115 |
Correct |
802 ms |
80840 KB |
Output is correct |
116 |
Correct |
593 ms |
63888 KB |
Output is correct |
117 |
Correct |
736 ms |
75356 KB |
Output is correct |
118 |
Correct |
564 ms |
60364 KB |
Output is correct |
119 |
Correct |
619 ms |
75332 KB |
Output is correct |
120 |
Correct |
634 ms |
86376 KB |
Output is correct |
121 |
Correct |
581 ms |
75368 KB |
Output is correct |
122 |
Correct |
652 ms |
69052 KB |
Output is correct |
123 |
Correct |
503 ms |
76836 KB |
Output is correct |
124 |
Correct |
691 ms |
74128 KB |
Output is correct |