#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=5e5+5;
const int inf=1e9;
vector<int> v;
vector<int> adj[mxn];
int S[mxn];
vector<int> con[mxn];
int level[mxn];
int dp[mxn][20];
struct DSU {
int parent[mxn];
void init() {
for(int i = 1; i < mxn; i++) parent[i] = i;
}
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(level[u] < level[v]) swap(u,v);
parent[u] = v;
}
} dsu1,dsu2;
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;
dp[cur][0] = prev;
for(int i = 1; i < 20; i++) dp[cur][i] = dp[dp[cur][i - 1]][i - 1];
for(int u : adj[cur]) {
if(u != prev) {
DFS(u,cur);
if(S[u] == S[cur]) dsu1.unite(u,cur);
}
}
}
int findLCA(int u,int v){
if(level[u] < level[v])swap(u,v);
int diff = level[u] - level[v];
for(int i = 0; i < 20; i++) if((diff>>i) & 1) u = dp[u][i];
if(u == v) return u;
for(int i = 19; i >= 0; i--) {
if(dp[u][i] != dp[v][i]) {
u = dp[u][i];
v = dp[v][i];
}
}
return dp[u][0];
}
void process(int u,int lv) {
int cu = dsu1.findrep(u);
int clv = level[cu];
while(clv > lv) {
int ccu = dsu1.findrep(dp[cu][0]);
dsu1.unite(cu,ccu);
dsu2.unite(S[cu],S[ccu]);
cu = ccu;
clv = level[cu];
}
}
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);
}
DFS(1,0);
dsu1.init();
dsu2.init();
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();
int u = con[i][j];
int v = con[i][j - 1];
int lca = findLCA(u,v);
process(u,level[lca]);
process(v,level[lca]);
}
}
int deg[k + 1] = {};
for(int i = 1; i <= n; i++) {
int x = dsu2.findrep(S[i]);
for(int u : adj[i]) {
if(x == dsu2.findrep(S[u])) continue;
deg[dsu2.findrep(S[u])]++;
}
}
int leaf = 0;
for(int i = 1; i <= k; i++) {
if(dsu2.parent[i] == i && deg[i] == 1) leaf++;
}
cout<<(leaf + 1) / 2<<endl;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:114:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for(int j = 1; j < con[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27756 KB |
Output is correct |
2 |
Correct |
18 ms |
27756 KB |
Output is correct |
3 |
Correct |
18 ms |
27756 KB |
Output is correct |
4 |
Correct |
20 ms |
27756 KB |
Output is correct |
5 |
Correct |
18 ms |
27756 KB |
Output is correct |
6 |
Correct |
18 ms |
27756 KB |
Output is correct |
7 |
Correct |
22 ms |
27756 KB |
Output is correct |
8 |
Correct |
18 ms |
27884 KB |
Output is correct |
9 |
Correct |
18 ms |
27756 KB |
Output is correct |
10 |
Correct |
18 ms |
27756 KB |
Output is correct |
11 |
Correct |
18 ms |
27756 KB |
Output is correct |
12 |
Correct |
18 ms |
27756 KB |
Output is correct |
13 |
Correct |
18 ms |
27756 KB |
Output is correct |
14 |
Correct |
18 ms |
27756 KB |
Output is correct |
15 |
Correct |
18 ms |
27884 KB |
Output is correct |
16 |
Correct |
19 ms |
27756 KB |
Output is correct |
17 |
Correct |
18 ms |
27756 KB |
Output is correct |
18 |
Correct |
18 ms |
27756 KB |
Output is correct |
19 |
Correct |
18 ms |
27756 KB |
Output is correct |
20 |
Correct |
18 ms |
27756 KB |
Output is correct |
21 |
Correct |
19 ms |
27756 KB |
Output is correct |
22 |
Correct |
18 ms |
27756 KB |
Output is correct |
23 |
Correct |
19 ms |
27756 KB |
Output is correct |
24 |
Correct |
19 ms |
27756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27756 KB |
Output is correct |
2 |
Correct |
18 ms |
27756 KB |
Output is correct |
3 |
Correct |
18 ms |
27756 KB |
Output is correct |
4 |
Correct |
20 ms |
27756 KB |
Output is correct |
5 |
Correct |
18 ms |
27756 KB |
Output is correct |
6 |
Correct |
18 ms |
27756 KB |
Output is correct |
7 |
Correct |
22 ms |
27756 KB |
Output is correct |
8 |
Correct |
18 ms |
27884 KB |
Output is correct |
9 |
Correct |
18 ms |
27756 KB |
Output is correct |
10 |
Correct |
18 ms |
27756 KB |
Output is correct |
11 |
Correct |
18 ms |
27756 KB |
Output is correct |
12 |
Correct |
18 ms |
27756 KB |
Output is correct |
13 |
Correct |
18 ms |
27756 KB |
Output is correct |
14 |
Correct |
18 ms |
27756 KB |
Output is correct |
15 |
Correct |
18 ms |
27884 KB |
Output is correct |
16 |
Correct |
19 ms |
27756 KB |
Output is correct |
17 |
Correct |
18 ms |
27756 KB |
Output is correct |
18 |
Correct |
18 ms |
27756 KB |
Output is correct |
19 |
Correct |
18 ms |
27756 KB |
Output is correct |
20 |
Correct |
18 ms |
27756 KB |
Output is correct |
21 |
Correct |
19 ms |
27756 KB |
Output is correct |
22 |
Correct |
18 ms |
27756 KB |
Output is correct |
23 |
Correct |
19 ms |
27756 KB |
Output is correct |
24 |
Correct |
19 ms |
27756 KB |
Output is correct |
25 |
Correct |
18 ms |
27756 KB |
Output is correct |
26 |
Correct |
22 ms |
28140 KB |
Output is correct |
27 |
Correct |
21 ms |
28160 KB |
Output is correct |
28 |
Correct |
22 ms |
28396 KB |
Output is correct |
29 |
Correct |
23 ms |
28268 KB |
Output is correct |
30 |
Correct |
22 ms |
28140 KB |
Output is correct |
31 |
Correct |
18 ms |
27756 KB |
Output is correct |
32 |
Correct |
22 ms |
28524 KB |
Output is correct |
33 |
Correct |
19 ms |
27756 KB |
Output is correct |
34 |
Correct |
21 ms |
28140 KB |
Output is correct |
35 |
Correct |
22 ms |
28268 KB |
Output is correct |
36 |
Correct |
22 ms |
28140 KB |
Output is correct |
37 |
Correct |
22 ms |
28140 KB |
Output is correct |
38 |
Correct |
18 ms |
27756 KB |
Output is correct |
39 |
Correct |
22 ms |
28140 KB |
Output is correct |
40 |
Correct |
22 ms |
28140 KB |
Output is correct |
41 |
Correct |
22 ms |
28140 KB |
Output is correct |
42 |
Correct |
21 ms |
28140 KB |
Output is correct |
43 |
Correct |
22 ms |
28524 KB |
Output is correct |
44 |
Correct |
18 ms |
27756 KB |
Output is correct |
45 |
Correct |
24 ms |
28284 KB |
Output is correct |
46 |
Correct |
22 ms |
28140 KB |
Output is correct |
47 |
Correct |
19 ms |
27756 KB |
Output is correct |
48 |
Correct |
22 ms |
28140 KB |
Output is correct |
49 |
Correct |
22 ms |
28268 KB |
Output is correct |
50 |
Correct |
23 ms |
28268 KB |
Output is correct |
51 |
Correct |
22 ms |
28140 KB |
Output is correct |
52 |
Correct |
21 ms |
28140 KB |
Output is correct |
53 |
Correct |
21 ms |
28140 KB |
Output is correct |
54 |
Correct |
21 ms |
28140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27756 KB |
Output is correct |
2 |
Correct |
18 ms |
27756 KB |
Output is correct |
3 |
Correct |
18 ms |
27756 KB |
Output is correct |
4 |
Correct |
20 ms |
27756 KB |
Output is correct |
5 |
Correct |
18 ms |
27756 KB |
Output is correct |
6 |
Correct |
18 ms |
27756 KB |
Output is correct |
7 |
Correct |
22 ms |
27756 KB |
Output is correct |
8 |
Correct |
18 ms |
27884 KB |
Output is correct |
9 |
Correct |
18 ms |
27756 KB |
Output is correct |
10 |
Correct |
18 ms |
27756 KB |
Output is correct |
11 |
Correct |
18 ms |
27756 KB |
Output is correct |
12 |
Correct |
18 ms |
27756 KB |
Output is correct |
13 |
Correct |
18 ms |
27756 KB |
Output is correct |
14 |
Correct |
18 ms |
27756 KB |
Output is correct |
15 |
Correct |
18 ms |
27884 KB |
Output is correct |
16 |
Correct |
19 ms |
27756 KB |
Output is correct |
17 |
Correct |
18 ms |
27756 KB |
Output is correct |
18 |
Correct |
18 ms |
27756 KB |
Output is correct |
19 |
Correct |
18 ms |
27756 KB |
Output is correct |
20 |
Correct |
18 ms |
27756 KB |
Output is correct |
21 |
Correct |
19 ms |
27756 KB |
Output is correct |
22 |
Correct |
18 ms |
27756 KB |
Output is correct |
23 |
Correct |
19 ms |
27756 KB |
Output is correct |
24 |
Correct |
19 ms |
27756 KB |
Output is correct |
25 |
Correct |
18 ms |
27756 KB |
Output is correct |
26 |
Correct |
171 ms |
41828 KB |
Output is correct |
27 |
Correct |
195 ms |
41708 KB |
Output is correct |
28 |
Correct |
21 ms |
28140 KB |
Output is correct |
29 |
Correct |
18 ms |
27756 KB |
Output is correct |
30 |
Correct |
20 ms |
27756 KB |
Output is correct |
31 |
Correct |
186 ms |
41580 KB |
Output is correct |
32 |
Correct |
21 ms |
28140 KB |
Output is correct |
33 |
Correct |
241 ms |
54508 KB |
Output is correct |
34 |
Correct |
193 ms |
41644 KB |
Output is correct |
35 |
Correct |
22 ms |
28140 KB |
Output is correct |
36 |
Correct |
203 ms |
42988 KB |
Output is correct |
37 |
Correct |
22 ms |
28140 KB |
Output is correct |
38 |
Correct |
22 ms |
28140 KB |
Output is correct |
39 |
Correct |
160 ms |
41828 KB |
Output is correct |
40 |
Correct |
22 ms |
28524 KB |
Output is correct |
41 |
Correct |
192 ms |
41576 KB |
Output is correct |
42 |
Correct |
249 ms |
46060 KB |
Output is correct |
43 |
Correct |
18 ms |
27756 KB |
Output is correct |
44 |
Correct |
221 ms |
55020 KB |
Output is correct |
45 |
Correct |
233 ms |
48492 KB |
Output is correct |
46 |
Correct |
21 ms |
28140 KB |
Output is correct |
47 |
Correct |
22 ms |
28140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
40420 KB |
Output is correct |
2 |
Correct |
165 ms |
43492 KB |
Output is correct |
3 |
Correct |
22 ms |
28140 KB |
Output is correct |
4 |
Correct |
21 ms |
28140 KB |
Output is correct |
5 |
Correct |
18 ms |
27756 KB |
Output is correct |
6 |
Correct |
18 ms |
27756 KB |
Output is correct |
7 |
Correct |
21 ms |
28140 KB |
Output is correct |
8 |
Correct |
222 ms |
43380 KB |
Output is correct |
9 |
Correct |
22 ms |
28140 KB |
Output is correct |
10 |
Correct |
193 ms |
41708 KB |
Output is correct |
11 |
Correct |
18 ms |
27756 KB |
Output is correct |
12 |
Correct |
204 ms |
41872 KB |
Output is correct |
13 |
Correct |
212 ms |
43116 KB |
Output is correct |
14 |
Correct |
201 ms |
44908 KB |
Output is correct |
15 |
Correct |
163 ms |
41956 KB |
Output is correct |
16 |
Correct |
22 ms |
28140 KB |
Output is correct |
17 |
Correct |
18 ms |
27756 KB |
Output is correct |
18 |
Correct |
169 ms |
44772 KB |
Output is correct |
19 |
Correct |
227 ms |
51564 KB |
Output is correct |
20 |
Correct |
22 ms |
28140 KB |
Output is correct |
21 |
Correct |
19 ms |
27756 KB |
Output is correct |
22 |
Correct |
171 ms |
43368 KB |
Output is correct |
23 |
Correct |
22 ms |
28140 KB |
Output is correct |
24 |
Correct |
205 ms |
42092 KB |
Output is correct |
25 |
Correct |
212 ms |
49132 KB |
Output is correct |
26 |
Correct |
21 ms |
28268 KB |
Output is correct |
27 |
Correct |
22 ms |
28268 KB |
Output is correct |
28 |
Correct |
22 ms |
28140 KB |
Output is correct |
29 |
Correct |
22 ms |
28140 KB |
Output is correct |
30 |
Correct |
22 ms |
28140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27756 KB |
Output is correct |
2 |
Correct |
18 ms |
27756 KB |
Output is correct |
3 |
Correct |
18 ms |
27756 KB |
Output is correct |
4 |
Correct |
20 ms |
27756 KB |
Output is correct |
5 |
Correct |
18 ms |
27756 KB |
Output is correct |
6 |
Correct |
18 ms |
27756 KB |
Output is correct |
7 |
Correct |
22 ms |
27756 KB |
Output is correct |
8 |
Correct |
18 ms |
27884 KB |
Output is correct |
9 |
Correct |
18 ms |
27756 KB |
Output is correct |
10 |
Correct |
18 ms |
27756 KB |
Output is correct |
11 |
Correct |
18 ms |
27756 KB |
Output is correct |
12 |
Correct |
18 ms |
27756 KB |
Output is correct |
13 |
Correct |
18 ms |
27756 KB |
Output is correct |
14 |
Correct |
18 ms |
27756 KB |
Output is correct |
15 |
Correct |
18 ms |
27884 KB |
Output is correct |
16 |
Correct |
19 ms |
27756 KB |
Output is correct |
17 |
Correct |
18 ms |
27756 KB |
Output is correct |
18 |
Correct |
18 ms |
27756 KB |
Output is correct |
19 |
Correct |
18 ms |
27756 KB |
Output is correct |
20 |
Correct |
18 ms |
27756 KB |
Output is correct |
21 |
Correct |
19 ms |
27756 KB |
Output is correct |
22 |
Correct |
18 ms |
27756 KB |
Output is correct |
23 |
Correct |
19 ms |
27756 KB |
Output is correct |
24 |
Correct |
19 ms |
27756 KB |
Output is correct |
25 |
Correct |
18 ms |
27756 KB |
Output is correct |
26 |
Correct |
22 ms |
28140 KB |
Output is correct |
27 |
Correct |
21 ms |
28160 KB |
Output is correct |
28 |
Correct |
22 ms |
28396 KB |
Output is correct |
29 |
Correct |
23 ms |
28268 KB |
Output is correct |
30 |
Correct |
22 ms |
28140 KB |
Output is correct |
31 |
Correct |
18 ms |
27756 KB |
Output is correct |
32 |
Correct |
22 ms |
28524 KB |
Output is correct |
33 |
Correct |
19 ms |
27756 KB |
Output is correct |
34 |
Correct |
21 ms |
28140 KB |
Output is correct |
35 |
Correct |
22 ms |
28268 KB |
Output is correct |
36 |
Correct |
22 ms |
28140 KB |
Output is correct |
37 |
Correct |
22 ms |
28140 KB |
Output is correct |
38 |
Correct |
18 ms |
27756 KB |
Output is correct |
39 |
Correct |
22 ms |
28140 KB |
Output is correct |
40 |
Correct |
22 ms |
28140 KB |
Output is correct |
41 |
Correct |
22 ms |
28140 KB |
Output is correct |
42 |
Correct |
21 ms |
28140 KB |
Output is correct |
43 |
Correct |
22 ms |
28524 KB |
Output is correct |
44 |
Correct |
18 ms |
27756 KB |
Output is correct |
45 |
Correct |
24 ms |
28284 KB |
Output is correct |
46 |
Correct |
22 ms |
28140 KB |
Output is correct |
47 |
Correct |
19 ms |
27756 KB |
Output is correct |
48 |
Correct |
22 ms |
28140 KB |
Output is correct |
49 |
Correct |
22 ms |
28268 KB |
Output is correct |
50 |
Correct |
23 ms |
28268 KB |
Output is correct |
51 |
Correct |
22 ms |
28140 KB |
Output is correct |
52 |
Correct |
21 ms |
28140 KB |
Output is correct |
53 |
Correct |
21 ms |
28140 KB |
Output is correct |
54 |
Correct |
21 ms |
28140 KB |
Output is correct |
55 |
Correct |
18 ms |
27756 KB |
Output is correct |
56 |
Correct |
171 ms |
41828 KB |
Output is correct |
57 |
Correct |
195 ms |
41708 KB |
Output is correct |
58 |
Correct |
21 ms |
28140 KB |
Output is correct |
59 |
Correct |
18 ms |
27756 KB |
Output is correct |
60 |
Correct |
20 ms |
27756 KB |
Output is correct |
61 |
Correct |
186 ms |
41580 KB |
Output is correct |
62 |
Correct |
21 ms |
28140 KB |
Output is correct |
63 |
Correct |
241 ms |
54508 KB |
Output is correct |
64 |
Correct |
193 ms |
41644 KB |
Output is correct |
65 |
Correct |
22 ms |
28140 KB |
Output is correct |
66 |
Correct |
203 ms |
42988 KB |
Output is correct |
67 |
Correct |
22 ms |
28140 KB |
Output is correct |
68 |
Correct |
22 ms |
28140 KB |
Output is correct |
69 |
Correct |
160 ms |
41828 KB |
Output is correct |
70 |
Correct |
22 ms |
28524 KB |
Output is correct |
71 |
Correct |
192 ms |
41576 KB |
Output is correct |
72 |
Correct |
249 ms |
46060 KB |
Output is correct |
73 |
Correct |
18 ms |
27756 KB |
Output is correct |
74 |
Correct |
221 ms |
55020 KB |
Output is correct |
75 |
Correct |
233 ms |
48492 KB |
Output is correct |
76 |
Correct |
21 ms |
28140 KB |
Output is correct |
77 |
Correct |
22 ms |
28140 KB |
Output is correct |
78 |
Correct |
155 ms |
40420 KB |
Output is correct |
79 |
Correct |
165 ms |
43492 KB |
Output is correct |
80 |
Correct |
22 ms |
28140 KB |
Output is correct |
81 |
Correct |
21 ms |
28140 KB |
Output is correct |
82 |
Correct |
18 ms |
27756 KB |
Output is correct |
83 |
Correct |
18 ms |
27756 KB |
Output is correct |
84 |
Correct |
21 ms |
28140 KB |
Output is correct |
85 |
Correct |
222 ms |
43380 KB |
Output is correct |
86 |
Correct |
22 ms |
28140 KB |
Output is correct |
87 |
Correct |
193 ms |
41708 KB |
Output is correct |
88 |
Correct |
18 ms |
27756 KB |
Output is correct |
89 |
Correct |
204 ms |
41872 KB |
Output is correct |
90 |
Correct |
212 ms |
43116 KB |
Output is correct |
91 |
Correct |
201 ms |
44908 KB |
Output is correct |
92 |
Correct |
163 ms |
41956 KB |
Output is correct |
93 |
Correct |
22 ms |
28140 KB |
Output is correct |
94 |
Correct |
18 ms |
27756 KB |
Output is correct |
95 |
Correct |
169 ms |
44772 KB |
Output is correct |
96 |
Correct |
227 ms |
51564 KB |
Output is correct |
97 |
Correct |
22 ms |
28140 KB |
Output is correct |
98 |
Correct |
19 ms |
27756 KB |
Output is correct |
99 |
Correct |
171 ms |
43368 KB |
Output is correct |
100 |
Correct |
22 ms |
28140 KB |
Output is correct |
101 |
Correct |
205 ms |
42092 KB |
Output is correct |
102 |
Correct |
212 ms |
49132 KB |
Output is correct |
103 |
Correct |
21 ms |
28268 KB |
Output is correct |
104 |
Correct |
22 ms |
28268 KB |
Output is correct |
105 |
Correct |
22 ms |
28140 KB |
Output is correct |
106 |
Correct |
22 ms |
28140 KB |
Output is correct |
107 |
Correct |
22 ms |
28140 KB |
Output is correct |
108 |
Correct |
1102 ms |
105680 KB |
Output is correct |
109 |
Correct |
1405 ms |
129728 KB |
Output is correct |
110 |
Correct |
1426 ms |
150108 KB |
Output is correct |
111 |
Correct |
1475 ms |
154732 KB |
Output is correct |
112 |
Correct |
1501 ms |
137008 KB |
Output is correct |
113 |
Correct |
1018 ms |
114260 KB |
Output is correct |
114 |
Correct |
1147 ms |
96728 KB |
Output is correct |
115 |
Correct |
1151 ms |
96868 KB |
Output is correct |
116 |
Correct |
1483 ms |
101808 KB |
Output is correct |
117 |
Correct |
1292 ms |
114592 KB |
Output is correct |
118 |
Correct |
1220 ms |
97448 KB |
Output is correct |
119 |
Correct |
1296 ms |
114576 KB |
Output is correct |
120 |
Correct |
1448 ms |
138336 KB |
Output is correct |
121 |
Correct |
1293 ms |
114656 KB |
Output is correct |
122 |
Correct |
1509 ms |
107560 KB |
Output is correct |
123 |
Correct |
978 ms |
116188 KB |
Output is correct |
124 |
Correct |
1363 ms |
112368 KB |
Output is correct |