#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#define int ll
using namespace std;
const int N = 5e5 + 5,inf = 1e18;
vector <pii> v[N],vec[N];
int col[N],tin[N],timer,d[25][N],dep[N];
int edge[N],val[N],n,D,I,p[N];
bool vis[N],del[N];
set <int> st[N];
void dfs(int x,int par){
d[0][x] = par;
dep[x] = dep[par] + 1;
tin[x] = ++timer;
for (auto [to, id]: v[x]){
if (to != par) {
edge[to] = id;
dfs(to,x);
}
}
}
int lca(int x,int y){
if (dep[x] < dep[y ]) swap(x,y);
for (int i = 20; i >= 0; i--)
if (dep[d[i][x]] >= dep[y]) x = d[i][x];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (d[i][x] != d[i][y]) x = d[i][x],y = d[i][y];
return d[0][x];
}
void getpath(int x,int y){
int c = lca(x,y);
while (x != c){
val[edge[x]] = 0;
x = d[0][x];
}
while (y != c){
val[edge[y]] = 0;
y = d[0][y];
}
}
void DFS(int x,int par,int dd){
if (dd >= D) D = dd,I = x;
for (auto [to,id]: v[x]){
if (to != par) DFS(to,x,dd + val[id]);
}
}
int P(int x){
if(x==p[x])return x;
return p[x]=P(p[x]);
}
void dsu(int x,int y){
int px = P(x),py = P(y);
if (st[px].size() < st[py].size()) swap(px,py);
p[py] = px;
del[py] = 1;
set <int> nw = st[px];
for (int v: st[py])
nw.insert(v);
st[px] = nw;
st[py].clear();
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int k;
cin>>n>>k;
for (int i = 1; i < n; i++){
int a,b;
cin>>a>>b;
st[a].insert(b);
st[b].insert(a);
v[a].pb({b,i});
v[b].pb({a,i});
val[i] = 1;
}
dfs(1,1);
for (int j = 1; j <= 20 ; j++)
for (int i = 1; i <= n; i++)
d[j][i] = d[j - 1][d[j - 1][i]];
for (int i = 1; i <= n; i++)
cin >> col[i],vec[col[i]].pb({tin[i],i});
for (int i = 1; i <= k; i++){
sort(vec[i].begin(),vec[i].end());
for (int j = 1; j < vec[i].size(); j++){
getpath(vec[i][j].s,vec[i][j - 1].s);
}
}
for (int i=1;i<=n;i++)
p[i] = i;
for (int i = 2; i <= n; i++)
if (!val[edge[i]]) dsu(i,d[0][i]);
for (int i = 1; i <= n;i++){
if (del[i]) continue;
set <int> nw;
for (int v: st[i])
nw.insert(P(v));
if (nw.find(i) != nw.end()) nw.erase(nw.find(i));
st[i] = nw;
}
int ans=0;
for (int i = 1; i <= n; i++){
if (del[i]) continue;
ans += ((int)st[i].size() == 1);
}
cout<<(ans + 1)/2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:110:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int j = 1; j < vec[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
49500 KB |
Output is correct |
2 |
Correct |
14 ms |
49496 KB |
Output is correct |
3 |
Correct |
14 ms |
49448 KB |
Output is correct |
4 |
Correct |
15 ms |
49448 KB |
Output is correct |
5 |
Correct |
15 ms |
49496 KB |
Output is correct |
6 |
Correct |
15 ms |
49500 KB |
Output is correct |
7 |
Correct |
15 ms |
49500 KB |
Output is correct |
8 |
Correct |
15 ms |
49488 KB |
Output is correct |
9 |
Correct |
14 ms |
49504 KB |
Output is correct |
10 |
Correct |
14 ms |
49412 KB |
Output is correct |
11 |
Correct |
18 ms |
49500 KB |
Output is correct |
12 |
Correct |
17 ms |
49500 KB |
Output is correct |
13 |
Correct |
22 ms |
49412 KB |
Output is correct |
14 |
Correct |
15 ms |
49496 KB |
Output is correct |
15 |
Correct |
14 ms |
49500 KB |
Output is correct |
16 |
Correct |
15 ms |
49500 KB |
Output is correct |
17 |
Correct |
23 ms |
49500 KB |
Output is correct |
18 |
Correct |
17 ms |
49500 KB |
Output is correct |
19 |
Correct |
17 ms |
49500 KB |
Output is correct |
20 |
Correct |
15 ms |
49460 KB |
Output is correct |
21 |
Correct |
17 ms |
49500 KB |
Output is correct |
22 |
Correct |
16 ms |
49500 KB |
Output is correct |
23 |
Correct |
24 ms |
49612 KB |
Output is correct |
24 |
Correct |
18 ms |
49500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
49500 KB |
Output is correct |
2 |
Correct |
14 ms |
49496 KB |
Output is correct |
3 |
Correct |
14 ms |
49448 KB |
Output is correct |
4 |
Correct |
15 ms |
49448 KB |
Output is correct |
5 |
Correct |
15 ms |
49496 KB |
Output is correct |
6 |
Correct |
15 ms |
49500 KB |
Output is correct |
7 |
Correct |
15 ms |
49500 KB |
Output is correct |
8 |
Correct |
15 ms |
49488 KB |
Output is correct |
9 |
Correct |
14 ms |
49504 KB |
Output is correct |
10 |
Correct |
14 ms |
49412 KB |
Output is correct |
11 |
Correct |
18 ms |
49500 KB |
Output is correct |
12 |
Correct |
17 ms |
49500 KB |
Output is correct |
13 |
Correct |
22 ms |
49412 KB |
Output is correct |
14 |
Correct |
15 ms |
49496 KB |
Output is correct |
15 |
Correct |
14 ms |
49500 KB |
Output is correct |
16 |
Correct |
15 ms |
49500 KB |
Output is correct |
17 |
Correct |
23 ms |
49500 KB |
Output is correct |
18 |
Correct |
17 ms |
49500 KB |
Output is correct |
19 |
Correct |
17 ms |
49500 KB |
Output is correct |
20 |
Correct |
15 ms |
49460 KB |
Output is correct |
21 |
Correct |
17 ms |
49500 KB |
Output is correct |
22 |
Correct |
16 ms |
49500 KB |
Output is correct |
23 |
Correct |
24 ms |
49612 KB |
Output is correct |
24 |
Correct |
18 ms |
49500 KB |
Output is correct |
25 |
Correct |
20 ms |
49500 KB |
Output is correct |
26 |
Correct |
19 ms |
50592 KB |
Output is correct |
27 |
Correct |
19 ms |
50776 KB |
Output is correct |
28 |
Correct |
18 ms |
50780 KB |
Output is correct |
29 |
Correct |
16 ms |
50600 KB |
Output is correct |
30 |
Correct |
35 ms |
50780 KB |
Output is correct |
31 |
Correct |
16 ms |
49496 KB |
Output is correct |
32 |
Correct |
19 ms |
50780 KB |
Output is correct |
33 |
Correct |
20 ms |
49412 KB |
Output is correct |
34 |
Correct |
324 ms |
50928 KB |
Output is correct |
35 |
Correct |
17 ms |
50780 KB |
Output is correct |
36 |
Correct |
19 ms |
50812 KB |
Output is correct |
37 |
Correct |
23 ms |
50764 KB |
Output is correct |
38 |
Correct |
12 ms |
49496 KB |
Output is correct |
39 |
Correct |
63 ms |
50780 KB |
Output is correct |
40 |
Correct |
50 ms |
50864 KB |
Output is correct |
41 |
Correct |
30 ms |
50776 KB |
Output is correct |
42 |
Correct |
180 ms |
50960 KB |
Output is correct |
43 |
Correct |
21 ms |
50780 KB |
Output is correct |
44 |
Correct |
17 ms |
47448 KB |
Output is correct |
45 |
Correct |
21 ms |
48728 KB |
Output is correct |
46 |
Correct |
25 ms |
48984 KB |
Output is correct |
47 |
Correct |
15 ms |
47452 KB |
Output is correct |
48 |
Correct |
27 ms |
48756 KB |
Output is correct |
49 |
Correct |
19 ms |
48984 KB |
Output is correct |
50 |
Correct |
19 ms |
48728 KB |
Output is correct |
51 |
Correct |
19 ms |
48832 KB |
Output is correct |
52 |
Correct |
223 ms |
48896 KB |
Output is correct |
53 |
Correct |
283 ms |
50932 KB |
Output is correct |
54 |
Correct |
68 ms |
50780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
49500 KB |
Output is correct |
2 |
Correct |
14 ms |
49496 KB |
Output is correct |
3 |
Correct |
14 ms |
49448 KB |
Output is correct |
4 |
Correct |
15 ms |
49448 KB |
Output is correct |
5 |
Correct |
15 ms |
49496 KB |
Output is correct |
6 |
Correct |
15 ms |
49500 KB |
Output is correct |
7 |
Correct |
15 ms |
49500 KB |
Output is correct |
8 |
Correct |
15 ms |
49488 KB |
Output is correct |
9 |
Correct |
14 ms |
49504 KB |
Output is correct |
10 |
Correct |
14 ms |
49412 KB |
Output is correct |
11 |
Correct |
18 ms |
49500 KB |
Output is correct |
12 |
Correct |
17 ms |
49500 KB |
Output is correct |
13 |
Correct |
22 ms |
49412 KB |
Output is correct |
14 |
Correct |
15 ms |
49496 KB |
Output is correct |
15 |
Correct |
14 ms |
49500 KB |
Output is correct |
16 |
Correct |
15 ms |
49500 KB |
Output is correct |
17 |
Correct |
23 ms |
49500 KB |
Output is correct |
18 |
Correct |
17 ms |
49500 KB |
Output is correct |
19 |
Correct |
17 ms |
49500 KB |
Output is correct |
20 |
Correct |
15 ms |
49460 KB |
Output is correct |
21 |
Correct |
17 ms |
49500 KB |
Output is correct |
22 |
Correct |
16 ms |
49500 KB |
Output is correct |
23 |
Correct |
24 ms |
49612 KB |
Output is correct |
24 |
Correct |
18 ms |
49500 KB |
Output is correct |
25 |
Correct |
12 ms |
49496 KB |
Output is correct |
26 |
Execution timed out |
3027 ms |
92604 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3020 ms |
91068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
49500 KB |
Output is correct |
2 |
Correct |
14 ms |
49496 KB |
Output is correct |
3 |
Correct |
14 ms |
49448 KB |
Output is correct |
4 |
Correct |
15 ms |
49448 KB |
Output is correct |
5 |
Correct |
15 ms |
49496 KB |
Output is correct |
6 |
Correct |
15 ms |
49500 KB |
Output is correct |
7 |
Correct |
15 ms |
49500 KB |
Output is correct |
8 |
Correct |
15 ms |
49488 KB |
Output is correct |
9 |
Correct |
14 ms |
49504 KB |
Output is correct |
10 |
Correct |
14 ms |
49412 KB |
Output is correct |
11 |
Correct |
18 ms |
49500 KB |
Output is correct |
12 |
Correct |
17 ms |
49500 KB |
Output is correct |
13 |
Correct |
22 ms |
49412 KB |
Output is correct |
14 |
Correct |
15 ms |
49496 KB |
Output is correct |
15 |
Correct |
14 ms |
49500 KB |
Output is correct |
16 |
Correct |
15 ms |
49500 KB |
Output is correct |
17 |
Correct |
23 ms |
49500 KB |
Output is correct |
18 |
Correct |
17 ms |
49500 KB |
Output is correct |
19 |
Correct |
17 ms |
49500 KB |
Output is correct |
20 |
Correct |
15 ms |
49460 KB |
Output is correct |
21 |
Correct |
17 ms |
49500 KB |
Output is correct |
22 |
Correct |
16 ms |
49500 KB |
Output is correct |
23 |
Correct |
24 ms |
49612 KB |
Output is correct |
24 |
Correct |
18 ms |
49500 KB |
Output is correct |
25 |
Correct |
20 ms |
49500 KB |
Output is correct |
26 |
Correct |
19 ms |
50592 KB |
Output is correct |
27 |
Correct |
19 ms |
50776 KB |
Output is correct |
28 |
Correct |
18 ms |
50780 KB |
Output is correct |
29 |
Correct |
16 ms |
50600 KB |
Output is correct |
30 |
Correct |
35 ms |
50780 KB |
Output is correct |
31 |
Correct |
16 ms |
49496 KB |
Output is correct |
32 |
Correct |
19 ms |
50780 KB |
Output is correct |
33 |
Correct |
20 ms |
49412 KB |
Output is correct |
34 |
Correct |
324 ms |
50928 KB |
Output is correct |
35 |
Correct |
17 ms |
50780 KB |
Output is correct |
36 |
Correct |
19 ms |
50812 KB |
Output is correct |
37 |
Correct |
23 ms |
50764 KB |
Output is correct |
38 |
Correct |
12 ms |
49496 KB |
Output is correct |
39 |
Correct |
63 ms |
50780 KB |
Output is correct |
40 |
Correct |
50 ms |
50864 KB |
Output is correct |
41 |
Correct |
30 ms |
50776 KB |
Output is correct |
42 |
Correct |
180 ms |
50960 KB |
Output is correct |
43 |
Correct |
21 ms |
50780 KB |
Output is correct |
44 |
Correct |
17 ms |
47448 KB |
Output is correct |
45 |
Correct |
21 ms |
48728 KB |
Output is correct |
46 |
Correct |
25 ms |
48984 KB |
Output is correct |
47 |
Correct |
15 ms |
47452 KB |
Output is correct |
48 |
Correct |
27 ms |
48756 KB |
Output is correct |
49 |
Correct |
19 ms |
48984 KB |
Output is correct |
50 |
Correct |
19 ms |
48728 KB |
Output is correct |
51 |
Correct |
19 ms |
48832 KB |
Output is correct |
52 |
Correct |
223 ms |
48896 KB |
Output is correct |
53 |
Correct |
283 ms |
50932 KB |
Output is correct |
54 |
Correct |
68 ms |
50780 KB |
Output is correct |
55 |
Correct |
12 ms |
49496 KB |
Output is correct |
56 |
Execution timed out |
3027 ms |
92604 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |