#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
const int nax=5e5+5;
vector<int> adj[nax];
set<int> stl[nax];
vector<int> sz(nax);
vector<int> sum(nax);
vector<int> s(nax);
vector<int> colours(nax);
int ans=0;
vector<int> parent(nax);
vector<int> adj2[nax];
vector<bool> vis(nax);
int find(int x){
if(parent[x]==x) return x;
return parent[x]=find(parent[x]);
}
bool sameset(int x,int y){
return find(x)==find(y);
}
void joinset(int x,int y){
x=find(x);
y=find(y);
parent[x]=y;
}
void precompute(int x,int p){
stl[x].insert(s[x]);
sum[x]=colours[s[x]];
sz[x]=1;
for(auto u:adj[x]){
if(u==p) continue;
precompute(u,x);
sz[x]+=sz[u];
if(stl[u].size()>stl[x].size()){
sum[x]=sum[u];
swap(stl[u],stl[x]);
}
for(auto i:stl[u]){
if(stl[x].count(i)) continue;
stl[x].insert(i);
sum[x]+=colours[i];
}
}
if(p!=-1&&sum[x]>sz[x]){
joinset(x,p);
}
}
void nab(int x,int p){
for(auto u:adj[x]){
if(u==p) continue;
if(!sameset(x,u)){
int curx=find(x);
int cury=find(u);
adj2[curx].pb(cury);
adj2[cury].pb(curx);
}
nab(u,x);
}
}
void dfs(int x,int p){
//cout <<x<<" "<<adj2[x].size()<<endl;
if(adj2[x].size()==1) ans++;
for(auto u:adj2[x]){
if(u==p) continue;
dfs(u,x);
}
return;
}
int main() {
int n,k;
cin>>n>>k;
for (int i = 0; i < n-1; ++i)
{
int a,b;
cin>>a>>b;
a--;b--;
adj[a].pb(b);
adj[b].pb(a);
}
for (int i = 0; i < n; ++i)
{
parent[i]=i;
}
for (int i = 0; i < n; ++i)
{
cin>>s[i];
s[i]--;
colours[s[i]]++;
}
precompute(0,-1);
nab(0,-1);
for (int i = 0; i < n; ++i)
{
if(adj2[i].size()>0){
dfs(i,-1);
break;
}
}
cout <<(ans+1)/2<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
57180 KB |
Output is correct |
2 |
Correct |
13 ms |
57276 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
13 ms |
57152 KB |
Output is correct |
5 |
Correct |
13 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
13 ms |
57180 KB |
Output is correct |
8 |
Correct |
13 ms |
57204 KB |
Output is correct |
9 |
Correct |
13 ms |
57264 KB |
Output is correct |
10 |
Correct |
13 ms |
57176 KB |
Output is correct |
11 |
Correct |
14 ms |
57180 KB |
Output is correct |
12 |
Correct |
17 ms |
57180 KB |
Output is correct |
13 |
Correct |
11 ms |
57180 KB |
Output is correct |
14 |
Correct |
13 ms |
57180 KB |
Output is correct |
15 |
Correct |
14 ms |
57180 KB |
Output is correct |
16 |
Correct |
14 ms |
57180 KB |
Output is correct |
17 |
Correct |
13 ms |
57276 KB |
Output is correct |
18 |
Correct |
14 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
57040 KB |
Output is correct |
20 |
Correct |
12 ms |
57180 KB |
Output is correct |
21 |
Correct |
12 ms |
57180 KB |
Output is correct |
22 |
Correct |
12 ms |
57180 KB |
Output is correct |
23 |
Correct |
11 ms |
57260 KB |
Output is correct |
24 |
Correct |
11 ms |
57260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
57180 KB |
Output is correct |
2 |
Correct |
13 ms |
57276 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
13 ms |
57152 KB |
Output is correct |
5 |
Correct |
13 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
13 ms |
57180 KB |
Output is correct |
8 |
Correct |
13 ms |
57204 KB |
Output is correct |
9 |
Correct |
13 ms |
57264 KB |
Output is correct |
10 |
Correct |
13 ms |
57176 KB |
Output is correct |
11 |
Correct |
14 ms |
57180 KB |
Output is correct |
12 |
Correct |
17 ms |
57180 KB |
Output is correct |
13 |
Correct |
11 ms |
57180 KB |
Output is correct |
14 |
Correct |
13 ms |
57180 KB |
Output is correct |
15 |
Correct |
14 ms |
57180 KB |
Output is correct |
16 |
Correct |
14 ms |
57180 KB |
Output is correct |
17 |
Correct |
13 ms |
57276 KB |
Output is correct |
18 |
Correct |
14 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
57040 KB |
Output is correct |
20 |
Correct |
12 ms |
57180 KB |
Output is correct |
21 |
Correct |
12 ms |
57180 KB |
Output is correct |
22 |
Correct |
12 ms |
57180 KB |
Output is correct |
23 |
Correct |
11 ms |
57260 KB |
Output is correct |
24 |
Correct |
11 ms |
57260 KB |
Output is correct |
25 |
Correct |
11 ms |
57180 KB |
Output is correct |
26 |
Correct |
14 ms |
57692 KB |
Output is correct |
27 |
Correct |
14 ms |
57436 KB |
Output is correct |
28 |
Correct |
14 ms |
57948 KB |
Output is correct |
29 |
Correct |
15 ms |
57948 KB |
Output is correct |
30 |
Correct |
13 ms |
57664 KB |
Output is correct |
31 |
Correct |
11 ms |
57276 KB |
Output is correct |
32 |
Correct |
13 ms |
57928 KB |
Output is correct |
33 |
Correct |
11 ms |
57180 KB |
Output is correct |
34 |
Correct |
13 ms |
57448 KB |
Output is correct |
35 |
Correct |
14 ms |
57948 KB |
Output is correct |
36 |
Correct |
15 ms |
57436 KB |
Output is correct |
37 |
Correct |
18 ms |
57944 KB |
Output is correct |
38 |
Correct |
13 ms |
57180 KB |
Output is correct |
39 |
Correct |
17 ms |
57952 KB |
Output is correct |
40 |
Correct |
15 ms |
57436 KB |
Output is correct |
41 |
Correct |
15 ms |
57436 KB |
Output is correct |
42 |
Correct |
17 ms |
57664 KB |
Output is correct |
43 |
Correct |
14 ms |
57692 KB |
Output is correct |
44 |
Correct |
12 ms |
57060 KB |
Output is correct |
45 |
Correct |
16 ms |
57692 KB |
Output is correct |
46 |
Correct |
14 ms |
57692 KB |
Output is correct |
47 |
Correct |
13 ms |
57180 KB |
Output is correct |
48 |
Correct |
18 ms |
57944 KB |
Output is correct |
49 |
Correct |
15 ms |
57692 KB |
Output is correct |
50 |
Correct |
16 ms |
57948 KB |
Output is correct |
51 |
Correct |
16 ms |
57436 KB |
Output is correct |
52 |
Correct |
15 ms |
57472 KB |
Output is correct |
53 |
Correct |
13 ms |
57436 KB |
Output is correct |
54 |
Correct |
16 ms |
57704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
57180 KB |
Output is correct |
2 |
Correct |
13 ms |
57276 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
13 ms |
57152 KB |
Output is correct |
5 |
Correct |
13 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
13 ms |
57180 KB |
Output is correct |
8 |
Correct |
13 ms |
57204 KB |
Output is correct |
9 |
Correct |
13 ms |
57264 KB |
Output is correct |
10 |
Correct |
13 ms |
57176 KB |
Output is correct |
11 |
Correct |
14 ms |
57180 KB |
Output is correct |
12 |
Correct |
17 ms |
57180 KB |
Output is correct |
13 |
Correct |
11 ms |
57180 KB |
Output is correct |
14 |
Correct |
13 ms |
57180 KB |
Output is correct |
15 |
Correct |
14 ms |
57180 KB |
Output is correct |
16 |
Correct |
14 ms |
57180 KB |
Output is correct |
17 |
Correct |
13 ms |
57276 KB |
Output is correct |
18 |
Correct |
14 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
57040 KB |
Output is correct |
20 |
Correct |
12 ms |
57180 KB |
Output is correct |
21 |
Correct |
12 ms |
57180 KB |
Output is correct |
22 |
Correct |
12 ms |
57180 KB |
Output is correct |
23 |
Correct |
11 ms |
57260 KB |
Output is correct |
24 |
Correct |
11 ms |
57260 KB |
Output is correct |
25 |
Correct |
13 ms |
57180 KB |
Output is correct |
26 |
Correct |
81 ms |
66764 KB |
Output is correct |
27 |
Correct |
111 ms |
73300 KB |
Output is correct |
28 |
Correct |
13 ms |
57436 KB |
Output is correct |
29 |
Correct |
12 ms |
57184 KB |
Output is correct |
30 |
Correct |
13 ms |
57180 KB |
Output is correct |
31 |
Correct |
83 ms |
66644 KB |
Output is correct |
32 |
Correct |
16 ms |
57436 KB |
Output is correct |
33 |
Correct |
93 ms |
78156 KB |
Output is correct |
34 |
Correct |
100 ms |
71864 KB |
Output is correct |
35 |
Correct |
15 ms |
57436 KB |
Output is correct |
36 |
Correct |
96 ms |
67668 KB |
Output is correct |
37 |
Correct |
13 ms |
57436 KB |
Output is correct |
38 |
Correct |
15 ms |
57436 KB |
Output is correct |
39 |
Correct |
83 ms |
66892 KB |
Output is correct |
40 |
Correct |
15 ms |
57692 KB |
Output is correct |
41 |
Correct |
84 ms |
66392 KB |
Output is correct |
42 |
Correct |
93 ms |
70228 KB |
Output is correct |
43 |
Correct |
11 ms |
57180 KB |
Output is correct |
44 |
Correct |
88 ms |
78420 KB |
Output is correct |
45 |
Correct |
100 ms |
73544 KB |
Output is correct |
46 |
Correct |
13 ms |
57436 KB |
Output is correct |
47 |
Correct |
13 ms |
57692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
65480 KB |
Output is correct |
2 |
Correct |
127 ms |
73676 KB |
Output is correct |
3 |
Correct |
14 ms |
57692 KB |
Output is correct |
4 |
Correct |
15 ms |
57436 KB |
Output is correct |
5 |
Correct |
12 ms |
57088 KB |
Output is correct |
6 |
Correct |
13 ms |
57180 KB |
Output is correct |
7 |
Correct |
15 ms |
57404 KB |
Output is correct |
8 |
Correct |
149 ms |
80220 KB |
Output is correct |
9 |
Correct |
16 ms |
57436 KB |
Output is correct |
10 |
Correct |
92 ms |
66900 KB |
Output is correct |
11 |
Correct |
13 ms |
57180 KB |
Output is correct |
12 |
Correct |
95 ms |
66644 KB |
Output is correct |
13 |
Correct |
149 ms |
79188 KB |
Output is correct |
14 |
Correct |
200 ms |
94332 KB |
Output is correct |
15 |
Correct |
87 ms |
66636 KB |
Output is correct |
16 |
Correct |
15 ms |
57692 KB |
Output is correct |
17 |
Correct |
13 ms |
57180 KB |
Output is correct |
18 |
Correct |
146 ms |
75216 KB |
Output is correct |
19 |
Correct |
161 ms |
83164 KB |
Output is correct |
20 |
Correct |
16 ms |
57692 KB |
Output is correct |
21 |
Correct |
13 ms |
57180 KB |
Output is correct |
22 |
Correct |
123 ms |
69948 KB |
Output is correct |
23 |
Correct |
15 ms |
57436 KB |
Output is correct |
24 |
Correct |
107 ms |
68972 KB |
Output is correct |
25 |
Correct |
160 ms |
81644 KB |
Output is correct |
26 |
Correct |
16 ms |
57692 KB |
Output is correct |
27 |
Correct |
16 ms |
57948 KB |
Output is correct |
28 |
Correct |
16 ms |
57664 KB |
Output is correct |
29 |
Correct |
15 ms |
57436 KB |
Output is correct |
30 |
Correct |
15 ms |
57636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
57180 KB |
Output is correct |
2 |
Correct |
13 ms |
57276 KB |
Output is correct |
3 |
Correct |
12 ms |
57180 KB |
Output is correct |
4 |
Correct |
13 ms |
57152 KB |
Output is correct |
5 |
Correct |
13 ms |
57180 KB |
Output is correct |
6 |
Correct |
12 ms |
57180 KB |
Output is correct |
7 |
Correct |
13 ms |
57180 KB |
Output is correct |
8 |
Correct |
13 ms |
57204 KB |
Output is correct |
9 |
Correct |
13 ms |
57264 KB |
Output is correct |
10 |
Correct |
13 ms |
57176 KB |
Output is correct |
11 |
Correct |
14 ms |
57180 KB |
Output is correct |
12 |
Correct |
17 ms |
57180 KB |
Output is correct |
13 |
Correct |
11 ms |
57180 KB |
Output is correct |
14 |
Correct |
13 ms |
57180 KB |
Output is correct |
15 |
Correct |
14 ms |
57180 KB |
Output is correct |
16 |
Correct |
14 ms |
57180 KB |
Output is correct |
17 |
Correct |
13 ms |
57276 KB |
Output is correct |
18 |
Correct |
14 ms |
57180 KB |
Output is correct |
19 |
Correct |
14 ms |
57040 KB |
Output is correct |
20 |
Correct |
12 ms |
57180 KB |
Output is correct |
21 |
Correct |
12 ms |
57180 KB |
Output is correct |
22 |
Correct |
12 ms |
57180 KB |
Output is correct |
23 |
Correct |
11 ms |
57260 KB |
Output is correct |
24 |
Correct |
11 ms |
57260 KB |
Output is correct |
25 |
Correct |
11 ms |
57180 KB |
Output is correct |
26 |
Correct |
14 ms |
57692 KB |
Output is correct |
27 |
Correct |
14 ms |
57436 KB |
Output is correct |
28 |
Correct |
14 ms |
57948 KB |
Output is correct |
29 |
Correct |
15 ms |
57948 KB |
Output is correct |
30 |
Correct |
13 ms |
57664 KB |
Output is correct |
31 |
Correct |
11 ms |
57276 KB |
Output is correct |
32 |
Correct |
13 ms |
57928 KB |
Output is correct |
33 |
Correct |
11 ms |
57180 KB |
Output is correct |
34 |
Correct |
13 ms |
57448 KB |
Output is correct |
35 |
Correct |
14 ms |
57948 KB |
Output is correct |
36 |
Correct |
15 ms |
57436 KB |
Output is correct |
37 |
Correct |
18 ms |
57944 KB |
Output is correct |
38 |
Correct |
13 ms |
57180 KB |
Output is correct |
39 |
Correct |
17 ms |
57952 KB |
Output is correct |
40 |
Correct |
15 ms |
57436 KB |
Output is correct |
41 |
Correct |
15 ms |
57436 KB |
Output is correct |
42 |
Correct |
17 ms |
57664 KB |
Output is correct |
43 |
Correct |
14 ms |
57692 KB |
Output is correct |
44 |
Correct |
12 ms |
57060 KB |
Output is correct |
45 |
Correct |
16 ms |
57692 KB |
Output is correct |
46 |
Correct |
14 ms |
57692 KB |
Output is correct |
47 |
Correct |
13 ms |
57180 KB |
Output is correct |
48 |
Correct |
18 ms |
57944 KB |
Output is correct |
49 |
Correct |
15 ms |
57692 KB |
Output is correct |
50 |
Correct |
16 ms |
57948 KB |
Output is correct |
51 |
Correct |
16 ms |
57436 KB |
Output is correct |
52 |
Correct |
15 ms |
57472 KB |
Output is correct |
53 |
Correct |
13 ms |
57436 KB |
Output is correct |
54 |
Correct |
16 ms |
57704 KB |
Output is correct |
55 |
Correct |
13 ms |
57180 KB |
Output is correct |
56 |
Correct |
81 ms |
66764 KB |
Output is correct |
57 |
Correct |
111 ms |
73300 KB |
Output is correct |
58 |
Correct |
13 ms |
57436 KB |
Output is correct |
59 |
Correct |
12 ms |
57184 KB |
Output is correct |
60 |
Correct |
13 ms |
57180 KB |
Output is correct |
61 |
Correct |
83 ms |
66644 KB |
Output is correct |
62 |
Correct |
16 ms |
57436 KB |
Output is correct |
63 |
Correct |
93 ms |
78156 KB |
Output is correct |
64 |
Correct |
100 ms |
71864 KB |
Output is correct |
65 |
Correct |
15 ms |
57436 KB |
Output is correct |
66 |
Correct |
96 ms |
67668 KB |
Output is correct |
67 |
Correct |
13 ms |
57436 KB |
Output is correct |
68 |
Correct |
15 ms |
57436 KB |
Output is correct |
69 |
Correct |
83 ms |
66892 KB |
Output is correct |
70 |
Correct |
15 ms |
57692 KB |
Output is correct |
71 |
Correct |
84 ms |
66392 KB |
Output is correct |
72 |
Correct |
93 ms |
70228 KB |
Output is correct |
73 |
Correct |
11 ms |
57180 KB |
Output is correct |
74 |
Correct |
88 ms |
78420 KB |
Output is correct |
75 |
Correct |
100 ms |
73544 KB |
Output is correct |
76 |
Correct |
13 ms |
57436 KB |
Output is correct |
77 |
Correct |
13 ms |
57692 KB |
Output is correct |
78 |
Correct |
76 ms |
65480 KB |
Output is correct |
79 |
Correct |
127 ms |
73676 KB |
Output is correct |
80 |
Correct |
14 ms |
57692 KB |
Output is correct |
81 |
Correct |
15 ms |
57436 KB |
Output is correct |
82 |
Correct |
12 ms |
57088 KB |
Output is correct |
83 |
Correct |
13 ms |
57180 KB |
Output is correct |
84 |
Correct |
15 ms |
57404 KB |
Output is correct |
85 |
Correct |
149 ms |
80220 KB |
Output is correct |
86 |
Correct |
16 ms |
57436 KB |
Output is correct |
87 |
Correct |
92 ms |
66900 KB |
Output is correct |
88 |
Correct |
13 ms |
57180 KB |
Output is correct |
89 |
Correct |
95 ms |
66644 KB |
Output is correct |
90 |
Correct |
149 ms |
79188 KB |
Output is correct |
91 |
Correct |
200 ms |
94332 KB |
Output is correct |
92 |
Correct |
87 ms |
66636 KB |
Output is correct |
93 |
Correct |
15 ms |
57692 KB |
Output is correct |
94 |
Correct |
13 ms |
57180 KB |
Output is correct |
95 |
Correct |
146 ms |
75216 KB |
Output is correct |
96 |
Correct |
161 ms |
83164 KB |
Output is correct |
97 |
Correct |
16 ms |
57692 KB |
Output is correct |
98 |
Correct |
13 ms |
57180 KB |
Output is correct |
99 |
Correct |
123 ms |
69948 KB |
Output is correct |
100 |
Correct |
15 ms |
57436 KB |
Output is correct |
101 |
Correct |
107 ms |
68972 KB |
Output is correct |
102 |
Correct |
160 ms |
81644 KB |
Output is correct |
103 |
Correct |
16 ms |
57692 KB |
Output is correct |
104 |
Correct |
16 ms |
57948 KB |
Output is correct |
105 |
Correct |
16 ms |
57664 KB |
Output is correct |
106 |
Correct |
15 ms |
57436 KB |
Output is correct |
107 |
Correct |
15 ms |
57636 KB |
Output is correct |
108 |
Correct |
770 ms |
119236 KB |
Output is correct |
109 |
Correct |
705 ms |
133460 KB |
Output is correct |
110 |
Correct |
624 ms |
151636 KB |
Output is correct |
111 |
Correct |
1154 ms |
191124 KB |
Output is correct |
112 |
Correct |
1091 ms |
174024 KB |
Output is correct |
113 |
Correct |
954 ms |
144004 KB |
Output is correct |
114 |
Correct |
553 ms |
104532 KB |
Output is correct |
115 |
Correct |
544 ms |
104532 KB |
Output is correct |
116 |
Correct |
880 ms |
177764 KB |
Output is correct |
117 |
Correct |
1310 ms |
260016 KB |
Output is correct |
118 |
Correct |
545 ms |
104568 KB |
Output is correct |
119 |
Correct |
1259 ms |
256088 KB |
Output is correct |
120 |
Correct |
1123 ms |
180424 KB |
Output is correct |
121 |
Correct |
1335 ms |
252500 KB |
Output is correct |
122 |
Correct |
1167 ms |
231036 KB |
Output is correct |
123 |
Correct |
963 ms |
149444 KB |
Output is correct |
124 |
Correct |
1115 ms |
201504 KB |
Output is correct |