#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <vector>
using namespace std;
set < int > a,b;
//set < pair < int , int > > road;
int now=0;
vector < int > Next[500005];
int color[500005];
int deg[500005]={0};
int tt[500005]={0};
int Father[500005]={0};
vector < int > con[500005];
vector < int > use[500005];
int sum[500005]={0};
int M;
int root[500005];
int Find(int here)
{
if(root[here]==here) return root[here];
root[here]=Find(root[here]);
return root[here];
}
void F2(int here,int fa,int xx)
{
for(auto i:Next[here])
{
if(i==fa) continue;
if(Find(here)!=Find(i))
{
now++;
deg[xx]++;
deg[now]++;
//printf("%d %d %d %d\n",here,i,deg[xx],deg[now]);
F2(i,here,now);
}
else F2(i,here,xx);
}
}
void F4(int here,int fa,int deg)
{
tt[here]=deg;
Father[here]=fa;
for(auto i:Next[here])
{
if(i!=fa)
{
//road.insert(make_pair(here,i));
F4(i,here,deg+1);
}
}
}
int LCA(int a,int b)
{
a=Find(a);
b=Find(b);
if(a==b) return a;
int x,y,t;
if(tt[a]==tt[b])
{
x=Father[a];
y=Father[b];
t=LCA(x,y);
root[Find(a)]=t;
root[Find(b)]=t;
return t;
}
else
{
if(tt[a]<tt[b]) swap(a,b);
x=Father[a];
//if(road.find(make_pair(x,a))!=road.end()) road.erase(make_pair(x,a));
t=LCA(x,b);
root[Find(a)]=t;
return t;
}
}
int main()
{
//freopen("01-04.txt","rt",stdin);
int N,K,i,j,x,y,xx=0,ok=1;
scanf("%d %d",&N,&M);
for(i=1;i<N;i++)
{
scanf("%d %d",&x,&y);
Next[x].push_back(y);
Next[y].push_back(x);
}
for(i=1;i<=N;i++)
{
scanf("%d",&color[i]);
root[i]=i;
use[color[i]].push_back(i);
}
F4(1,-1,0);
for(i=1;i<=M;i++)
{
K=use[i].size();
if(K>=2)
{
x=use[i][0];
for(j=1;j<K;j++) x=LCA(x,use[i][j]);
}
}
now=0;
F2(1,-1,0);
for(i=0;i<=now;i++) if(deg[i]==1) xx++;
if(xx==1) printf("0\n");
else printf("%d\n",(xx+1)/2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:84:26: warning: unused variable 'ok' [-Wunused-variable]
84 | int N,K,i,j,x,y,xx=0,ok=1;
| ^~
mergers.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
85 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | scanf("%d %d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
94 | scanf("%d",&color[i]);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
35564 KB |
Output is correct |
2 |
Correct |
23 ms |
35692 KB |
Output is correct |
3 |
Correct |
23 ms |
35564 KB |
Output is correct |
4 |
Correct |
23 ms |
35564 KB |
Output is correct |
5 |
Correct |
23 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35564 KB |
Output is correct |
7 |
Correct |
22 ms |
35564 KB |
Output is correct |
8 |
Correct |
22 ms |
35564 KB |
Output is correct |
9 |
Correct |
26 ms |
35564 KB |
Output is correct |
10 |
Correct |
23 ms |
35564 KB |
Output is correct |
11 |
Correct |
23 ms |
35564 KB |
Output is correct |
12 |
Correct |
23 ms |
35564 KB |
Output is correct |
13 |
Correct |
23 ms |
35564 KB |
Output is correct |
14 |
Correct |
23 ms |
35564 KB |
Output is correct |
15 |
Correct |
23 ms |
35564 KB |
Output is correct |
16 |
Correct |
23 ms |
35564 KB |
Output is correct |
17 |
Correct |
23 ms |
35564 KB |
Output is correct |
18 |
Correct |
26 ms |
35564 KB |
Output is correct |
19 |
Correct |
23 ms |
35564 KB |
Output is correct |
20 |
Correct |
23 ms |
35564 KB |
Output is correct |
21 |
Correct |
22 ms |
35564 KB |
Output is correct |
22 |
Correct |
23 ms |
35564 KB |
Output is correct |
23 |
Correct |
22 ms |
35564 KB |
Output is correct |
24 |
Correct |
22 ms |
35564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
35564 KB |
Output is correct |
2 |
Correct |
23 ms |
35692 KB |
Output is correct |
3 |
Correct |
23 ms |
35564 KB |
Output is correct |
4 |
Correct |
23 ms |
35564 KB |
Output is correct |
5 |
Correct |
23 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35564 KB |
Output is correct |
7 |
Correct |
22 ms |
35564 KB |
Output is correct |
8 |
Correct |
22 ms |
35564 KB |
Output is correct |
9 |
Correct |
26 ms |
35564 KB |
Output is correct |
10 |
Correct |
23 ms |
35564 KB |
Output is correct |
11 |
Correct |
23 ms |
35564 KB |
Output is correct |
12 |
Correct |
23 ms |
35564 KB |
Output is correct |
13 |
Correct |
23 ms |
35564 KB |
Output is correct |
14 |
Correct |
23 ms |
35564 KB |
Output is correct |
15 |
Correct |
23 ms |
35564 KB |
Output is correct |
16 |
Correct |
23 ms |
35564 KB |
Output is correct |
17 |
Correct |
23 ms |
35564 KB |
Output is correct |
18 |
Correct |
26 ms |
35564 KB |
Output is correct |
19 |
Correct |
23 ms |
35564 KB |
Output is correct |
20 |
Correct |
23 ms |
35564 KB |
Output is correct |
21 |
Correct |
22 ms |
35564 KB |
Output is correct |
22 |
Correct |
23 ms |
35564 KB |
Output is correct |
23 |
Correct |
22 ms |
35564 KB |
Output is correct |
24 |
Correct |
22 ms |
35564 KB |
Output is correct |
25 |
Correct |
23 ms |
35564 KB |
Output is correct |
26 |
Correct |
26 ms |
35820 KB |
Output is correct |
27 |
Correct |
25 ms |
35820 KB |
Output is correct |
28 |
Correct |
26 ms |
35948 KB |
Output is correct |
29 |
Correct |
26 ms |
35820 KB |
Output is correct |
30 |
Correct |
29 ms |
35820 KB |
Output is correct |
31 |
Correct |
23 ms |
35692 KB |
Output is correct |
32 |
Correct |
27 ms |
36076 KB |
Output is correct |
33 |
Correct |
23 ms |
35564 KB |
Output is correct |
34 |
Correct |
26 ms |
35820 KB |
Output is correct |
35 |
Correct |
26 ms |
35948 KB |
Output is correct |
36 |
Correct |
26 ms |
35820 KB |
Output is correct |
37 |
Correct |
25 ms |
35820 KB |
Output is correct |
38 |
Correct |
23 ms |
35564 KB |
Output is correct |
39 |
Correct |
25 ms |
35820 KB |
Output is correct |
40 |
Correct |
25 ms |
35820 KB |
Output is correct |
41 |
Correct |
25 ms |
35820 KB |
Output is correct |
42 |
Correct |
25 ms |
35820 KB |
Output is correct |
43 |
Correct |
26 ms |
36076 KB |
Output is correct |
44 |
Correct |
23 ms |
35564 KB |
Output is correct |
45 |
Correct |
27 ms |
35948 KB |
Output is correct |
46 |
Correct |
25 ms |
35820 KB |
Output is correct |
47 |
Correct |
23 ms |
35564 KB |
Output is correct |
48 |
Correct |
26 ms |
35820 KB |
Output is correct |
49 |
Correct |
26 ms |
35948 KB |
Output is correct |
50 |
Correct |
25 ms |
35980 KB |
Output is correct |
51 |
Correct |
25 ms |
35820 KB |
Output is correct |
52 |
Correct |
25 ms |
35820 KB |
Output is correct |
53 |
Correct |
25 ms |
35820 KB |
Output is correct |
54 |
Correct |
25 ms |
35820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
35564 KB |
Output is correct |
2 |
Correct |
23 ms |
35692 KB |
Output is correct |
3 |
Correct |
23 ms |
35564 KB |
Output is correct |
4 |
Correct |
23 ms |
35564 KB |
Output is correct |
5 |
Correct |
23 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35564 KB |
Output is correct |
7 |
Correct |
22 ms |
35564 KB |
Output is correct |
8 |
Correct |
22 ms |
35564 KB |
Output is correct |
9 |
Correct |
26 ms |
35564 KB |
Output is correct |
10 |
Correct |
23 ms |
35564 KB |
Output is correct |
11 |
Correct |
23 ms |
35564 KB |
Output is correct |
12 |
Correct |
23 ms |
35564 KB |
Output is correct |
13 |
Correct |
23 ms |
35564 KB |
Output is correct |
14 |
Correct |
23 ms |
35564 KB |
Output is correct |
15 |
Correct |
23 ms |
35564 KB |
Output is correct |
16 |
Correct |
23 ms |
35564 KB |
Output is correct |
17 |
Correct |
23 ms |
35564 KB |
Output is correct |
18 |
Correct |
26 ms |
35564 KB |
Output is correct |
19 |
Correct |
23 ms |
35564 KB |
Output is correct |
20 |
Correct |
23 ms |
35564 KB |
Output is correct |
21 |
Correct |
22 ms |
35564 KB |
Output is correct |
22 |
Correct |
23 ms |
35564 KB |
Output is correct |
23 |
Correct |
22 ms |
35564 KB |
Output is correct |
24 |
Correct |
22 ms |
35564 KB |
Output is correct |
25 |
Correct |
23 ms |
35564 KB |
Output is correct |
26 |
Correct |
81 ms |
42724 KB |
Output is correct |
27 |
Correct |
124 ms |
42348 KB |
Output is correct |
28 |
Correct |
25 ms |
35820 KB |
Output is correct |
29 |
Correct |
24 ms |
35564 KB |
Output is correct |
30 |
Correct |
23 ms |
35564 KB |
Output is correct |
31 |
Correct |
118 ms |
42476 KB |
Output is correct |
32 |
Correct |
26 ms |
35820 KB |
Output is correct |
33 |
Correct |
145 ms |
49516 KB |
Output is correct |
34 |
Correct |
130 ms |
42476 KB |
Output is correct |
35 |
Correct |
26 ms |
35820 KB |
Output is correct |
36 |
Correct |
119 ms |
43116 KB |
Output is correct |
37 |
Correct |
25 ms |
35820 KB |
Output is correct |
38 |
Correct |
24 ms |
35820 KB |
Output is correct |
39 |
Correct |
87 ms |
42596 KB |
Output is correct |
40 |
Correct |
29 ms |
36076 KB |
Output is correct |
41 |
Correct |
140 ms |
42344 KB |
Output is correct |
42 |
Correct |
116 ms |
44652 KB |
Output is correct |
43 |
Correct |
23 ms |
35564 KB |
Output is correct |
44 |
Correct |
136 ms |
49772 KB |
Output is correct |
45 |
Correct |
133 ms |
46092 KB |
Output is correct |
46 |
Correct |
26 ms |
35860 KB |
Output is correct |
47 |
Correct |
25 ms |
35820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
42724 KB |
Output is correct |
2 |
Correct |
95 ms |
46004 KB |
Output is correct |
3 |
Correct |
25 ms |
35820 KB |
Output is correct |
4 |
Correct |
24 ms |
35820 KB |
Output is correct |
5 |
Correct |
24 ms |
35564 KB |
Output is correct |
6 |
Correct |
23 ms |
35564 KB |
Output is correct |
7 |
Correct |
25 ms |
35820 KB |
Output is correct |
8 |
Correct |
159 ms |
43756 KB |
Output is correct |
9 |
Correct |
25 ms |
35820 KB |
Output is correct |
10 |
Correct |
110 ms |
42476 KB |
Output is correct |
11 |
Correct |
23 ms |
35564 KB |
Output is correct |
12 |
Correct |
117 ms |
42392 KB |
Output is correct |
13 |
Correct |
130 ms |
43756 KB |
Output is correct |
14 |
Correct |
122 ms |
45676 KB |
Output is correct |
15 |
Correct |
97 ms |
42596 KB |
Output is correct |
16 |
Correct |
26 ms |
35948 KB |
Output is correct |
17 |
Correct |
25 ms |
35692 KB |
Output is correct |
18 |
Correct |
94 ms |
45540 KB |
Output is correct |
19 |
Correct |
146 ms |
49516 KB |
Output is correct |
20 |
Correct |
25 ms |
35840 KB |
Output is correct |
21 |
Correct |
29 ms |
35564 KB |
Output is correct |
22 |
Correct |
102 ms |
44008 KB |
Output is correct |
23 |
Correct |
24 ms |
35820 KB |
Output is correct |
24 |
Correct |
144 ms |
42988 KB |
Output is correct |
25 |
Correct |
147 ms |
48364 KB |
Output is correct |
26 |
Correct |
24 ms |
36096 KB |
Output is correct |
27 |
Correct |
24 ms |
35948 KB |
Output is correct |
28 |
Correct |
24 ms |
35820 KB |
Output is correct |
29 |
Correct |
25 ms |
35820 KB |
Output is correct |
30 |
Correct |
25 ms |
35820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
35564 KB |
Output is correct |
2 |
Correct |
23 ms |
35692 KB |
Output is correct |
3 |
Correct |
23 ms |
35564 KB |
Output is correct |
4 |
Correct |
23 ms |
35564 KB |
Output is correct |
5 |
Correct |
23 ms |
35712 KB |
Output is correct |
6 |
Correct |
22 ms |
35564 KB |
Output is correct |
7 |
Correct |
22 ms |
35564 KB |
Output is correct |
8 |
Correct |
22 ms |
35564 KB |
Output is correct |
9 |
Correct |
26 ms |
35564 KB |
Output is correct |
10 |
Correct |
23 ms |
35564 KB |
Output is correct |
11 |
Correct |
23 ms |
35564 KB |
Output is correct |
12 |
Correct |
23 ms |
35564 KB |
Output is correct |
13 |
Correct |
23 ms |
35564 KB |
Output is correct |
14 |
Correct |
23 ms |
35564 KB |
Output is correct |
15 |
Correct |
23 ms |
35564 KB |
Output is correct |
16 |
Correct |
23 ms |
35564 KB |
Output is correct |
17 |
Correct |
23 ms |
35564 KB |
Output is correct |
18 |
Correct |
26 ms |
35564 KB |
Output is correct |
19 |
Correct |
23 ms |
35564 KB |
Output is correct |
20 |
Correct |
23 ms |
35564 KB |
Output is correct |
21 |
Correct |
22 ms |
35564 KB |
Output is correct |
22 |
Correct |
23 ms |
35564 KB |
Output is correct |
23 |
Correct |
22 ms |
35564 KB |
Output is correct |
24 |
Correct |
22 ms |
35564 KB |
Output is correct |
25 |
Correct |
23 ms |
35564 KB |
Output is correct |
26 |
Correct |
26 ms |
35820 KB |
Output is correct |
27 |
Correct |
25 ms |
35820 KB |
Output is correct |
28 |
Correct |
26 ms |
35948 KB |
Output is correct |
29 |
Correct |
26 ms |
35820 KB |
Output is correct |
30 |
Correct |
29 ms |
35820 KB |
Output is correct |
31 |
Correct |
23 ms |
35692 KB |
Output is correct |
32 |
Correct |
27 ms |
36076 KB |
Output is correct |
33 |
Correct |
23 ms |
35564 KB |
Output is correct |
34 |
Correct |
26 ms |
35820 KB |
Output is correct |
35 |
Correct |
26 ms |
35948 KB |
Output is correct |
36 |
Correct |
26 ms |
35820 KB |
Output is correct |
37 |
Correct |
25 ms |
35820 KB |
Output is correct |
38 |
Correct |
23 ms |
35564 KB |
Output is correct |
39 |
Correct |
25 ms |
35820 KB |
Output is correct |
40 |
Correct |
25 ms |
35820 KB |
Output is correct |
41 |
Correct |
25 ms |
35820 KB |
Output is correct |
42 |
Correct |
25 ms |
35820 KB |
Output is correct |
43 |
Correct |
26 ms |
36076 KB |
Output is correct |
44 |
Correct |
23 ms |
35564 KB |
Output is correct |
45 |
Correct |
27 ms |
35948 KB |
Output is correct |
46 |
Correct |
25 ms |
35820 KB |
Output is correct |
47 |
Correct |
23 ms |
35564 KB |
Output is correct |
48 |
Correct |
26 ms |
35820 KB |
Output is correct |
49 |
Correct |
26 ms |
35948 KB |
Output is correct |
50 |
Correct |
25 ms |
35980 KB |
Output is correct |
51 |
Correct |
25 ms |
35820 KB |
Output is correct |
52 |
Correct |
25 ms |
35820 KB |
Output is correct |
53 |
Correct |
25 ms |
35820 KB |
Output is correct |
54 |
Correct |
25 ms |
35820 KB |
Output is correct |
55 |
Correct |
23 ms |
35564 KB |
Output is correct |
56 |
Correct |
81 ms |
42724 KB |
Output is correct |
57 |
Correct |
124 ms |
42348 KB |
Output is correct |
58 |
Correct |
25 ms |
35820 KB |
Output is correct |
59 |
Correct |
24 ms |
35564 KB |
Output is correct |
60 |
Correct |
23 ms |
35564 KB |
Output is correct |
61 |
Correct |
118 ms |
42476 KB |
Output is correct |
62 |
Correct |
26 ms |
35820 KB |
Output is correct |
63 |
Correct |
145 ms |
49516 KB |
Output is correct |
64 |
Correct |
130 ms |
42476 KB |
Output is correct |
65 |
Correct |
26 ms |
35820 KB |
Output is correct |
66 |
Correct |
119 ms |
43116 KB |
Output is correct |
67 |
Correct |
25 ms |
35820 KB |
Output is correct |
68 |
Correct |
24 ms |
35820 KB |
Output is correct |
69 |
Correct |
87 ms |
42596 KB |
Output is correct |
70 |
Correct |
29 ms |
36076 KB |
Output is correct |
71 |
Correct |
140 ms |
42344 KB |
Output is correct |
72 |
Correct |
116 ms |
44652 KB |
Output is correct |
73 |
Correct |
23 ms |
35564 KB |
Output is correct |
74 |
Correct |
136 ms |
49772 KB |
Output is correct |
75 |
Correct |
133 ms |
46092 KB |
Output is correct |
76 |
Correct |
26 ms |
35860 KB |
Output is correct |
77 |
Correct |
25 ms |
35820 KB |
Output is correct |
78 |
Correct |
84 ms |
42724 KB |
Output is correct |
79 |
Correct |
95 ms |
46004 KB |
Output is correct |
80 |
Correct |
25 ms |
35820 KB |
Output is correct |
81 |
Correct |
24 ms |
35820 KB |
Output is correct |
82 |
Correct |
24 ms |
35564 KB |
Output is correct |
83 |
Correct |
23 ms |
35564 KB |
Output is correct |
84 |
Correct |
25 ms |
35820 KB |
Output is correct |
85 |
Correct |
159 ms |
43756 KB |
Output is correct |
86 |
Correct |
25 ms |
35820 KB |
Output is correct |
87 |
Correct |
110 ms |
42476 KB |
Output is correct |
88 |
Correct |
23 ms |
35564 KB |
Output is correct |
89 |
Correct |
117 ms |
42392 KB |
Output is correct |
90 |
Correct |
130 ms |
43756 KB |
Output is correct |
91 |
Correct |
122 ms |
45676 KB |
Output is correct |
92 |
Correct |
97 ms |
42596 KB |
Output is correct |
93 |
Correct |
26 ms |
35948 KB |
Output is correct |
94 |
Correct |
25 ms |
35692 KB |
Output is correct |
95 |
Correct |
94 ms |
45540 KB |
Output is correct |
96 |
Correct |
146 ms |
49516 KB |
Output is correct |
97 |
Correct |
25 ms |
35840 KB |
Output is correct |
98 |
Correct |
29 ms |
35564 KB |
Output is correct |
99 |
Correct |
102 ms |
44008 KB |
Output is correct |
100 |
Correct |
24 ms |
35820 KB |
Output is correct |
101 |
Correct |
144 ms |
42988 KB |
Output is correct |
102 |
Correct |
147 ms |
48364 KB |
Output is correct |
103 |
Correct |
24 ms |
36096 KB |
Output is correct |
104 |
Correct |
24 ms |
35948 KB |
Output is correct |
105 |
Correct |
24 ms |
35820 KB |
Output is correct |
106 |
Correct |
25 ms |
35820 KB |
Output is correct |
107 |
Correct |
25 ms |
35820 KB |
Output is correct |
108 |
Correct |
630 ms |
77684 KB |
Output is correct |
109 |
Correct |
798 ms |
87336 KB |
Output is correct |
110 |
Correct |
835 ms |
98548 KB |
Output is correct |
111 |
Correct |
923 ms |
110052 KB |
Output is correct |
112 |
Correct |
874 ms |
99456 KB |
Output is correct |
113 |
Correct |
586 ms |
86740 KB |
Output is correct |
114 |
Correct |
722 ms |
69328 KB |
Output is correct |
115 |
Correct |
766 ms |
69392 KB |
Output is correct |
116 |
Correct |
887 ms |
74040 KB |
Output is correct |
117 |
Correct |
854 ms |
87088 KB |
Output is correct |
118 |
Correct |
775 ms |
70644 KB |
Output is correct |
119 |
Correct |
870 ms |
87148 KB |
Output is correct |
120 |
Correct |
905 ms |
101100 KB |
Output is correct |
121 |
Correct |
822 ms |
87180 KB |
Output is correct |
122 |
Correct |
943 ms |
79476 KB |
Output is correct |
123 |
Correct |
596 ms |
88796 KB |
Output is correct |
124 |
Correct |
916 ms |
85304 KB |
Output is correct |