#include<bits/stdc++.h>
#define f first
#define s second
//#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 5e5 + 5, mod = 1e9 + 7; // !
int t, p[N], P[N][20], X[N], in[N], out[N];
int ans = 0, timer = 0;
vector<int> V[N], x[N];
bool cmp(int u, int v) {
return (in[u] < in[v]);
}
int find(int x) {
return (p[x] == x ? x : p[x] = find(p[x]));
}
void dfs0(int u) {
in[u] = ++timer;
for(int i = 1; i <= 18; i++) P[u][i] = P[P[u][i - 1]][i - 1];
for(int i = 0; i < V[u].size(); i++) {
if(V[u][i] == P[u][0]) continue;
P[V[u][i]][0] = u;
dfs0(V[u][i]);
}
out[u] = timer;
}
bool check(int u, int v) {
return (in[u] <= in[v] && out[v] <= out[u]);
}
int lca(int u, int v) {
if(check(u, v)) return u;
if(check(v, u)) return v;
for(int i = 18; i >= 0; i--) {
if(P[u][i] && !check(P[u][i], v)) u = P[u][i];
}
return P[u][0];
}
void up(int u, int v, int lca) {
int cur = u;
while(true) {
cur = find(cur);
if(cur == 1 || find(cur) == find(lca)) break;
p[cur] = find(P[cur][0]);
cur = p[cur];
}
}
void dfs(int u) {
for(int i = 0; i < V[u].size(); i++) {
dfs(V[u][i]);
X[u] += X[V[u][i]];
}
ans += ((int)V[u].size() + (u - 1 > 0) == 1);
}
main(){
int n, m;
cin >> n >> m;
for(int i =1; i <= n; i++) p[i] = i;
for(int i = 2; i <= n; i++) {
int u, v;
cin >> u >> v;
V[u].push_back(v);
V[v].push_back(u);
}
dfs0(1);
for(int i = 1; i <= n; i++) {
int a; cin >> a;
x[a].push_back(i);
}
for(int i = 1; i <= m; i++) {
sort(x[i].begin(), x[i].end(), cmp);
for(int j = 1; j < x[i].size(); j++) {
int u = x[i][j - 1], v = x[i][j];
int l = lca(u, v);
up(u, v, l);
up(v, u, l);
}
}
for(int i = 1; i <= n; i++) V[i].clear();
for(int i = 2; i <= n; i++) if(find(i) != find(P[i][0])) V[find(P[i][0])].push_back(find(i));
dfs(1);
cout << (ans + 1) /2;
}
Compilation message
mergers.cpp: In function 'void dfs0(int)':
mergers.cpp:21:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
mergers.cpp: In function 'void dfs(int)':
mergers.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
mergers.cpp: At global scope:
mergers.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | main(){
| ^~~~
mergers.cpp: In function 'int main()':
mergers.cpp:76:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int j = 1; j < x[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23744 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23820 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23780 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
13 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23748 KB |
Output is correct |
22 |
Correct |
12 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23744 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23820 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23780 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
13 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23748 KB |
Output is correct |
22 |
Correct |
12 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
17 ms |
24188 KB |
Output is correct |
27 |
Correct |
15 ms |
24120 KB |
Output is correct |
28 |
Correct |
15 ms |
24276 KB |
Output is correct |
29 |
Correct |
15 ms |
24220 KB |
Output is correct |
30 |
Correct |
16 ms |
24148 KB |
Output is correct |
31 |
Correct |
12 ms |
23792 KB |
Output is correct |
32 |
Correct |
16 ms |
24380 KB |
Output is correct |
33 |
Correct |
12 ms |
23716 KB |
Output is correct |
34 |
Correct |
14 ms |
24136 KB |
Output is correct |
35 |
Correct |
15 ms |
24216 KB |
Output is correct |
36 |
Correct |
15 ms |
24168 KB |
Output is correct |
37 |
Correct |
16 ms |
24148 KB |
Output is correct |
38 |
Correct |
12 ms |
23752 KB |
Output is correct |
39 |
Correct |
15 ms |
24228 KB |
Output is correct |
40 |
Correct |
15 ms |
24212 KB |
Output is correct |
41 |
Correct |
15 ms |
24180 KB |
Output is correct |
42 |
Correct |
15 ms |
24268 KB |
Output is correct |
43 |
Correct |
15 ms |
24276 KB |
Output is correct |
44 |
Correct |
14 ms |
23740 KB |
Output is correct |
45 |
Correct |
15 ms |
24276 KB |
Output is correct |
46 |
Correct |
16 ms |
24148 KB |
Output is correct |
47 |
Correct |
12 ms |
23724 KB |
Output is correct |
48 |
Correct |
15 ms |
24148 KB |
Output is correct |
49 |
Correct |
15 ms |
24308 KB |
Output is correct |
50 |
Correct |
16 ms |
24276 KB |
Output is correct |
51 |
Correct |
15 ms |
24240 KB |
Output is correct |
52 |
Correct |
15 ms |
24232 KB |
Output is correct |
53 |
Correct |
15 ms |
24228 KB |
Output is correct |
54 |
Correct |
15 ms |
24148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23744 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23820 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23780 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
13 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23748 KB |
Output is correct |
22 |
Correct |
12 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23724 KB |
Output is correct |
26 |
Correct |
122 ms |
36796 KB |
Output is correct |
27 |
Correct |
143 ms |
36504 KB |
Output is correct |
28 |
Correct |
14 ms |
24148 KB |
Output is correct |
29 |
Correct |
13 ms |
23764 KB |
Output is correct |
30 |
Correct |
14 ms |
23892 KB |
Output is correct |
31 |
Correct |
149 ms |
36556 KB |
Output is correct |
32 |
Correct |
15 ms |
24216 KB |
Output is correct |
33 |
Correct |
156 ms |
42228 KB |
Output is correct |
34 |
Correct |
150 ms |
36556 KB |
Output is correct |
35 |
Correct |
15 ms |
24148 KB |
Output is correct |
36 |
Correct |
163 ms |
37180 KB |
Output is correct |
37 |
Correct |
15 ms |
24148 KB |
Output is correct |
38 |
Correct |
16 ms |
24228 KB |
Output is correct |
39 |
Correct |
124 ms |
36800 KB |
Output is correct |
40 |
Correct |
18 ms |
24276 KB |
Output is correct |
41 |
Correct |
140 ms |
36584 KB |
Output is correct |
42 |
Correct |
155 ms |
38348 KB |
Output is correct |
43 |
Correct |
13 ms |
23764 KB |
Output is correct |
44 |
Correct |
158 ms |
42620 KB |
Output is correct |
45 |
Correct |
176 ms |
39568 KB |
Output is correct |
46 |
Correct |
15 ms |
24216 KB |
Output is correct |
47 |
Correct |
16 ms |
24204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
36808 KB |
Output is correct |
2 |
Correct |
131 ms |
39528 KB |
Output is correct |
3 |
Correct |
16 ms |
24260 KB |
Output is correct |
4 |
Correct |
17 ms |
24124 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
15 ms |
24232 KB |
Output is correct |
8 |
Correct |
158 ms |
38108 KB |
Output is correct |
9 |
Correct |
16 ms |
24148 KB |
Output is correct |
10 |
Correct |
157 ms |
37016 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
149 ms |
36936 KB |
Output is correct |
13 |
Correct |
154 ms |
37928 KB |
Output is correct |
14 |
Correct |
151 ms |
39492 KB |
Output is correct |
15 |
Correct |
124 ms |
36808 KB |
Output is correct |
16 |
Correct |
16 ms |
24260 KB |
Output is correct |
17 |
Correct |
13 ms |
23764 KB |
Output is correct |
18 |
Correct |
140 ms |
39392 KB |
Output is correct |
19 |
Correct |
164 ms |
42668 KB |
Output is correct |
20 |
Correct |
16 ms |
24224 KB |
Output is correct |
21 |
Correct |
15 ms |
23764 KB |
Output is correct |
22 |
Correct |
137 ms |
38120 KB |
Output is correct |
23 |
Correct |
15 ms |
24204 KB |
Output is correct |
24 |
Correct |
150 ms |
37232 KB |
Output is correct |
25 |
Correct |
165 ms |
41552 KB |
Output is correct |
26 |
Correct |
15 ms |
24264 KB |
Output is correct |
27 |
Correct |
15 ms |
24340 KB |
Output is correct |
28 |
Correct |
15 ms |
24124 KB |
Output is correct |
29 |
Correct |
15 ms |
24148 KB |
Output is correct |
30 |
Correct |
15 ms |
24276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23828 KB |
Output is correct |
4 |
Correct |
12 ms |
23744 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23728 KB |
Output is correct |
8 |
Correct |
13 ms |
23780 KB |
Output is correct |
9 |
Correct |
12 ms |
23820 KB |
Output is correct |
10 |
Correct |
13 ms |
23740 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
12 ms |
23748 KB |
Output is correct |
15 |
Correct |
13 ms |
23764 KB |
Output is correct |
16 |
Correct |
14 ms |
23704 KB |
Output is correct |
17 |
Correct |
12 ms |
23780 KB |
Output is correct |
18 |
Correct |
12 ms |
23752 KB |
Output is correct |
19 |
Correct |
13 ms |
23784 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
23748 KB |
Output is correct |
22 |
Correct |
12 ms |
23812 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23764 KB |
Output is correct |
26 |
Correct |
17 ms |
24188 KB |
Output is correct |
27 |
Correct |
15 ms |
24120 KB |
Output is correct |
28 |
Correct |
15 ms |
24276 KB |
Output is correct |
29 |
Correct |
15 ms |
24220 KB |
Output is correct |
30 |
Correct |
16 ms |
24148 KB |
Output is correct |
31 |
Correct |
12 ms |
23792 KB |
Output is correct |
32 |
Correct |
16 ms |
24380 KB |
Output is correct |
33 |
Correct |
12 ms |
23716 KB |
Output is correct |
34 |
Correct |
14 ms |
24136 KB |
Output is correct |
35 |
Correct |
15 ms |
24216 KB |
Output is correct |
36 |
Correct |
15 ms |
24168 KB |
Output is correct |
37 |
Correct |
16 ms |
24148 KB |
Output is correct |
38 |
Correct |
12 ms |
23752 KB |
Output is correct |
39 |
Correct |
15 ms |
24228 KB |
Output is correct |
40 |
Correct |
15 ms |
24212 KB |
Output is correct |
41 |
Correct |
15 ms |
24180 KB |
Output is correct |
42 |
Correct |
15 ms |
24268 KB |
Output is correct |
43 |
Correct |
15 ms |
24276 KB |
Output is correct |
44 |
Correct |
14 ms |
23740 KB |
Output is correct |
45 |
Correct |
15 ms |
24276 KB |
Output is correct |
46 |
Correct |
16 ms |
24148 KB |
Output is correct |
47 |
Correct |
12 ms |
23724 KB |
Output is correct |
48 |
Correct |
15 ms |
24148 KB |
Output is correct |
49 |
Correct |
15 ms |
24308 KB |
Output is correct |
50 |
Correct |
16 ms |
24276 KB |
Output is correct |
51 |
Correct |
15 ms |
24240 KB |
Output is correct |
52 |
Correct |
15 ms |
24232 KB |
Output is correct |
53 |
Correct |
15 ms |
24228 KB |
Output is correct |
54 |
Correct |
15 ms |
24148 KB |
Output is correct |
55 |
Correct |
12 ms |
23724 KB |
Output is correct |
56 |
Correct |
122 ms |
36796 KB |
Output is correct |
57 |
Correct |
143 ms |
36504 KB |
Output is correct |
58 |
Correct |
14 ms |
24148 KB |
Output is correct |
59 |
Correct |
13 ms |
23764 KB |
Output is correct |
60 |
Correct |
14 ms |
23892 KB |
Output is correct |
61 |
Correct |
149 ms |
36556 KB |
Output is correct |
62 |
Correct |
15 ms |
24216 KB |
Output is correct |
63 |
Correct |
156 ms |
42228 KB |
Output is correct |
64 |
Correct |
150 ms |
36556 KB |
Output is correct |
65 |
Correct |
15 ms |
24148 KB |
Output is correct |
66 |
Correct |
163 ms |
37180 KB |
Output is correct |
67 |
Correct |
15 ms |
24148 KB |
Output is correct |
68 |
Correct |
16 ms |
24228 KB |
Output is correct |
69 |
Correct |
124 ms |
36800 KB |
Output is correct |
70 |
Correct |
18 ms |
24276 KB |
Output is correct |
71 |
Correct |
140 ms |
36584 KB |
Output is correct |
72 |
Correct |
155 ms |
38348 KB |
Output is correct |
73 |
Correct |
13 ms |
23764 KB |
Output is correct |
74 |
Correct |
158 ms |
42620 KB |
Output is correct |
75 |
Correct |
176 ms |
39568 KB |
Output is correct |
76 |
Correct |
15 ms |
24216 KB |
Output is correct |
77 |
Correct |
16 ms |
24204 KB |
Output is correct |
78 |
Correct |
123 ms |
36808 KB |
Output is correct |
79 |
Correct |
131 ms |
39528 KB |
Output is correct |
80 |
Correct |
16 ms |
24260 KB |
Output is correct |
81 |
Correct |
17 ms |
24124 KB |
Output is correct |
82 |
Correct |
12 ms |
23764 KB |
Output is correct |
83 |
Correct |
14 ms |
23764 KB |
Output is correct |
84 |
Correct |
15 ms |
24232 KB |
Output is correct |
85 |
Correct |
158 ms |
38108 KB |
Output is correct |
86 |
Correct |
16 ms |
24148 KB |
Output is correct |
87 |
Correct |
157 ms |
37016 KB |
Output is correct |
88 |
Correct |
14 ms |
23764 KB |
Output is correct |
89 |
Correct |
149 ms |
36936 KB |
Output is correct |
90 |
Correct |
154 ms |
37928 KB |
Output is correct |
91 |
Correct |
151 ms |
39492 KB |
Output is correct |
92 |
Correct |
124 ms |
36808 KB |
Output is correct |
93 |
Correct |
16 ms |
24260 KB |
Output is correct |
94 |
Correct |
13 ms |
23764 KB |
Output is correct |
95 |
Correct |
140 ms |
39392 KB |
Output is correct |
96 |
Correct |
164 ms |
42668 KB |
Output is correct |
97 |
Correct |
16 ms |
24224 KB |
Output is correct |
98 |
Correct |
15 ms |
23764 KB |
Output is correct |
99 |
Correct |
137 ms |
38120 KB |
Output is correct |
100 |
Correct |
15 ms |
24204 KB |
Output is correct |
101 |
Correct |
150 ms |
37232 KB |
Output is correct |
102 |
Correct |
165 ms |
41552 KB |
Output is correct |
103 |
Correct |
15 ms |
24264 KB |
Output is correct |
104 |
Correct |
15 ms |
24340 KB |
Output is correct |
105 |
Correct |
15 ms |
24124 KB |
Output is correct |
106 |
Correct |
15 ms |
24148 KB |
Output is correct |
107 |
Correct |
15 ms |
24276 KB |
Output is correct |
108 |
Correct |
855 ms |
103040 KB |
Output is correct |
109 |
Correct |
979 ms |
109004 KB |
Output is correct |
110 |
Correct |
999 ms |
117944 KB |
Output is correct |
111 |
Correct |
1182 ms |
130948 KB |
Output is correct |
112 |
Correct |
1120 ms |
124180 KB |
Output is correct |
113 |
Correct |
751 ms |
111628 KB |
Output is correct |
114 |
Correct |
885 ms |
94824 KB |
Output is correct |
115 |
Correct |
884 ms |
94756 KB |
Output is correct |
116 |
Correct |
1057 ms |
99852 KB |
Output is correct |
117 |
Correct |
993 ms |
112460 KB |
Output is correct |
118 |
Correct |
875 ms |
96384 KB |
Output is correct |
119 |
Correct |
992 ms |
112552 KB |
Output is correct |
120 |
Correct |
1144 ms |
123560 KB |
Output is correct |
121 |
Correct |
1035 ms |
112508 KB |
Output is correct |
122 |
Correct |
987 ms |
106724 KB |
Output is correct |
123 |
Correct |
736 ms |
112032 KB |
Output is correct |
124 |
Correct |
1061 ms |
110640 KB |
Output is correct |