#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
vector<int> v;
vector<int> adj[mxn];
int parent[mxn];
int sz[mxn];
int S[mxn];
vector<int> con[mxn];
int level[mxn];
int findrep(int u) {
return parent[u] == u ? u : parent[u] = findrep(parent[u]);
}
void unite(int u,int v) {
u = findrep(u);
v = findrep(v);
if(u == v) return;
if(sz[u] > sz[v]) swap(u,v);
parent[u] = v;
sz[v] += sz[u];
}
bool dfs(int cur,int prev,int src) {
if(cur == src) return true;
bool f = false;
for(int u : adj[cur]) {
if(u == prev) continue;
f |= dfs(u,cur,src);
}
if(f) v.push_back(cur);
return f;
}
void DFS(int cur,int prev) {
level[cur] = level[prev] + 1;
for(int u : adj[cur]) {
if(u != prev) DFS(u,cur);
}
}
int main() {
int n,k;
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++) {
cin >> S[i];
con[S[i]].push_back(i);
}
for(int i = 1; i <= k; i++) {
parent[i] = i;
sz[i] = 1;
}
for(int i = 1; i <= k; i++) {
sort(con[i].begin(),con[i].end(),[](int a,int b) {
return level[a] < level[b];
});
for(int j = 1; j < con[i].size(); j++) {
v.clear();
dfs(con[i][j - 1],-1,con[i][j]);
for(int x : v) {
unite(i,S[x]);
}
}
}
int deg[k + 1] = {};
for(int i = 1; i <= n; i++) {
int x = findrep(S[i]);
for(int u : adj[i]) {
if(x == findrep(S[u])) continue;
deg[findrep(S[u])]++;
}
}
int leaf = 0;
for(int i = 1; i <= k; i++) {
if(parent[i] == i && deg[i] == 1) leaf++;
}
cout<<(leaf + 1) / 2<<endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:78:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j = 1; j < con[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4972 KB |
Output is correct |
2 |
Correct |
4 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
4 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
4 ms |
4972 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
5 ms |
4972 KB |
Output is correct |
12 |
Correct |
4 ms |
4972 KB |
Output is correct |
13 |
Correct |
4 ms |
4972 KB |
Output is correct |
14 |
Correct |
4 ms |
4972 KB |
Output is correct |
15 |
Correct |
5 ms |
4972 KB |
Output is correct |
16 |
Correct |
4 ms |
4972 KB |
Output is correct |
17 |
Correct |
4 ms |
4972 KB |
Output is correct |
18 |
Correct |
4 ms |
4972 KB |
Output is correct |
19 |
Correct |
4 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
4972 KB |
Output is correct |
22 |
Correct |
4 ms |
4972 KB |
Output is correct |
23 |
Correct |
4 ms |
4972 KB |
Output is correct |
24 |
Correct |
4 ms |
4972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4972 KB |
Output is correct |
2 |
Correct |
4 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
4 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
4 ms |
4972 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
5 ms |
4972 KB |
Output is correct |
12 |
Correct |
4 ms |
4972 KB |
Output is correct |
13 |
Correct |
4 ms |
4972 KB |
Output is correct |
14 |
Correct |
4 ms |
4972 KB |
Output is correct |
15 |
Correct |
5 ms |
4972 KB |
Output is correct |
16 |
Correct |
4 ms |
4972 KB |
Output is correct |
17 |
Correct |
4 ms |
4972 KB |
Output is correct |
18 |
Correct |
4 ms |
4972 KB |
Output is correct |
19 |
Correct |
4 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
4972 KB |
Output is correct |
22 |
Correct |
4 ms |
4972 KB |
Output is correct |
23 |
Correct |
4 ms |
4972 KB |
Output is correct |
24 |
Correct |
4 ms |
4972 KB |
Output is correct |
25 |
Correct |
4 ms |
4972 KB |
Output is correct |
26 |
Correct |
34 ms |
5228 KB |
Output is correct |
27 |
Correct |
126 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5356 KB |
Output is correct |
29 |
Correct |
9 ms |
5356 KB |
Output is correct |
30 |
Correct |
129 ms |
5356 KB |
Output is correct |
31 |
Correct |
4 ms |
4972 KB |
Output is correct |
32 |
Correct |
8 ms |
5484 KB |
Output is correct |
33 |
Correct |
4 ms |
5248 KB |
Output is correct |
34 |
Correct |
64 ms |
5356 KB |
Output is correct |
35 |
Correct |
8 ms |
5356 KB |
Output is correct |
36 |
Correct |
128 ms |
5228 KB |
Output is correct |
37 |
Correct |
80 ms |
5228 KB |
Output is correct |
38 |
Correct |
4 ms |
4972 KB |
Output is correct |
39 |
Correct |
75 ms |
5228 KB |
Output is correct |
40 |
Correct |
137 ms |
5228 KB |
Output is correct |
41 |
Correct |
136 ms |
5228 KB |
Output is correct |
42 |
Correct |
34 ms |
5228 KB |
Output is correct |
43 |
Correct |
149 ms |
5356 KB |
Output is correct |
44 |
Correct |
4 ms |
5100 KB |
Output is correct |
45 |
Correct |
11 ms |
5356 KB |
Output is correct |
46 |
Correct |
70 ms |
5292 KB |
Output is correct |
47 |
Correct |
4 ms |
4972 KB |
Output is correct |
48 |
Correct |
92 ms |
5228 KB |
Output is correct |
49 |
Correct |
8 ms |
5356 KB |
Output is correct |
50 |
Correct |
8 ms |
5356 KB |
Output is correct |
51 |
Correct |
90 ms |
5228 KB |
Output is correct |
52 |
Correct |
59 ms |
5228 KB |
Output is correct |
53 |
Correct |
34 ms |
5228 KB |
Output is correct |
54 |
Correct |
137 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4972 KB |
Output is correct |
2 |
Correct |
4 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
4 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
4 ms |
4972 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
5 ms |
4972 KB |
Output is correct |
12 |
Correct |
4 ms |
4972 KB |
Output is correct |
13 |
Correct |
4 ms |
4972 KB |
Output is correct |
14 |
Correct |
4 ms |
4972 KB |
Output is correct |
15 |
Correct |
5 ms |
4972 KB |
Output is correct |
16 |
Correct |
4 ms |
4972 KB |
Output is correct |
17 |
Correct |
4 ms |
4972 KB |
Output is correct |
18 |
Correct |
4 ms |
4972 KB |
Output is correct |
19 |
Correct |
4 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
4972 KB |
Output is correct |
22 |
Correct |
4 ms |
4972 KB |
Output is correct |
23 |
Correct |
4 ms |
4972 KB |
Output is correct |
24 |
Correct |
4 ms |
4972 KB |
Output is correct |
25 |
Correct |
4 ms |
4972 KB |
Output is correct |
26 |
Execution timed out |
3073 ms |
9572 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3070 ms |
9448 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4972 KB |
Output is correct |
2 |
Correct |
4 ms |
4972 KB |
Output is correct |
3 |
Correct |
4 ms |
4972 KB |
Output is correct |
4 |
Correct |
4 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
4 ms |
4972 KB |
Output is correct |
8 |
Correct |
4 ms |
4972 KB |
Output is correct |
9 |
Correct |
4 ms |
4972 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
5 ms |
4972 KB |
Output is correct |
12 |
Correct |
4 ms |
4972 KB |
Output is correct |
13 |
Correct |
4 ms |
4972 KB |
Output is correct |
14 |
Correct |
4 ms |
4972 KB |
Output is correct |
15 |
Correct |
5 ms |
4972 KB |
Output is correct |
16 |
Correct |
4 ms |
4972 KB |
Output is correct |
17 |
Correct |
4 ms |
4972 KB |
Output is correct |
18 |
Correct |
4 ms |
4972 KB |
Output is correct |
19 |
Correct |
4 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
4972 KB |
Output is correct |
21 |
Correct |
4 ms |
4972 KB |
Output is correct |
22 |
Correct |
4 ms |
4972 KB |
Output is correct |
23 |
Correct |
4 ms |
4972 KB |
Output is correct |
24 |
Correct |
4 ms |
4972 KB |
Output is correct |
25 |
Correct |
4 ms |
4972 KB |
Output is correct |
26 |
Correct |
34 ms |
5228 KB |
Output is correct |
27 |
Correct |
126 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5356 KB |
Output is correct |
29 |
Correct |
9 ms |
5356 KB |
Output is correct |
30 |
Correct |
129 ms |
5356 KB |
Output is correct |
31 |
Correct |
4 ms |
4972 KB |
Output is correct |
32 |
Correct |
8 ms |
5484 KB |
Output is correct |
33 |
Correct |
4 ms |
5248 KB |
Output is correct |
34 |
Correct |
64 ms |
5356 KB |
Output is correct |
35 |
Correct |
8 ms |
5356 KB |
Output is correct |
36 |
Correct |
128 ms |
5228 KB |
Output is correct |
37 |
Correct |
80 ms |
5228 KB |
Output is correct |
38 |
Correct |
4 ms |
4972 KB |
Output is correct |
39 |
Correct |
75 ms |
5228 KB |
Output is correct |
40 |
Correct |
137 ms |
5228 KB |
Output is correct |
41 |
Correct |
136 ms |
5228 KB |
Output is correct |
42 |
Correct |
34 ms |
5228 KB |
Output is correct |
43 |
Correct |
149 ms |
5356 KB |
Output is correct |
44 |
Correct |
4 ms |
5100 KB |
Output is correct |
45 |
Correct |
11 ms |
5356 KB |
Output is correct |
46 |
Correct |
70 ms |
5292 KB |
Output is correct |
47 |
Correct |
4 ms |
4972 KB |
Output is correct |
48 |
Correct |
92 ms |
5228 KB |
Output is correct |
49 |
Correct |
8 ms |
5356 KB |
Output is correct |
50 |
Correct |
8 ms |
5356 KB |
Output is correct |
51 |
Correct |
90 ms |
5228 KB |
Output is correct |
52 |
Correct |
59 ms |
5228 KB |
Output is correct |
53 |
Correct |
34 ms |
5228 KB |
Output is correct |
54 |
Correct |
137 ms |
5228 KB |
Output is correct |
55 |
Correct |
4 ms |
4972 KB |
Output is correct |
56 |
Execution timed out |
3073 ms |
9572 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |