#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,k,p[500001][20],group[500001],level[500001],mark[500001];
vector<int> adj1[500001],adj2[500001],g[500001];
set<int> temp[500001];
vector<pii> edge;
bool vis[500001];
void dfs1(int x, int par) {
level[x]=level[par]+1;
p[x][0]=par;
for (int i=1; i<20; ++i) p[x][i]=p[p[x][i-1]][i-1];
for (auto s : adj1[x]) {
if (s==par) continue;
dfs1(s,x);
}
}
int lca(int x, int y) {
if (level[x]<level[y]) swap(x,y);
int dif=level[x]-level[y];
for (int i=0; i<20; ++i) {
if (dif&(1<<i)) x=p[x][i];
}
if (x==y) return x;
for (int i=19; i>=0; --i) {
if (p[x][i]!=p[y][i]) {
x=p[x][i];
y=p[y][i];
}
}
return p[x][0];
}
void dfs2(int x, int par) {
for (auto s : adj1[x]) {
if (s==par) continue;
dfs2(s,x);
mark[x]+=mark[s];
}
}
void dfs3(int x, int gr) {
vis[x]=true;
group[x]=gr;
for (auto s : adj2[x]) {
if (!vis[s]) dfs3(s,gr);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>k;
for (int i=0; i<n-1; ++i) {
int a,b; cin>>a>>b;
adj1[a].push_back(b);
adj1[b].push_back(a);
edge.push_back(pii(a,b));
}
for (int i=1; i<=n; ++i) {
int x; cin>>x;
g[x].push_back(i);
}
dfs1(1,0);
for (int i=1; i<=k; ++i) {
for (int j=1; j<g[i].size(); ++j) {
int LCA=lca(g[i][j],g[i][j-1]);
++mark[g[i][j]];
++mark[g[i][j-1]];
mark[LCA]-=2;
}
}
dfs2(1,0);
for (int i=2; i<=n; ++i) {
if (mark[i]>0) {
adj2[i].push_back(p[i][0]);
adj2[p[i][0]].push_back(i);
}
}
int cnt=0;
for (int i=1; i<=n; ++i) {
if (!vis[i]) dfs3(i,++cnt);
}
for (auto s : edge) {
if (group[s.first]==group[s.second]) continue;
temp[group[s.first]].insert(group[s.second]);
temp[group[s.second]].insert(group[s.first]);
}
int leaf=0;
for (int i=1; i<=cnt; ++i) {
if (temp[i].size()==1) ++leaf;
}
cout<<(leaf+1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:73:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int j=1; j<g[i].size(); ++j) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58964 KB |
Output is correct |
2 |
Correct |
29 ms |
58964 KB |
Output is correct |
3 |
Correct |
27 ms |
59060 KB |
Output is correct |
4 |
Correct |
28 ms |
58964 KB |
Output is correct |
5 |
Correct |
29 ms |
59008 KB |
Output is correct |
6 |
Correct |
30 ms |
59028 KB |
Output is correct |
7 |
Correct |
29 ms |
59044 KB |
Output is correct |
8 |
Correct |
27 ms |
59088 KB |
Output is correct |
9 |
Correct |
28 ms |
59040 KB |
Output is correct |
10 |
Correct |
27 ms |
59040 KB |
Output is correct |
11 |
Correct |
28 ms |
59032 KB |
Output is correct |
12 |
Correct |
27 ms |
59036 KB |
Output is correct |
13 |
Correct |
28 ms |
58964 KB |
Output is correct |
14 |
Correct |
27 ms |
59032 KB |
Output is correct |
15 |
Correct |
28 ms |
58964 KB |
Output is correct |
16 |
Correct |
30 ms |
58956 KB |
Output is correct |
17 |
Correct |
30 ms |
58952 KB |
Output is correct |
18 |
Correct |
32 ms |
59020 KB |
Output is correct |
19 |
Correct |
27 ms |
58992 KB |
Output is correct |
20 |
Correct |
29 ms |
58996 KB |
Output is correct |
21 |
Correct |
30 ms |
58968 KB |
Output is correct |
22 |
Correct |
28 ms |
59032 KB |
Output is correct |
23 |
Correct |
27 ms |
59044 KB |
Output is correct |
24 |
Correct |
27 ms |
59028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58964 KB |
Output is correct |
2 |
Correct |
29 ms |
58964 KB |
Output is correct |
3 |
Correct |
27 ms |
59060 KB |
Output is correct |
4 |
Correct |
28 ms |
58964 KB |
Output is correct |
5 |
Correct |
29 ms |
59008 KB |
Output is correct |
6 |
Correct |
30 ms |
59028 KB |
Output is correct |
7 |
Correct |
29 ms |
59044 KB |
Output is correct |
8 |
Correct |
27 ms |
59088 KB |
Output is correct |
9 |
Correct |
28 ms |
59040 KB |
Output is correct |
10 |
Correct |
27 ms |
59040 KB |
Output is correct |
11 |
Correct |
28 ms |
59032 KB |
Output is correct |
12 |
Correct |
27 ms |
59036 KB |
Output is correct |
13 |
Correct |
28 ms |
58964 KB |
Output is correct |
14 |
Correct |
27 ms |
59032 KB |
Output is correct |
15 |
Correct |
28 ms |
58964 KB |
Output is correct |
16 |
Correct |
30 ms |
58956 KB |
Output is correct |
17 |
Correct |
30 ms |
58952 KB |
Output is correct |
18 |
Correct |
32 ms |
59020 KB |
Output is correct |
19 |
Correct |
27 ms |
58992 KB |
Output is correct |
20 |
Correct |
29 ms |
58996 KB |
Output is correct |
21 |
Correct |
30 ms |
58968 KB |
Output is correct |
22 |
Correct |
28 ms |
59032 KB |
Output is correct |
23 |
Correct |
27 ms |
59044 KB |
Output is correct |
24 |
Correct |
27 ms |
59028 KB |
Output is correct |
25 |
Correct |
26 ms |
58964 KB |
Output is correct |
26 |
Correct |
31 ms |
59772 KB |
Output is correct |
27 |
Correct |
30 ms |
59508 KB |
Output is correct |
28 |
Correct |
31 ms |
59852 KB |
Output is correct |
29 |
Correct |
30 ms |
59812 KB |
Output is correct |
30 |
Correct |
30 ms |
59596 KB |
Output is correct |
31 |
Correct |
28 ms |
59008 KB |
Output is correct |
32 |
Correct |
30 ms |
59860 KB |
Output is correct |
33 |
Correct |
27 ms |
58968 KB |
Output is correct |
34 |
Correct |
30 ms |
59604 KB |
Output is correct |
35 |
Correct |
34 ms |
59872 KB |
Output is correct |
36 |
Correct |
30 ms |
59512 KB |
Output is correct |
37 |
Correct |
30 ms |
59552 KB |
Output is correct |
38 |
Correct |
27 ms |
59068 KB |
Output is correct |
39 |
Correct |
30 ms |
59640 KB |
Output is correct |
40 |
Correct |
29 ms |
59604 KB |
Output is correct |
41 |
Correct |
30 ms |
59560 KB |
Output is correct |
42 |
Correct |
31 ms |
59608 KB |
Output is correct |
43 |
Correct |
31 ms |
59716 KB |
Output is correct |
44 |
Correct |
28 ms |
59056 KB |
Output is correct |
45 |
Correct |
30 ms |
59672 KB |
Output is correct |
46 |
Correct |
29 ms |
59596 KB |
Output is correct |
47 |
Correct |
30 ms |
59020 KB |
Output is correct |
48 |
Correct |
30 ms |
59532 KB |
Output is correct |
49 |
Correct |
30 ms |
59812 KB |
Output is correct |
50 |
Correct |
33 ms |
59872 KB |
Output is correct |
51 |
Correct |
30 ms |
59604 KB |
Output is correct |
52 |
Correct |
30 ms |
59528 KB |
Output is correct |
53 |
Correct |
29 ms |
59704 KB |
Output is correct |
54 |
Correct |
30 ms |
59596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58964 KB |
Output is correct |
2 |
Correct |
29 ms |
58964 KB |
Output is correct |
3 |
Correct |
27 ms |
59060 KB |
Output is correct |
4 |
Correct |
28 ms |
58964 KB |
Output is correct |
5 |
Correct |
29 ms |
59008 KB |
Output is correct |
6 |
Correct |
30 ms |
59028 KB |
Output is correct |
7 |
Correct |
29 ms |
59044 KB |
Output is correct |
8 |
Correct |
27 ms |
59088 KB |
Output is correct |
9 |
Correct |
28 ms |
59040 KB |
Output is correct |
10 |
Correct |
27 ms |
59040 KB |
Output is correct |
11 |
Correct |
28 ms |
59032 KB |
Output is correct |
12 |
Correct |
27 ms |
59036 KB |
Output is correct |
13 |
Correct |
28 ms |
58964 KB |
Output is correct |
14 |
Correct |
27 ms |
59032 KB |
Output is correct |
15 |
Correct |
28 ms |
58964 KB |
Output is correct |
16 |
Correct |
30 ms |
58956 KB |
Output is correct |
17 |
Correct |
30 ms |
58952 KB |
Output is correct |
18 |
Correct |
32 ms |
59020 KB |
Output is correct |
19 |
Correct |
27 ms |
58992 KB |
Output is correct |
20 |
Correct |
29 ms |
58996 KB |
Output is correct |
21 |
Correct |
30 ms |
58968 KB |
Output is correct |
22 |
Correct |
28 ms |
59032 KB |
Output is correct |
23 |
Correct |
27 ms |
59044 KB |
Output is correct |
24 |
Correct |
27 ms |
59028 KB |
Output is correct |
25 |
Correct |
28 ms |
59004 KB |
Output is correct |
26 |
Correct |
99 ms |
77804 KB |
Output is correct |
27 |
Correct |
142 ms |
77244 KB |
Output is correct |
28 |
Correct |
29 ms |
59596 KB |
Output is correct |
29 |
Correct |
28 ms |
58964 KB |
Output is correct |
30 |
Correct |
27 ms |
59068 KB |
Output is correct |
31 |
Correct |
130 ms |
77176 KB |
Output is correct |
32 |
Correct |
29 ms |
59544 KB |
Output is correct |
33 |
Correct |
207 ms |
84208 KB |
Output is correct |
34 |
Correct |
152 ms |
77260 KB |
Output is correct |
35 |
Correct |
29 ms |
59604 KB |
Output is correct |
36 |
Correct |
174 ms |
77916 KB |
Output is correct |
37 |
Correct |
32 ms |
59564 KB |
Output is correct |
38 |
Correct |
29 ms |
59496 KB |
Output is correct |
39 |
Correct |
98 ms |
77756 KB |
Output is correct |
40 |
Correct |
29 ms |
59812 KB |
Output is correct |
41 |
Correct |
128 ms |
77048 KB |
Output is correct |
42 |
Correct |
192 ms |
79440 KB |
Output is correct |
43 |
Correct |
27 ms |
59028 KB |
Output is correct |
44 |
Correct |
168 ms |
84384 KB |
Output is correct |
45 |
Correct |
165 ms |
80780 KB |
Output is correct |
46 |
Correct |
29 ms |
59604 KB |
Output is correct |
47 |
Correct |
30 ms |
59620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
76364 KB |
Output is correct |
2 |
Correct |
158 ms |
84588 KB |
Output is correct |
3 |
Correct |
34 ms |
59732 KB |
Output is correct |
4 |
Correct |
30 ms |
59604 KB |
Output is correct |
5 |
Correct |
28 ms |
59036 KB |
Output is correct |
6 |
Correct |
28 ms |
59064 KB |
Output is correct |
7 |
Correct |
29 ms |
59528 KB |
Output is correct |
8 |
Correct |
146 ms |
78988 KB |
Output is correct |
9 |
Correct |
31 ms |
59596 KB |
Output is correct |
10 |
Correct |
136 ms |
77344 KB |
Output is correct |
11 |
Correct |
27 ms |
58968 KB |
Output is correct |
12 |
Correct |
150 ms |
77224 KB |
Output is correct |
13 |
Correct |
147 ms |
79836 KB |
Output is correct |
14 |
Correct |
159 ms |
86064 KB |
Output is correct |
15 |
Correct |
108 ms |
77764 KB |
Output is correct |
16 |
Correct |
31 ms |
59672 KB |
Output is correct |
17 |
Correct |
29 ms |
59072 KB |
Output is correct |
18 |
Correct |
151 ms |
84880 KB |
Output is correct |
19 |
Correct |
181 ms |
89968 KB |
Output is correct |
20 |
Correct |
29 ms |
59556 KB |
Output is correct |
21 |
Correct |
28 ms |
59092 KB |
Output is correct |
22 |
Correct |
118 ms |
79948 KB |
Output is correct |
23 |
Correct |
31 ms |
59604 KB |
Output is correct |
24 |
Correct |
144 ms |
78272 KB |
Output is correct |
25 |
Correct |
183 ms |
88764 KB |
Output is correct |
26 |
Correct |
32 ms |
59744 KB |
Output is correct |
27 |
Correct |
31 ms |
59904 KB |
Output is correct |
28 |
Correct |
44 ms |
59616 KB |
Output is correct |
29 |
Correct |
29 ms |
59636 KB |
Output is correct |
30 |
Correct |
30 ms |
59612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
58964 KB |
Output is correct |
2 |
Correct |
29 ms |
58964 KB |
Output is correct |
3 |
Correct |
27 ms |
59060 KB |
Output is correct |
4 |
Correct |
28 ms |
58964 KB |
Output is correct |
5 |
Correct |
29 ms |
59008 KB |
Output is correct |
6 |
Correct |
30 ms |
59028 KB |
Output is correct |
7 |
Correct |
29 ms |
59044 KB |
Output is correct |
8 |
Correct |
27 ms |
59088 KB |
Output is correct |
9 |
Correct |
28 ms |
59040 KB |
Output is correct |
10 |
Correct |
27 ms |
59040 KB |
Output is correct |
11 |
Correct |
28 ms |
59032 KB |
Output is correct |
12 |
Correct |
27 ms |
59036 KB |
Output is correct |
13 |
Correct |
28 ms |
58964 KB |
Output is correct |
14 |
Correct |
27 ms |
59032 KB |
Output is correct |
15 |
Correct |
28 ms |
58964 KB |
Output is correct |
16 |
Correct |
30 ms |
58956 KB |
Output is correct |
17 |
Correct |
30 ms |
58952 KB |
Output is correct |
18 |
Correct |
32 ms |
59020 KB |
Output is correct |
19 |
Correct |
27 ms |
58992 KB |
Output is correct |
20 |
Correct |
29 ms |
58996 KB |
Output is correct |
21 |
Correct |
30 ms |
58968 KB |
Output is correct |
22 |
Correct |
28 ms |
59032 KB |
Output is correct |
23 |
Correct |
27 ms |
59044 KB |
Output is correct |
24 |
Correct |
27 ms |
59028 KB |
Output is correct |
25 |
Correct |
26 ms |
58964 KB |
Output is correct |
26 |
Correct |
31 ms |
59772 KB |
Output is correct |
27 |
Correct |
30 ms |
59508 KB |
Output is correct |
28 |
Correct |
31 ms |
59852 KB |
Output is correct |
29 |
Correct |
30 ms |
59812 KB |
Output is correct |
30 |
Correct |
30 ms |
59596 KB |
Output is correct |
31 |
Correct |
28 ms |
59008 KB |
Output is correct |
32 |
Correct |
30 ms |
59860 KB |
Output is correct |
33 |
Correct |
27 ms |
58968 KB |
Output is correct |
34 |
Correct |
30 ms |
59604 KB |
Output is correct |
35 |
Correct |
34 ms |
59872 KB |
Output is correct |
36 |
Correct |
30 ms |
59512 KB |
Output is correct |
37 |
Correct |
30 ms |
59552 KB |
Output is correct |
38 |
Correct |
27 ms |
59068 KB |
Output is correct |
39 |
Correct |
30 ms |
59640 KB |
Output is correct |
40 |
Correct |
29 ms |
59604 KB |
Output is correct |
41 |
Correct |
30 ms |
59560 KB |
Output is correct |
42 |
Correct |
31 ms |
59608 KB |
Output is correct |
43 |
Correct |
31 ms |
59716 KB |
Output is correct |
44 |
Correct |
28 ms |
59056 KB |
Output is correct |
45 |
Correct |
30 ms |
59672 KB |
Output is correct |
46 |
Correct |
29 ms |
59596 KB |
Output is correct |
47 |
Correct |
30 ms |
59020 KB |
Output is correct |
48 |
Correct |
30 ms |
59532 KB |
Output is correct |
49 |
Correct |
30 ms |
59812 KB |
Output is correct |
50 |
Correct |
33 ms |
59872 KB |
Output is correct |
51 |
Correct |
30 ms |
59604 KB |
Output is correct |
52 |
Correct |
30 ms |
59528 KB |
Output is correct |
53 |
Correct |
29 ms |
59704 KB |
Output is correct |
54 |
Correct |
30 ms |
59596 KB |
Output is correct |
55 |
Correct |
28 ms |
59004 KB |
Output is correct |
56 |
Correct |
99 ms |
77804 KB |
Output is correct |
57 |
Correct |
142 ms |
77244 KB |
Output is correct |
58 |
Correct |
29 ms |
59596 KB |
Output is correct |
59 |
Correct |
28 ms |
58964 KB |
Output is correct |
60 |
Correct |
27 ms |
59068 KB |
Output is correct |
61 |
Correct |
130 ms |
77176 KB |
Output is correct |
62 |
Correct |
29 ms |
59544 KB |
Output is correct |
63 |
Correct |
207 ms |
84208 KB |
Output is correct |
64 |
Correct |
152 ms |
77260 KB |
Output is correct |
65 |
Correct |
29 ms |
59604 KB |
Output is correct |
66 |
Correct |
174 ms |
77916 KB |
Output is correct |
67 |
Correct |
32 ms |
59564 KB |
Output is correct |
68 |
Correct |
29 ms |
59496 KB |
Output is correct |
69 |
Correct |
98 ms |
77756 KB |
Output is correct |
70 |
Correct |
29 ms |
59812 KB |
Output is correct |
71 |
Correct |
128 ms |
77048 KB |
Output is correct |
72 |
Correct |
192 ms |
79440 KB |
Output is correct |
73 |
Correct |
27 ms |
59028 KB |
Output is correct |
74 |
Correct |
168 ms |
84384 KB |
Output is correct |
75 |
Correct |
165 ms |
80780 KB |
Output is correct |
76 |
Correct |
29 ms |
59604 KB |
Output is correct |
77 |
Correct |
30 ms |
59620 KB |
Output is correct |
78 |
Correct |
95 ms |
76364 KB |
Output is correct |
79 |
Correct |
158 ms |
84588 KB |
Output is correct |
80 |
Correct |
34 ms |
59732 KB |
Output is correct |
81 |
Correct |
30 ms |
59604 KB |
Output is correct |
82 |
Correct |
28 ms |
59036 KB |
Output is correct |
83 |
Correct |
28 ms |
59064 KB |
Output is correct |
84 |
Correct |
29 ms |
59528 KB |
Output is correct |
85 |
Correct |
146 ms |
78988 KB |
Output is correct |
86 |
Correct |
31 ms |
59596 KB |
Output is correct |
87 |
Correct |
136 ms |
77344 KB |
Output is correct |
88 |
Correct |
27 ms |
58968 KB |
Output is correct |
89 |
Correct |
150 ms |
77224 KB |
Output is correct |
90 |
Correct |
147 ms |
79836 KB |
Output is correct |
91 |
Correct |
159 ms |
86064 KB |
Output is correct |
92 |
Correct |
108 ms |
77764 KB |
Output is correct |
93 |
Correct |
31 ms |
59672 KB |
Output is correct |
94 |
Correct |
29 ms |
59072 KB |
Output is correct |
95 |
Correct |
151 ms |
84880 KB |
Output is correct |
96 |
Correct |
181 ms |
89968 KB |
Output is correct |
97 |
Correct |
29 ms |
59556 KB |
Output is correct |
98 |
Correct |
28 ms |
59092 KB |
Output is correct |
99 |
Correct |
118 ms |
79948 KB |
Output is correct |
100 |
Correct |
31 ms |
59604 KB |
Output is correct |
101 |
Correct |
144 ms |
78272 KB |
Output is correct |
102 |
Correct |
183 ms |
88764 KB |
Output is correct |
103 |
Correct |
32 ms |
59744 KB |
Output is correct |
104 |
Correct |
31 ms |
59904 KB |
Output is correct |
105 |
Correct |
44 ms |
59616 KB |
Output is correct |
106 |
Correct |
29 ms |
59636 KB |
Output is correct |
107 |
Correct |
30 ms |
59612 KB |
Output is correct |
108 |
Correct |
677 ms |
154152 KB |
Output is correct |
109 |
Correct |
1410 ms |
161704 KB |
Output is correct |
110 |
Correct |
1436 ms |
172848 KB |
Output is correct |
111 |
Correct |
1205 ms |
209308 KB |
Output is correct |
112 |
Correct |
1382 ms |
187484 KB |
Output is correct |
113 |
Correct |
1137 ms |
182836 KB |
Output is correct |
114 |
Correct |
858 ms |
144252 KB |
Output is correct |
115 |
Correct |
846 ms |
144216 KB |
Output is correct |
116 |
Correct |
985 ms |
146620 KB |
Output is correct |
117 |
Correct |
1071 ms |
188776 KB |
Output is correct |
118 |
Correct |
846 ms |
143636 KB |
Output is correct |
119 |
Correct |
1059 ms |
188616 KB |
Output is correct |
120 |
Correct |
1225 ms |
202768 KB |
Output is correct |
121 |
Correct |
1043 ms |
188468 KB |
Output is correct |
122 |
Correct |
971 ms |
157088 KB |
Output is correct |
123 |
Correct |
1038 ms |
188076 KB |
Output is correct |
124 |
Correct |
1069 ms |
175340 KB |
Output is correct |