#include <bits/stdc++.h>
using namespace std;
const int mxN=5e5;
int n, k, tin[mxN], low[mxN], dt, ci, d[mxN], ans, who[mxN], st[2*mxN], sth;
vector<array<int, 2>> adj[mxN];
vector<int> bs[mxN];
void dfs1(int u=0, int p=-1) {
tin[u]=dt++;
for(array<int, 2> v : adj[u])
if(v[1]^p)
dfs1(v[0], v[1]);
}
void dfs2(int u=0, int p=-1) {
tin[u]=low[u]=dt++;
st[sth++]=u;
for(array<int, 2> v : adj[u]) {
if(v[1]==p)
continue;
if(!tin[v[0]]) {
dfs2(v[0], v[1]);
low[u]=min(low[v[0]], low[u]);
} else
low[u]=min(tin[v[0]], low[u]);
}
if(low[u]==tin[u]) {
do
who[st[sth-1]]=ci;
while(st[--sth]^u);
++ci;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for(int i=1, u, v; i<n; ++i) {
cin >> u >> v, --u, --v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
for(int i=0, si; i<n; ++i)
cin >> si, bs[si-1].push_back(i);
dfs1();
for(int i=0; i<k; ++i) {
sort(bs[i].begin(), bs[i].end(), [](int i, int j) {
return tin[i]<tin[j];
});
for(int j=0; j+1<bs[i].size(); ++j) {
adj[bs[i][j]].push_back({bs[i][j+1], dt});
adj[bs[i][j+1]].push_back({bs[i][j], dt++});
}
}
memset(tin, 0, 4*n);
dfs2();
for(int i=0; i<n; ++i)
for(array<int, 2> j : adj[i])
if(who[i]^who[j[0]])
++d[who[i]];
for(int i=0; i<ci; ++i)
ans+=d[i]==1;
cout << (ans+1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:53:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j+1<bs[i].size(); ++j) {
~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23808 KB |
Output is correct |
2 |
Correct |
23 ms |
23808 KB |
Output is correct |
3 |
Correct |
22 ms |
23936 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
22 ms |
23808 KB |
Output is correct |
6 |
Correct |
23 ms |
23936 KB |
Output is correct |
7 |
Correct |
23 ms |
23936 KB |
Output is correct |
8 |
Correct |
24 ms |
23936 KB |
Output is correct |
9 |
Correct |
23 ms |
23936 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
11 |
Correct |
23 ms |
23936 KB |
Output is correct |
12 |
Correct |
24 ms |
23936 KB |
Output is correct |
13 |
Correct |
23 ms |
23808 KB |
Output is correct |
14 |
Correct |
23 ms |
23936 KB |
Output is correct |
15 |
Correct |
23 ms |
23808 KB |
Output is correct |
16 |
Correct |
24 ms |
23808 KB |
Output is correct |
17 |
Correct |
36 ms |
23936 KB |
Output is correct |
18 |
Correct |
23 ms |
23808 KB |
Output is correct |
19 |
Correct |
23 ms |
24064 KB |
Output is correct |
20 |
Correct |
23 ms |
23804 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23936 KB |
Output is correct |
24 |
Correct |
23 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23808 KB |
Output is correct |
2 |
Correct |
23 ms |
23808 KB |
Output is correct |
3 |
Correct |
22 ms |
23936 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
22 ms |
23808 KB |
Output is correct |
6 |
Correct |
23 ms |
23936 KB |
Output is correct |
7 |
Correct |
23 ms |
23936 KB |
Output is correct |
8 |
Correct |
24 ms |
23936 KB |
Output is correct |
9 |
Correct |
23 ms |
23936 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
11 |
Correct |
23 ms |
23936 KB |
Output is correct |
12 |
Correct |
24 ms |
23936 KB |
Output is correct |
13 |
Correct |
23 ms |
23808 KB |
Output is correct |
14 |
Correct |
23 ms |
23936 KB |
Output is correct |
15 |
Correct |
23 ms |
23808 KB |
Output is correct |
16 |
Correct |
24 ms |
23808 KB |
Output is correct |
17 |
Correct |
36 ms |
23936 KB |
Output is correct |
18 |
Correct |
23 ms |
23808 KB |
Output is correct |
19 |
Correct |
23 ms |
24064 KB |
Output is correct |
20 |
Correct |
23 ms |
23804 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23936 KB |
Output is correct |
24 |
Correct |
23 ms |
23936 KB |
Output is correct |
25 |
Correct |
23 ms |
23936 KB |
Output is correct |
26 |
Correct |
25 ms |
24184 KB |
Output is correct |
27 |
Correct |
25 ms |
24192 KB |
Output is correct |
28 |
Correct |
26 ms |
24312 KB |
Output is correct |
29 |
Correct |
25 ms |
24192 KB |
Output is correct |
30 |
Correct |
25 ms |
24320 KB |
Output is correct |
31 |
Correct |
22 ms |
23952 KB |
Output is correct |
32 |
Correct |
26 ms |
24320 KB |
Output is correct |
33 |
Correct |
21 ms |
23936 KB |
Output is correct |
34 |
Correct |
23 ms |
24192 KB |
Output is correct |
35 |
Correct |
24 ms |
24100 KB |
Output is correct |
36 |
Correct |
25 ms |
24192 KB |
Output is correct |
37 |
Correct |
24 ms |
24184 KB |
Output is correct |
38 |
Correct |
22 ms |
23800 KB |
Output is correct |
39 |
Correct |
25 ms |
24320 KB |
Output is correct |
40 |
Correct |
24 ms |
24320 KB |
Output is correct |
41 |
Correct |
24 ms |
24192 KB |
Output is correct |
42 |
Correct |
24 ms |
24192 KB |
Output is correct |
43 |
Correct |
25 ms |
24320 KB |
Output is correct |
44 |
Correct |
22 ms |
23936 KB |
Output is correct |
45 |
Correct |
24 ms |
24184 KB |
Output is correct |
46 |
Correct |
24 ms |
24192 KB |
Output is correct |
47 |
Correct |
23 ms |
23808 KB |
Output is correct |
48 |
Correct |
25 ms |
24184 KB |
Output is correct |
49 |
Correct |
24 ms |
24312 KB |
Output is correct |
50 |
Correct |
24 ms |
24192 KB |
Output is correct |
51 |
Correct |
24 ms |
24192 KB |
Output is correct |
52 |
Correct |
23 ms |
24192 KB |
Output is correct |
53 |
Correct |
24 ms |
24120 KB |
Output is correct |
54 |
Correct |
24 ms |
24320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23808 KB |
Output is correct |
2 |
Correct |
23 ms |
23808 KB |
Output is correct |
3 |
Correct |
22 ms |
23936 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
22 ms |
23808 KB |
Output is correct |
6 |
Correct |
23 ms |
23936 KB |
Output is correct |
7 |
Correct |
23 ms |
23936 KB |
Output is correct |
8 |
Correct |
24 ms |
23936 KB |
Output is correct |
9 |
Correct |
23 ms |
23936 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
11 |
Correct |
23 ms |
23936 KB |
Output is correct |
12 |
Correct |
24 ms |
23936 KB |
Output is correct |
13 |
Correct |
23 ms |
23808 KB |
Output is correct |
14 |
Correct |
23 ms |
23936 KB |
Output is correct |
15 |
Correct |
23 ms |
23808 KB |
Output is correct |
16 |
Correct |
24 ms |
23808 KB |
Output is correct |
17 |
Correct |
36 ms |
23936 KB |
Output is correct |
18 |
Correct |
23 ms |
23808 KB |
Output is correct |
19 |
Correct |
23 ms |
24064 KB |
Output is correct |
20 |
Correct |
23 ms |
23804 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23936 KB |
Output is correct |
24 |
Correct |
23 ms |
23936 KB |
Output is correct |
25 |
Correct |
22 ms |
23800 KB |
Output is correct |
26 |
Correct |
131 ms |
33868 KB |
Output is correct |
27 |
Correct |
186 ms |
38108 KB |
Output is correct |
28 |
Correct |
26 ms |
24184 KB |
Output is correct |
29 |
Correct |
23 ms |
23808 KB |
Output is correct |
30 |
Correct |
22 ms |
23936 KB |
Output is correct |
31 |
Correct |
158 ms |
34168 KB |
Output is correct |
32 |
Correct |
25 ms |
24192 KB |
Output is correct |
33 |
Correct |
170 ms |
39256 KB |
Output is correct |
34 |
Correct |
154 ms |
36472 KB |
Output is correct |
35 |
Correct |
25 ms |
24184 KB |
Output is correct |
36 |
Correct |
170 ms |
33328 KB |
Output is correct |
37 |
Correct |
25 ms |
24448 KB |
Output is correct |
38 |
Correct |
26 ms |
24192 KB |
Output is correct |
39 |
Correct |
150 ms |
33768 KB |
Output is correct |
40 |
Correct |
26 ms |
24312 KB |
Output is correct |
41 |
Correct |
163 ms |
39988 KB |
Output is correct |
42 |
Correct |
172 ms |
38264 KB |
Output is correct |
43 |
Correct |
24 ms |
23936 KB |
Output is correct |
44 |
Correct |
165 ms |
39024 KB |
Output is correct |
45 |
Correct |
169 ms |
38776 KB |
Output is correct |
46 |
Correct |
26 ms |
24184 KB |
Output is correct |
47 |
Correct |
24 ms |
24312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
33772 KB |
Output is correct |
2 |
Correct |
93 ms |
34284 KB |
Output is correct |
3 |
Correct |
24 ms |
24192 KB |
Output is correct |
4 |
Correct |
24 ms |
24312 KB |
Output is correct |
5 |
Correct |
22 ms |
23808 KB |
Output is correct |
6 |
Correct |
22 ms |
23936 KB |
Output is correct |
7 |
Correct |
24 ms |
24192 KB |
Output is correct |
8 |
Correct |
136 ms |
33528 KB |
Output is correct |
9 |
Correct |
25 ms |
24184 KB |
Output is correct |
10 |
Correct |
136 ms |
33616 KB |
Output is correct |
11 |
Correct |
22 ms |
23936 KB |
Output is correct |
12 |
Correct |
152 ms |
32764 KB |
Output is correct |
13 |
Correct |
162 ms |
33560 KB |
Output is correct |
14 |
Correct |
113 ms |
34168 KB |
Output is correct |
15 |
Correct |
144 ms |
33932 KB |
Output is correct |
16 |
Correct |
25 ms |
24192 KB |
Output is correct |
17 |
Correct |
22 ms |
23808 KB |
Output is correct |
18 |
Correct |
99 ms |
33896 KB |
Output is correct |
19 |
Correct |
147 ms |
36860 KB |
Output is correct |
20 |
Correct |
24 ms |
24184 KB |
Output is correct |
21 |
Correct |
22 ms |
23936 KB |
Output is correct |
22 |
Correct |
113 ms |
33236 KB |
Output is correct |
23 |
Correct |
24 ms |
24192 KB |
Output is correct |
24 |
Correct |
142 ms |
33144 KB |
Output is correct |
25 |
Correct |
130 ms |
36600 KB |
Output is correct |
26 |
Correct |
24 ms |
24192 KB |
Output is correct |
27 |
Correct |
24 ms |
24192 KB |
Output is correct |
28 |
Correct |
25 ms |
24192 KB |
Output is correct |
29 |
Correct |
24 ms |
24192 KB |
Output is correct |
30 |
Correct |
25 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23808 KB |
Output is correct |
2 |
Correct |
23 ms |
23808 KB |
Output is correct |
3 |
Correct |
22 ms |
23936 KB |
Output is correct |
4 |
Correct |
21 ms |
23808 KB |
Output is correct |
5 |
Correct |
22 ms |
23808 KB |
Output is correct |
6 |
Correct |
23 ms |
23936 KB |
Output is correct |
7 |
Correct |
23 ms |
23936 KB |
Output is correct |
8 |
Correct |
24 ms |
23936 KB |
Output is correct |
9 |
Correct |
23 ms |
23936 KB |
Output is correct |
10 |
Correct |
22 ms |
23808 KB |
Output is correct |
11 |
Correct |
23 ms |
23936 KB |
Output is correct |
12 |
Correct |
24 ms |
23936 KB |
Output is correct |
13 |
Correct |
23 ms |
23808 KB |
Output is correct |
14 |
Correct |
23 ms |
23936 KB |
Output is correct |
15 |
Correct |
23 ms |
23808 KB |
Output is correct |
16 |
Correct |
24 ms |
23808 KB |
Output is correct |
17 |
Correct |
36 ms |
23936 KB |
Output is correct |
18 |
Correct |
23 ms |
23808 KB |
Output is correct |
19 |
Correct |
23 ms |
24064 KB |
Output is correct |
20 |
Correct |
23 ms |
23804 KB |
Output is correct |
21 |
Correct |
23 ms |
23936 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
23 ms |
23936 KB |
Output is correct |
24 |
Correct |
23 ms |
23936 KB |
Output is correct |
25 |
Correct |
23 ms |
23936 KB |
Output is correct |
26 |
Correct |
25 ms |
24184 KB |
Output is correct |
27 |
Correct |
25 ms |
24192 KB |
Output is correct |
28 |
Correct |
26 ms |
24312 KB |
Output is correct |
29 |
Correct |
25 ms |
24192 KB |
Output is correct |
30 |
Correct |
25 ms |
24320 KB |
Output is correct |
31 |
Correct |
22 ms |
23952 KB |
Output is correct |
32 |
Correct |
26 ms |
24320 KB |
Output is correct |
33 |
Correct |
21 ms |
23936 KB |
Output is correct |
34 |
Correct |
23 ms |
24192 KB |
Output is correct |
35 |
Correct |
24 ms |
24100 KB |
Output is correct |
36 |
Correct |
25 ms |
24192 KB |
Output is correct |
37 |
Correct |
24 ms |
24184 KB |
Output is correct |
38 |
Correct |
22 ms |
23800 KB |
Output is correct |
39 |
Correct |
25 ms |
24320 KB |
Output is correct |
40 |
Correct |
24 ms |
24320 KB |
Output is correct |
41 |
Correct |
24 ms |
24192 KB |
Output is correct |
42 |
Correct |
24 ms |
24192 KB |
Output is correct |
43 |
Correct |
25 ms |
24320 KB |
Output is correct |
44 |
Correct |
22 ms |
23936 KB |
Output is correct |
45 |
Correct |
24 ms |
24184 KB |
Output is correct |
46 |
Correct |
24 ms |
24192 KB |
Output is correct |
47 |
Correct |
23 ms |
23808 KB |
Output is correct |
48 |
Correct |
25 ms |
24184 KB |
Output is correct |
49 |
Correct |
24 ms |
24312 KB |
Output is correct |
50 |
Correct |
24 ms |
24192 KB |
Output is correct |
51 |
Correct |
24 ms |
24192 KB |
Output is correct |
52 |
Correct |
23 ms |
24192 KB |
Output is correct |
53 |
Correct |
24 ms |
24120 KB |
Output is correct |
54 |
Correct |
24 ms |
24320 KB |
Output is correct |
55 |
Correct |
22 ms |
23800 KB |
Output is correct |
56 |
Correct |
131 ms |
33868 KB |
Output is correct |
57 |
Correct |
186 ms |
38108 KB |
Output is correct |
58 |
Correct |
26 ms |
24184 KB |
Output is correct |
59 |
Correct |
23 ms |
23808 KB |
Output is correct |
60 |
Correct |
22 ms |
23936 KB |
Output is correct |
61 |
Correct |
158 ms |
34168 KB |
Output is correct |
62 |
Correct |
25 ms |
24192 KB |
Output is correct |
63 |
Correct |
170 ms |
39256 KB |
Output is correct |
64 |
Correct |
154 ms |
36472 KB |
Output is correct |
65 |
Correct |
25 ms |
24184 KB |
Output is correct |
66 |
Correct |
170 ms |
33328 KB |
Output is correct |
67 |
Correct |
25 ms |
24448 KB |
Output is correct |
68 |
Correct |
26 ms |
24192 KB |
Output is correct |
69 |
Correct |
150 ms |
33768 KB |
Output is correct |
70 |
Correct |
26 ms |
24312 KB |
Output is correct |
71 |
Correct |
163 ms |
39988 KB |
Output is correct |
72 |
Correct |
172 ms |
38264 KB |
Output is correct |
73 |
Correct |
24 ms |
23936 KB |
Output is correct |
74 |
Correct |
165 ms |
39024 KB |
Output is correct |
75 |
Correct |
169 ms |
38776 KB |
Output is correct |
76 |
Correct |
26 ms |
24184 KB |
Output is correct |
77 |
Correct |
24 ms |
24312 KB |
Output is correct |
78 |
Correct |
108 ms |
33772 KB |
Output is correct |
79 |
Correct |
93 ms |
34284 KB |
Output is correct |
80 |
Correct |
24 ms |
24192 KB |
Output is correct |
81 |
Correct |
24 ms |
24312 KB |
Output is correct |
82 |
Correct |
22 ms |
23808 KB |
Output is correct |
83 |
Correct |
22 ms |
23936 KB |
Output is correct |
84 |
Correct |
24 ms |
24192 KB |
Output is correct |
85 |
Correct |
136 ms |
33528 KB |
Output is correct |
86 |
Correct |
25 ms |
24184 KB |
Output is correct |
87 |
Correct |
136 ms |
33616 KB |
Output is correct |
88 |
Correct |
22 ms |
23936 KB |
Output is correct |
89 |
Correct |
152 ms |
32764 KB |
Output is correct |
90 |
Correct |
162 ms |
33560 KB |
Output is correct |
91 |
Correct |
113 ms |
34168 KB |
Output is correct |
92 |
Correct |
144 ms |
33932 KB |
Output is correct |
93 |
Correct |
25 ms |
24192 KB |
Output is correct |
94 |
Correct |
22 ms |
23808 KB |
Output is correct |
95 |
Correct |
99 ms |
33896 KB |
Output is correct |
96 |
Correct |
147 ms |
36860 KB |
Output is correct |
97 |
Correct |
24 ms |
24184 KB |
Output is correct |
98 |
Correct |
22 ms |
23936 KB |
Output is correct |
99 |
Correct |
113 ms |
33236 KB |
Output is correct |
100 |
Correct |
24 ms |
24192 KB |
Output is correct |
101 |
Correct |
142 ms |
33144 KB |
Output is correct |
102 |
Correct |
130 ms |
36600 KB |
Output is correct |
103 |
Correct |
24 ms |
24192 KB |
Output is correct |
104 |
Correct |
24 ms |
24192 KB |
Output is correct |
105 |
Correct |
25 ms |
24192 KB |
Output is correct |
106 |
Correct |
24 ms |
24192 KB |
Output is correct |
107 |
Correct |
25 ms |
24184 KB |
Output is correct |
108 |
Correct |
871 ms |
72832 KB |
Output is correct |
109 |
Correct |
1126 ms |
90548 KB |
Output is correct |
110 |
Correct |
1192 ms |
100476 KB |
Output is correct |
111 |
Correct |
1016 ms |
93048 KB |
Output is correct |
112 |
Correct |
998 ms |
86392 KB |
Output is correct |
113 |
Correct |
674 ms |
75732 KB |
Output is correct |
114 |
Correct |
1174 ms |
102776 KB |
Output is correct |
115 |
Correct |
1211 ms |
104676 KB |
Output is correct |
116 |
Correct |
1169 ms |
75228 KB |
Output is correct |
117 |
Correct |
1002 ms |
76080 KB |
Output is correct |
118 |
Correct |
1129 ms |
72740 KB |
Output is correct |
119 |
Correct |
913 ms |
76024 KB |
Output is correct |
120 |
Correct |
967 ms |
89464 KB |
Output is correct |
121 |
Correct |
893 ms |
76280 KB |
Output is correct |
122 |
Correct |
1106 ms |
89080 KB |
Output is correct |
123 |
Correct |
586 ms |
77024 KB |
Output is correct |
124 |
Correct |
1014 ms |
71928 KB |
Output is correct |