#include <iostream>
#include <fstream>
#include <map>
#include <vector>
using namespace std;
const int nmax=500005;
map<int,int> ap[nmax];
vector<int> v[nmax];
int l[nmax],r[nmax],p[nmax],closed[nmax],w[nmax],sons[nmax],rele[nmax],tot[nmax],col[nmax],c[nmax],tt[nmax];
int nr,n,i,paths,k,x,y,frz;
void baga(int path,int cc)
{
ap[path][cc]++;
if(ap[path][cc]==1) rele[path]++;
if(ap[path][cc]==tot[cc]) rele[path]--;
}
void dfs(int x)
{
int big=0;
w[x]=1;
++nr;l[x]=nr;col[nr]=c[x];
for(int i=0;i<v[x].size();i++)
if(!w[v[x][i]])
{
tt[v[x][i]]=x;
dfs(v[x][i]);
if(w[v[x][i]]>w[big])
big=v[x][i];
w[x]+=w[v[x][i]];
}
if(big&&(!closed[big])) p[x]=p[big];
else
{
p[x]=++paths;
if(big) sons[paths]++;
}
baga(p[x],c[x]);
for(int i=0;i<v[x].size();i++)
if(v[x][i]!=big&&v[x][i]!=tt[x])
{
if(closed[v[x][i]]) sons[p[x]]++;
else
{
sons[p[x]]+=sons[p[v[x][i]]];
for(int j=l[v[x][i]];j<=r[v[x][i]];j++)
baga(p[x],col[j]);
}
}
if(rele[p[x]]==0)
{
closed[x]=1;
if(sons[p[x]]==0) frz++;
}
r[x]=nr;
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>k;
for(i=1;i<=n-1;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(i=1;i<=n;i++)
{
cin>>c[i];
tot[c[i]]++;
}
dfs(1);
if(sons[p[1]]==1) frz++;
if(frz==1) frz=0;
cout<<(frz+1)/2;
return 0;
}
Compilation message
mergers.cpp: In function 'void dfs(int)':
mergers.cpp:22:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
mergers.cpp:38:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
35704 KB |
Output is correct |
2 |
Correct |
35 ms |
35576 KB |
Output is correct |
3 |
Correct |
35 ms |
35576 KB |
Output is correct |
4 |
Correct |
35 ms |
35576 KB |
Output is correct |
5 |
Correct |
35 ms |
35704 KB |
Output is correct |
6 |
Correct |
34 ms |
35704 KB |
Output is correct |
7 |
Correct |
34 ms |
35676 KB |
Output is correct |
8 |
Correct |
34 ms |
35704 KB |
Output is correct |
9 |
Correct |
34 ms |
35704 KB |
Output is correct |
10 |
Correct |
34 ms |
35704 KB |
Output is correct |
11 |
Correct |
34 ms |
35704 KB |
Output is correct |
12 |
Correct |
35 ms |
35688 KB |
Output is correct |
13 |
Correct |
35 ms |
35704 KB |
Output is correct |
14 |
Correct |
35 ms |
35704 KB |
Output is correct |
15 |
Correct |
36 ms |
35704 KB |
Output is correct |
16 |
Correct |
41 ms |
35576 KB |
Output is correct |
17 |
Correct |
35 ms |
35576 KB |
Output is correct |
18 |
Correct |
36 ms |
35576 KB |
Output is correct |
19 |
Correct |
35 ms |
35680 KB |
Output is correct |
20 |
Correct |
35 ms |
35584 KB |
Output is correct |
21 |
Correct |
35 ms |
35704 KB |
Output is correct |
22 |
Correct |
35 ms |
35580 KB |
Output is correct |
23 |
Correct |
35 ms |
35676 KB |
Output is correct |
24 |
Correct |
34 ms |
35576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
35704 KB |
Output is correct |
2 |
Correct |
35 ms |
35576 KB |
Output is correct |
3 |
Correct |
35 ms |
35576 KB |
Output is correct |
4 |
Correct |
35 ms |
35576 KB |
Output is correct |
5 |
Correct |
35 ms |
35704 KB |
Output is correct |
6 |
Correct |
34 ms |
35704 KB |
Output is correct |
7 |
Correct |
34 ms |
35676 KB |
Output is correct |
8 |
Correct |
34 ms |
35704 KB |
Output is correct |
9 |
Correct |
34 ms |
35704 KB |
Output is correct |
10 |
Correct |
34 ms |
35704 KB |
Output is correct |
11 |
Correct |
34 ms |
35704 KB |
Output is correct |
12 |
Correct |
35 ms |
35688 KB |
Output is correct |
13 |
Correct |
35 ms |
35704 KB |
Output is correct |
14 |
Correct |
35 ms |
35704 KB |
Output is correct |
15 |
Correct |
36 ms |
35704 KB |
Output is correct |
16 |
Correct |
41 ms |
35576 KB |
Output is correct |
17 |
Correct |
35 ms |
35576 KB |
Output is correct |
18 |
Correct |
36 ms |
35576 KB |
Output is correct |
19 |
Correct |
35 ms |
35680 KB |
Output is correct |
20 |
Correct |
35 ms |
35584 KB |
Output is correct |
21 |
Correct |
35 ms |
35704 KB |
Output is correct |
22 |
Correct |
35 ms |
35580 KB |
Output is correct |
23 |
Correct |
35 ms |
35676 KB |
Output is correct |
24 |
Correct |
34 ms |
35576 KB |
Output is correct |
25 |
Correct |
35 ms |
35576 KB |
Output is correct |
26 |
Correct |
38 ms |
36012 KB |
Output is correct |
27 |
Correct |
38 ms |
35872 KB |
Output is correct |
28 |
Correct |
38 ms |
36088 KB |
Output is correct |
29 |
Correct |
38 ms |
36060 KB |
Output is correct |
30 |
Correct |
39 ms |
36088 KB |
Output is correct |
31 |
Correct |
35 ms |
35704 KB |
Output is correct |
32 |
Correct |
39 ms |
36240 KB |
Output is correct |
33 |
Correct |
36 ms |
35704 KB |
Output is correct |
34 |
Correct |
38 ms |
36088 KB |
Output is correct |
35 |
Correct |
40 ms |
36088 KB |
Output is correct |
36 |
Correct |
36 ms |
35960 KB |
Output is correct |
37 |
Correct |
40 ms |
36216 KB |
Output is correct |
38 |
Correct |
35 ms |
35704 KB |
Output is correct |
39 |
Correct |
41 ms |
36344 KB |
Output is correct |
40 |
Correct |
37 ms |
35956 KB |
Output is correct |
41 |
Correct |
37 ms |
35960 KB |
Output is correct |
42 |
Correct |
37 ms |
36088 KB |
Output is correct |
43 |
Correct |
37 ms |
36088 KB |
Output is correct |
44 |
Correct |
35 ms |
35576 KB |
Output is correct |
45 |
Correct |
40 ms |
36216 KB |
Output is correct |
46 |
Correct |
39 ms |
36192 KB |
Output is correct |
47 |
Correct |
35 ms |
35704 KB |
Output is correct |
48 |
Correct |
37 ms |
36088 KB |
Output is correct |
49 |
Correct |
37 ms |
36088 KB |
Output is correct |
50 |
Correct |
37 ms |
36060 KB |
Output is correct |
51 |
Correct |
37 ms |
35960 KB |
Output is correct |
52 |
Correct |
37 ms |
36088 KB |
Output is correct |
53 |
Correct |
38 ms |
36088 KB |
Output is correct |
54 |
Correct |
38 ms |
36088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
35704 KB |
Output is correct |
2 |
Correct |
35 ms |
35576 KB |
Output is correct |
3 |
Correct |
35 ms |
35576 KB |
Output is correct |
4 |
Correct |
35 ms |
35576 KB |
Output is correct |
5 |
Correct |
35 ms |
35704 KB |
Output is correct |
6 |
Correct |
34 ms |
35704 KB |
Output is correct |
7 |
Correct |
34 ms |
35676 KB |
Output is correct |
8 |
Correct |
34 ms |
35704 KB |
Output is correct |
9 |
Correct |
34 ms |
35704 KB |
Output is correct |
10 |
Correct |
34 ms |
35704 KB |
Output is correct |
11 |
Correct |
34 ms |
35704 KB |
Output is correct |
12 |
Correct |
35 ms |
35688 KB |
Output is correct |
13 |
Correct |
35 ms |
35704 KB |
Output is correct |
14 |
Correct |
35 ms |
35704 KB |
Output is correct |
15 |
Correct |
36 ms |
35704 KB |
Output is correct |
16 |
Correct |
41 ms |
35576 KB |
Output is correct |
17 |
Correct |
35 ms |
35576 KB |
Output is correct |
18 |
Correct |
36 ms |
35576 KB |
Output is correct |
19 |
Correct |
35 ms |
35680 KB |
Output is correct |
20 |
Correct |
35 ms |
35584 KB |
Output is correct |
21 |
Correct |
35 ms |
35704 KB |
Output is correct |
22 |
Correct |
35 ms |
35580 KB |
Output is correct |
23 |
Correct |
35 ms |
35676 KB |
Output is correct |
24 |
Correct |
34 ms |
35576 KB |
Output is correct |
25 |
Correct |
36 ms |
35620 KB |
Output is correct |
26 |
Correct |
124 ms |
48368 KB |
Output is correct |
27 |
Correct |
199 ms |
51460 KB |
Output is correct |
28 |
Correct |
37 ms |
35960 KB |
Output is correct |
29 |
Correct |
35 ms |
35704 KB |
Output is correct |
30 |
Correct |
34 ms |
35704 KB |
Output is correct |
31 |
Correct |
134 ms |
45944 KB |
Output is correct |
32 |
Correct |
37 ms |
36088 KB |
Output is correct |
33 |
Correct |
164 ms |
48860 KB |
Output is correct |
34 |
Correct |
177 ms |
50296 KB |
Output is correct |
35 |
Correct |
38 ms |
35960 KB |
Output is correct |
36 |
Correct |
130 ms |
43768 KB |
Output is correct |
37 |
Correct |
37 ms |
35932 KB |
Output is correct |
38 |
Correct |
37 ms |
35960 KB |
Output is correct |
39 |
Correct |
128 ms |
48368 KB |
Output is correct |
40 |
Correct |
38 ms |
36088 KB |
Output is correct |
41 |
Correct |
130 ms |
45816 KB |
Output is correct |
42 |
Correct |
137 ms |
44908 KB |
Output is correct |
43 |
Correct |
34 ms |
35736 KB |
Output is correct |
44 |
Correct |
135 ms |
48888 KB |
Output is correct |
45 |
Correct |
151 ms |
48604 KB |
Output is correct |
46 |
Correct |
38 ms |
36088 KB |
Output is correct |
47 |
Correct |
38 ms |
36088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
46952 KB |
Output is correct |
2 |
Correct |
125 ms |
47840 KB |
Output is correct |
3 |
Correct |
37 ms |
36088 KB |
Output is correct |
4 |
Correct |
37 ms |
35832 KB |
Output is correct |
5 |
Correct |
35 ms |
35704 KB |
Output is correct |
6 |
Correct |
34 ms |
35576 KB |
Output is correct |
7 |
Correct |
36 ms |
35960 KB |
Output is correct |
8 |
Correct |
257 ms |
55896 KB |
Output is correct |
9 |
Correct |
37 ms |
35960 KB |
Output is correct |
10 |
Correct |
146 ms |
46428 KB |
Output is correct |
11 |
Correct |
35 ms |
35576 KB |
Output is correct |
12 |
Correct |
134 ms |
43672 KB |
Output is correct |
13 |
Correct |
216 ms |
53628 KB |
Output is correct |
14 |
Correct |
153 ms |
49732 KB |
Output is correct |
15 |
Correct |
130 ms |
48372 KB |
Output is correct |
16 |
Correct |
38 ms |
36088 KB |
Output is correct |
17 |
Correct |
34 ms |
35704 KB |
Output is correct |
18 |
Correct |
138 ms |
49904 KB |
Output is correct |
19 |
Correct |
162 ms |
52648 KB |
Output is correct |
20 |
Correct |
39 ms |
36216 KB |
Output is correct |
21 |
Correct |
35 ms |
35576 KB |
Output is correct |
22 |
Correct |
172 ms |
50928 KB |
Output is correct |
23 |
Correct |
38 ms |
35960 KB |
Output is correct |
24 |
Correct |
154 ms |
45720 KB |
Output is correct |
25 |
Correct |
156 ms |
51496 KB |
Output is correct |
26 |
Correct |
38 ms |
36060 KB |
Output is correct |
27 |
Correct |
37 ms |
36060 KB |
Output is correct |
28 |
Correct |
38 ms |
35960 KB |
Output is correct |
29 |
Correct |
37 ms |
36088 KB |
Output is correct |
30 |
Correct |
38 ms |
36088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
35704 KB |
Output is correct |
2 |
Correct |
35 ms |
35576 KB |
Output is correct |
3 |
Correct |
35 ms |
35576 KB |
Output is correct |
4 |
Correct |
35 ms |
35576 KB |
Output is correct |
5 |
Correct |
35 ms |
35704 KB |
Output is correct |
6 |
Correct |
34 ms |
35704 KB |
Output is correct |
7 |
Correct |
34 ms |
35676 KB |
Output is correct |
8 |
Correct |
34 ms |
35704 KB |
Output is correct |
9 |
Correct |
34 ms |
35704 KB |
Output is correct |
10 |
Correct |
34 ms |
35704 KB |
Output is correct |
11 |
Correct |
34 ms |
35704 KB |
Output is correct |
12 |
Correct |
35 ms |
35688 KB |
Output is correct |
13 |
Correct |
35 ms |
35704 KB |
Output is correct |
14 |
Correct |
35 ms |
35704 KB |
Output is correct |
15 |
Correct |
36 ms |
35704 KB |
Output is correct |
16 |
Correct |
41 ms |
35576 KB |
Output is correct |
17 |
Correct |
35 ms |
35576 KB |
Output is correct |
18 |
Correct |
36 ms |
35576 KB |
Output is correct |
19 |
Correct |
35 ms |
35680 KB |
Output is correct |
20 |
Correct |
35 ms |
35584 KB |
Output is correct |
21 |
Correct |
35 ms |
35704 KB |
Output is correct |
22 |
Correct |
35 ms |
35580 KB |
Output is correct |
23 |
Correct |
35 ms |
35676 KB |
Output is correct |
24 |
Correct |
34 ms |
35576 KB |
Output is correct |
25 |
Correct |
35 ms |
35576 KB |
Output is correct |
26 |
Correct |
38 ms |
36012 KB |
Output is correct |
27 |
Correct |
38 ms |
35872 KB |
Output is correct |
28 |
Correct |
38 ms |
36088 KB |
Output is correct |
29 |
Correct |
38 ms |
36060 KB |
Output is correct |
30 |
Correct |
39 ms |
36088 KB |
Output is correct |
31 |
Correct |
35 ms |
35704 KB |
Output is correct |
32 |
Correct |
39 ms |
36240 KB |
Output is correct |
33 |
Correct |
36 ms |
35704 KB |
Output is correct |
34 |
Correct |
38 ms |
36088 KB |
Output is correct |
35 |
Correct |
40 ms |
36088 KB |
Output is correct |
36 |
Correct |
36 ms |
35960 KB |
Output is correct |
37 |
Correct |
40 ms |
36216 KB |
Output is correct |
38 |
Correct |
35 ms |
35704 KB |
Output is correct |
39 |
Correct |
41 ms |
36344 KB |
Output is correct |
40 |
Correct |
37 ms |
35956 KB |
Output is correct |
41 |
Correct |
37 ms |
35960 KB |
Output is correct |
42 |
Correct |
37 ms |
36088 KB |
Output is correct |
43 |
Correct |
37 ms |
36088 KB |
Output is correct |
44 |
Correct |
35 ms |
35576 KB |
Output is correct |
45 |
Correct |
40 ms |
36216 KB |
Output is correct |
46 |
Correct |
39 ms |
36192 KB |
Output is correct |
47 |
Correct |
35 ms |
35704 KB |
Output is correct |
48 |
Correct |
37 ms |
36088 KB |
Output is correct |
49 |
Correct |
37 ms |
36088 KB |
Output is correct |
50 |
Correct |
37 ms |
36060 KB |
Output is correct |
51 |
Correct |
37 ms |
35960 KB |
Output is correct |
52 |
Correct |
37 ms |
36088 KB |
Output is correct |
53 |
Correct |
38 ms |
36088 KB |
Output is correct |
54 |
Correct |
38 ms |
36088 KB |
Output is correct |
55 |
Correct |
36 ms |
35620 KB |
Output is correct |
56 |
Correct |
124 ms |
48368 KB |
Output is correct |
57 |
Correct |
199 ms |
51460 KB |
Output is correct |
58 |
Correct |
37 ms |
35960 KB |
Output is correct |
59 |
Correct |
35 ms |
35704 KB |
Output is correct |
60 |
Correct |
34 ms |
35704 KB |
Output is correct |
61 |
Correct |
134 ms |
45944 KB |
Output is correct |
62 |
Correct |
37 ms |
36088 KB |
Output is correct |
63 |
Correct |
164 ms |
48860 KB |
Output is correct |
64 |
Correct |
177 ms |
50296 KB |
Output is correct |
65 |
Correct |
38 ms |
35960 KB |
Output is correct |
66 |
Correct |
130 ms |
43768 KB |
Output is correct |
67 |
Correct |
37 ms |
35932 KB |
Output is correct |
68 |
Correct |
37 ms |
35960 KB |
Output is correct |
69 |
Correct |
128 ms |
48368 KB |
Output is correct |
70 |
Correct |
38 ms |
36088 KB |
Output is correct |
71 |
Correct |
130 ms |
45816 KB |
Output is correct |
72 |
Correct |
137 ms |
44908 KB |
Output is correct |
73 |
Correct |
34 ms |
35736 KB |
Output is correct |
74 |
Correct |
135 ms |
48888 KB |
Output is correct |
75 |
Correct |
151 ms |
48604 KB |
Output is correct |
76 |
Correct |
38 ms |
36088 KB |
Output is correct |
77 |
Correct |
38 ms |
36088 KB |
Output is correct |
78 |
Correct |
125 ms |
46952 KB |
Output is correct |
79 |
Correct |
125 ms |
47840 KB |
Output is correct |
80 |
Correct |
37 ms |
36088 KB |
Output is correct |
81 |
Correct |
37 ms |
35832 KB |
Output is correct |
82 |
Correct |
35 ms |
35704 KB |
Output is correct |
83 |
Correct |
34 ms |
35576 KB |
Output is correct |
84 |
Correct |
36 ms |
35960 KB |
Output is correct |
85 |
Correct |
257 ms |
55896 KB |
Output is correct |
86 |
Correct |
37 ms |
35960 KB |
Output is correct |
87 |
Correct |
146 ms |
46428 KB |
Output is correct |
88 |
Correct |
35 ms |
35576 KB |
Output is correct |
89 |
Correct |
134 ms |
43672 KB |
Output is correct |
90 |
Correct |
216 ms |
53628 KB |
Output is correct |
91 |
Correct |
153 ms |
49732 KB |
Output is correct |
92 |
Correct |
130 ms |
48372 KB |
Output is correct |
93 |
Correct |
38 ms |
36088 KB |
Output is correct |
94 |
Correct |
34 ms |
35704 KB |
Output is correct |
95 |
Correct |
138 ms |
49904 KB |
Output is correct |
96 |
Correct |
162 ms |
52648 KB |
Output is correct |
97 |
Correct |
39 ms |
36216 KB |
Output is correct |
98 |
Correct |
35 ms |
35576 KB |
Output is correct |
99 |
Correct |
172 ms |
50928 KB |
Output is correct |
100 |
Correct |
38 ms |
35960 KB |
Output is correct |
101 |
Correct |
154 ms |
45720 KB |
Output is correct |
102 |
Correct |
156 ms |
51496 KB |
Output is correct |
103 |
Correct |
38 ms |
36060 KB |
Output is correct |
104 |
Correct |
37 ms |
36060 KB |
Output is correct |
105 |
Correct |
38 ms |
35960 KB |
Output is correct |
106 |
Correct |
37 ms |
36088 KB |
Output is correct |
107 |
Correct |
38 ms |
36088 KB |
Output is correct |
108 |
Correct |
1135 ms |
106836 KB |
Output is correct |
109 |
Correct |
779 ms |
84092 KB |
Output is correct |
110 |
Correct |
801 ms |
93220 KB |
Output is correct |
111 |
Correct |
1241 ms |
128660 KB |
Output is correct |
112 |
Correct |
1328 ms |
115952 KB |
Output is correct |
113 |
Correct |
865 ms |
102824 KB |
Output is correct |
114 |
Correct |
772 ms |
83784 KB |
Output is correct |
115 |
Correct |
782 ms |
83960 KB |
Output is correct |
116 |
Correct |
1605 ms |
147320 KB |
Output is correct |
117 |
Correct |
952 ms |
101608 KB |
Output is correct |
118 |
Correct |
861 ms |
84528 KB |
Output is correct |
119 |
Correct |
937 ms |
104784 KB |
Output is correct |
120 |
Correct |
947 ms |
111876 KB |
Output is correct |
121 |
Correct |
1678 ms |
140964 KB |
Output is correct |
122 |
Correct |
2195 ms |
190164 KB |
Output is correct |
123 |
Correct |
756 ms |
100872 KB |
Output is correct |
124 |
Correct |
1145 ms |
117856 KB |
Output is correct |