#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 5e5 + 7;
int n, k, u, v, s[N], level[N], sub[N], par[20][N], cnt = 0, pset[N], ret[N], sz[N], deg[N], now[N], ans = 0;
bool vis[N], col[N];
vector <int> edge[N], tp[N], g[N], leaf;
unordered_map < long long, int > ed;
int read(){
char p;
while((p = getchar())){
if(p < '0' || p > '9') continue;
break;
}
int ret = p - '0';
while((p = getchar())){
if(p >= '0' && p <= '9'){
ret *= 10;
ret += p - '0';
}
else break;
}
return ret;
}
void predfs(int u, int p){
int ch = 0;
for(int v : edge[u]){
if(v == p) continue;
deg[u]++;
par[0][v] = u;
level[v] = level[u] + 1;
predfs(v, u);
ch++;
}
if(ch == 0) leaf.pb(u);
}
int lca(int u, int v){
if(level[u] > level[v]) swap(u, v);
for(int i = 19; i >= 0; i--) if(level[par[i][v]] >= level[u]) v = par[i][v];
for(int i = 19; i >= 0; i--) if(par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
if(u == v) return u;
return par[0][u];
}
int fset(int x){
if(pset[x] == x) return x;
return pset[x] = fset(pset[x]);
}
void unionset(int u, int v){
u = fset(u), v = fset(v);
if(u == v) return;
if(now[u] <= now[v]){
pset[u] = v;
sz[v] += sz[u];
sz[u] = 0;
now[v] += now[u];
now[u] = 0;
}
else{
pset[v] = u;
sz[u] += sz[v];
sz[v] = 0;
now[u] += now[v];
now[v] = 0;
}
}
void dfs(int u, int p){
int child = 0;
for(int v : g[u]){
if(v == p) continue;
child ++;
dfs(v, u);
}
if(child == 0 || (child == 1 && u == 1)) ans++;
}
int main(){
n = read(), k = read();
for(int i = 1; i < n; i++){
u = read(), v = read();
edge[u].pb(v);
edge[v].pb(u);
}
for(int i = 1; i <= n; i++){
s[i] = read();
tp[s[i]].pb(i);
}
par[0][1] = 1;
level[1] = 1;
predfs(1, 1);
for(int i = 1; i <= 19; i++){
for(int j = 1; j <= n; j++){
par[i][j] = par[i - 1][par[i - 1][j]];
}
}
for(int i = 1; i <= k; i++){
now[i] = 1;
pset[i] = i;
sz[i] = 1;
if(tp[i].empty()) continue;
int cur = tp[i][0];
for(int j = 1; j < (int)tp[i].size(); j++){
cur = lca(cur, tp[i][j]);
}
sub[cur]++;
}
queue <int> q;
for(int x : leaf) q.push(x);
while(!q.empty()){
int u = q.front();
q.pop();
int p = fset(s[u]);
sz[p] -= sub[u];
deg[par[0][u]]--;
if(!deg[par[0][u]]){
q.push(par[0][u]);
}
if(sz[p] == 0) continue;
unionset(p, s[par[0][u]]);
}
for(int i = 1; i <= k; i++){
if(pset[i] == i) ret[i] = ++cnt;
}
for(int i = 1; i <= k; i++){
if(pset[i] != i) ret[i] = ret[fset(i)];
}
for(int i = 1; i <= n; i++){
int id = ret[s[i]];
for(int j : edge[i]){
int jd = ret[s[j]];
if(id == jd) continue;
if(ed[1LL * id * N + jd] > 0) continue;
g[id].pb(jd);
g[jd].pb(id);
ed[1LL * id * N + jd]++;
ed[1LL * jd * N + id]++;
}
}
dfs(1, 1);
if(ans == 1) printf("0");
else printf("%d", (ans + 1) / 2);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
35832 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
38 ms |
35704 KB |
Output is correct |
4 |
Correct |
35 ms |
35832 KB |
Output is correct |
5 |
Correct |
37 ms |
35832 KB |
Output is correct |
6 |
Correct |
36 ms |
35812 KB |
Output is correct |
7 |
Correct |
36 ms |
35812 KB |
Output is correct |
8 |
Correct |
38 ms |
35764 KB |
Output is correct |
9 |
Correct |
39 ms |
35704 KB |
Output is correct |
10 |
Correct |
35 ms |
35832 KB |
Output is correct |
11 |
Correct |
37 ms |
35840 KB |
Output is correct |
12 |
Correct |
34 ms |
35832 KB |
Output is correct |
13 |
Correct |
43 ms |
35832 KB |
Output is correct |
14 |
Correct |
33 ms |
35832 KB |
Output is correct |
15 |
Correct |
34 ms |
35840 KB |
Output is correct |
16 |
Correct |
38 ms |
35908 KB |
Output is correct |
17 |
Correct |
35 ms |
35712 KB |
Output is correct |
18 |
Correct |
40 ms |
35772 KB |
Output is correct |
19 |
Correct |
39 ms |
35840 KB |
Output is correct |
20 |
Correct |
35 ms |
35704 KB |
Output is correct |
21 |
Correct |
39 ms |
35832 KB |
Output is correct |
22 |
Correct |
44 ms |
35832 KB |
Output is correct |
23 |
Correct |
36 ms |
35832 KB |
Output is correct |
24 |
Correct |
40 ms |
35832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
35832 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
38 ms |
35704 KB |
Output is correct |
4 |
Correct |
35 ms |
35832 KB |
Output is correct |
5 |
Correct |
37 ms |
35832 KB |
Output is correct |
6 |
Correct |
36 ms |
35812 KB |
Output is correct |
7 |
Correct |
36 ms |
35812 KB |
Output is correct |
8 |
Correct |
38 ms |
35764 KB |
Output is correct |
9 |
Correct |
39 ms |
35704 KB |
Output is correct |
10 |
Correct |
35 ms |
35832 KB |
Output is correct |
11 |
Correct |
37 ms |
35840 KB |
Output is correct |
12 |
Correct |
34 ms |
35832 KB |
Output is correct |
13 |
Correct |
43 ms |
35832 KB |
Output is correct |
14 |
Correct |
33 ms |
35832 KB |
Output is correct |
15 |
Correct |
34 ms |
35840 KB |
Output is correct |
16 |
Correct |
38 ms |
35908 KB |
Output is correct |
17 |
Correct |
35 ms |
35712 KB |
Output is correct |
18 |
Correct |
40 ms |
35772 KB |
Output is correct |
19 |
Correct |
39 ms |
35840 KB |
Output is correct |
20 |
Correct |
35 ms |
35704 KB |
Output is correct |
21 |
Correct |
39 ms |
35832 KB |
Output is correct |
22 |
Correct |
44 ms |
35832 KB |
Output is correct |
23 |
Correct |
36 ms |
35832 KB |
Output is correct |
24 |
Correct |
40 ms |
35832 KB |
Output is correct |
25 |
Correct |
38 ms |
35840 KB |
Output is correct |
26 |
Correct |
38 ms |
36472 KB |
Output is correct |
27 |
Correct |
38 ms |
36224 KB |
Output is correct |
28 |
Correct |
38 ms |
36600 KB |
Output is correct |
29 |
Correct |
46 ms |
36600 KB |
Output is correct |
30 |
Correct |
41 ms |
36216 KB |
Output is correct |
31 |
Correct |
34 ms |
35832 KB |
Output is correct |
32 |
Correct |
37 ms |
36600 KB |
Output is correct |
33 |
Correct |
38 ms |
35832 KB |
Output is correct |
34 |
Correct |
37 ms |
36216 KB |
Output is correct |
35 |
Correct |
40 ms |
36600 KB |
Output is correct |
36 |
Correct |
40 ms |
36216 KB |
Output is correct |
37 |
Correct |
43 ms |
36344 KB |
Output is correct |
38 |
Correct |
34 ms |
35832 KB |
Output is correct |
39 |
Correct |
37 ms |
36224 KB |
Output is correct |
40 |
Correct |
40 ms |
36216 KB |
Output is correct |
41 |
Correct |
37 ms |
36216 KB |
Output is correct |
42 |
Correct |
37 ms |
36352 KB |
Output is correct |
43 |
Correct |
39 ms |
36344 KB |
Output is correct |
44 |
Correct |
35 ms |
35832 KB |
Output is correct |
45 |
Correct |
38 ms |
36344 KB |
Output is correct |
46 |
Correct |
38 ms |
36344 KB |
Output is correct |
47 |
Correct |
36 ms |
35784 KB |
Output is correct |
48 |
Correct |
37 ms |
36216 KB |
Output is correct |
49 |
Correct |
39 ms |
36600 KB |
Output is correct |
50 |
Correct |
38 ms |
36728 KB |
Output is correct |
51 |
Correct |
40 ms |
36216 KB |
Output is correct |
52 |
Correct |
47 ms |
36216 KB |
Output is correct |
53 |
Correct |
40 ms |
36344 KB |
Output is correct |
54 |
Correct |
38 ms |
36216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
35832 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
38 ms |
35704 KB |
Output is correct |
4 |
Correct |
35 ms |
35832 KB |
Output is correct |
5 |
Correct |
37 ms |
35832 KB |
Output is correct |
6 |
Correct |
36 ms |
35812 KB |
Output is correct |
7 |
Correct |
36 ms |
35812 KB |
Output is correct |
8 |
Correct |
38 ms |
35764 KB |
Output is correct |
9 |
Correct |
39 ms |
35704 KB |
Output is correct |
10 |
Correct |
35 ms |
35832 KB |
Output is correct |
11 |
Correct |
37 ms |
35840 KB |
Output is correct |
12 |
Correct |
34 ms |
35832 KB |
Output is correct |
13 |
Correct |
43 ms |
35832 KB |
Output is correct |
14 |
Correct |
33 ms |
35832 KB |
Output is correct |
15 |
Correct |
34 ms |
35840 KB |
Output is correct |
16 |
Correct |
38 ms |
35908 KB |
Output is correct |
17 |
Correct |
35 ms |
35712 KB |
Output is correct |
18 |
Correct |
40 ms |
35772 KB |
Output is correct |
19 |
Correct |
39 ms |
35840 KB |
Output is correct |
20 |
Correct |
35 ms |
35704 KB |
Output is correct |
21 |
Correct |
39 ms |
35832 KB |
Output is correct |
22 |
Correct |
44 ms |
35832 KB |
Output is correct |
23 |
Correct |
36 ms |
35832 KB |
Output is correct |
24 |
Correct |
40 ms |
35832 KB |
Output is correct |
25 |
Correct |
37 ms |
35708 KB |
Output is correct |
26 |
Correct |
304 ms |
49312 KB |
Output is correct |
27 |
Correct |
307 ms |
49024 KB |
Output is correct |
28 |
Correct |
40 ms |
36216 KB |
Output is correct |
29 |
Correct |
34 ms |
35712 KB |
Output is correct |
30 |
Correct |
39 ms |
35840 KB |
Output is correct |
31 |
Correct |
317 ms |
49124 KB |
Output is correct |
32 |
Correct |
43 ms |
36216 KB |
Output is correct |
33 |
Correct |
170 ms |
54136 KB |
Output is correct |
34 |
Correct |
322 ms |
48956 KB |
Output is correct |
35 |
Correct |
38 ms |
36216 KB |
Output is correct |
36 |
Correct |
329 ms |
49272 KB |
Output is correct |
37 |
Correct |
37 ms |
36344 KB |
Output is correct |
38 |
Correct |
39 ms |
36224 KB |
Output is correct |
39 |
Correct |
322 ms |
49148 KB |
Output is correct |
40 |
Correct |
38 ms |
36344 KB |
Output is correct |
41 |
Correct |
132 ms |
49012 KB |
Output is correct |
42 |
Correct |
235 ms |
50372 KB |
Output is correct |
43 |
Correct |
33 ms |
35832 KB |
Output is correct |
44 |
Correct |
156 ms |
54484 KB |
Output is correct |
45 |
Correct |
127 ms |
51796 KB |
Output is correct |
46 |
Correct |
35 ms |
36216 KB |
Output is correct |
47 |
Correct |
39 ms |
36216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
49152 KB |
Output is correct |
2 |
Correct |
293 ms |
65028 KB |
Output is correct |
3 |
Correct |
40 ms |
36600 KB |
Output is correct |
4 |
Correct |
35 ms |
36224 KB |
Output is correct |
5 |
Correct |
38 ms |
35960 KB |
Output is correct |
6 |
Correct |
36 ms |
35832 KB |
Output is correct |
7 |
Correct |
36 ms |
36216 KB |
Output is correct |
8 |
Correct |
477 ms |
52212 KB |
Output is correct |
9 |
Correct |
40 ms |
36188 KB |
Output is correct |
10 |
Correct |
371 ms |
49376 KB |
Output is correct |
11 |
Correct |
34 ms |
35832 KB |
Output is correct |
12 |
Correct |
363 ms |
49200 KB |
Output is correct |
13 |
Correct |
500 ms |
53288 KB |
Output is correct |
14 |
Correct |
405 ms |
64568 KB |
Output is correct |
15 |
Correct |
311 ms |
49324 KB |
Output is correct |
16 |
Correct |
41 ms |
36472 KB |
Output is correct |
17 |
Correct |
33 ms |
35840 KB |
Output is correct |
18 |
Correct |
316 ms |
62536 KB |
Output is correct |
19 |
Correct |
388 ms |
68668 KB |
Output is correct |
20 |
Correct |
37 ms |
36472 KB |
Output is correct |
21 |
Correct |
32 ms |
35840 KB |
Output is correct |
22 |
Correct |
308 ms |
53376 KB |
Output is correct |
23 |
Correct |
39 ms |
36224 KB |
Output is correct |
24 |
Correct |
377 ms |
50552 KB |
Output is correct |
25 |
Correct |
457 ms |
66528 KB |
Output is correct |
26 |
Correct |
40 ms |
36728 KB |
Output is correct |
27 |
Correct |
42 ms |
36600 KB |
Output is correct |
28 |
Correct |
45 ms |
36224 KB |
Output is correct |
29 |
Correct |
36 ms |
36216 KB |
Output is correct |
30 |
Correct |
37 ms |
36352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
35832 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
38 ms |
35704 KB |
Output is correct |
4 |
Correct |
35 ms |
35832 KB |
Output is correct |
5 |
Correct |
37 ms |
35832 KB |
Output is correct |
6 |
Correct |
36 ms |
35812 KB |
Output is correct |
7 |
Correct |
36 ms |
35812 KB |
Output is correct |
8 |
Correct |
38 ms |
35764 KB |
Output is correct |
9 |
Correct |
39 ms |
35704 KB |
Output is correct |
10 |
Correct |
35 ms |
35832 KB |
Output is correct |
11 |
Correct |
37 ms |
35840 KB |
Output is correct |
12 |
Correct |
34 ms |
35832 KB |
Output is correct |
13 |
Correct |
43 ms |
35832 KB |
Output is correct |
14 |
Correct |
33 ms |
35832 KB |
Output is correct |
15 |
Correct |
34 ms |
35840 KB |
Output is correct |
16 |
Correct |
38 ms |
35908 KB |
Output is correct |
17 |
Correct |
35 ms |
35712 KB |
Output is correct |
18 |
Correct |
40 ms |
35772 KB |
Output is correct |
19 |
Correct |
39 ms |
35840 KB |
Output is correct |
20 |
Correct |
35 ms |
35704 KB |
Output is correct |
21 |
Correct |
39 ms |
35832 KB |
Output is correct |
22 |
Correct |
44 ms |
35832 KB |
Output is correct |
23 |
Correct |
36 ms |
35832 KB |
Output is correct |
24 |
Correct |
40 ms |
35832 KB |
Output is correct |
25 |
Correct |
38 ms |
35840 KB |
Output is correct |
26 |
Correct |
38 ms |
36472 KB |
Output is correct |
27 |
Correct |
38 ms |
36224 KB |
Output is correct |
28 |
Correct |
38 ms |
36600 KB |
Output is correct |
29 |
Correct |
46 ms |
36600 KB |
Output is correct |
30 |
Correct |
41 ms |
36216 KB |
Output is correct |
31 |
Correct |
34 ms |
35832 KB |
Output is correct |
32 |
Correct |
37 ms |
36600 KB |
Output is correct |
33 |
Correct |
38 ms |
35832 KB |
Output is correct |
34 |
Correct |
37 ms |
36216 KB |
Output is correct |
35 |
Correct |
40 ms |
36600 KB |
Output is correct |
36 |
Correct |
40 ms |
36216 KB |
Output is correct |
37 |
Correct |
43 ms |
36344 KB |
Output is correct |
38 |
Correct |
34 ms |
35832 KB |
Output is correct |
39 |
Correct |
37 ms |
36224 KB |
Output is correct |
40 |
Correct |
40 ms |
36216 KB |
Output is correct |
41 |
Correct |
37 ms |
36216 KB |
Output is correct |
42 |
Correct |
37 ms |
36352 KB |
Output is correct |
43 |
Correct |
39 ms |
36344 KB |
Output is correct |
44 |
Correct |
35 ms |
35832 KB |
Output is correct |
45 |
Correct |
38 ms |
36344 KB |
Output is correct |
46 |
Correct |
38 ms |
36344 KB |
Output is correct |
47 |
Correct |
36 ms |
35784 KB |
Output is correct |
48 |
Correct |
37 ms |
36216 KB |
Output is correct |
49 |
Correct |
39 ms |
36600 KB |
Output is correct |
50 |
Correct |
38 ms |
36728 KB |
Output is correct |
51 |
Correct |
40 ms |
36216 KB |
Output is correct |
52 |
Correct |
47 ms |
36216 KB |
Output is correct |
53 |
Correct |
40 ms |
36344 KB |
Output is correct |
54 |
Correct |
38 ms |
36216 KB |
Output is correct |
55 |
Correct |
37 ms |
35708 KB |
Output is correct |
56 |
Correct |
304 ms |
49312 KB |
Output is correct |
57 |
Correct |
307 ms |
49024 KB |
Output is correct |
58 |
Correct |
40 ms |
36216 KB |
Output is correct |
59 |
Correct |
34 ms |
35712 KB |
Output is correct |
60 |
Correct |
39 ms |
35840 KB |
Output is correct |
61 |
Correct |
317 ms |
49124 KB |
Output is correct |
62 |
Correct |
43 ms |
36216 KB |
Output is correct |
63 |
Correct |
170 ms |
54136 KB |
Output is correct |
64 |
Correct |
322 ms |
48956 KB |
Output is correct |
65 |
Correct |
38 ms |
36216 KB |
Output is correct |
66 |
Correct |
329 ms |
49272 KB |
Output is correct |
67 |
Correct |
37 ms |
36344 KB |
Output is correct |
68 |
Correct |
39 ms |
36224 KB |
Output is correct |
69 |
Correct |
322 ms |
49148 KB |
Output is correct |
70 |
Correct |
38 ms |
36344 KB |
Output is correct |
71 |
Correct |
132 ms |
49012 KB |
Output is correct |
72 |
Correct |
235 ms |
50372 KB |
Output is correct |
73 |
Correct |
33 ms |
35832 KB |
Output is correct |
74 |
Correct |
156 ms |
54484 KB |
Output is correct |
75 |
Correct |
127 ms |
51796 KB |
Output is correct |
76 |
Correct |
35 ms |
36216 KB |
Output is correct |
77 |
Correct |
39 ms |
36216 KB |
Output is correct |
78 |
Correct |
254 ms |
49152 KB |
Output is correct |
79 |
Correct |
293 ms |
65028 KB |
Output is correct |
80 |
Correct |
40 ms |
36600 KB |
Output is correct |
81 |
Correct |
35 ms |
36224 KB |
Output is correct |
82 |
Correct |
38 ms |
35960 KB |
Output is correct |
83 |
Correct |
36 ms |
35832 KB |
Output is correct |
84 |
Correct |
36 ms |
36216 KB |
Output is correct |
85 |
Correct |
477 ms |
52212 KB |
Output is correct |
86 |
Correct |
40 ms |
36188 KB |
Output is correct |
87 |
Correct |
371 ms |
49376 KB |
Output is correct |
88 |
Correct |
34 ms |
35832 KB |
Output is correct |
89 |
Correct |
363 ms |
49200 KB |
Output is correct |
90 |
Correct |
500 ms |
53288 KB |
Output is correct |
91 |
Correct |
405 ms |
64568 KB |
Output is correct |
92 |
Correct |
311 ms |
49324 KB |
Output is correct |
93 |
Correct |
41 ms |
36472 KB |
Output is correct |
94 |
Correct |
33 ms |
35840 KB |
Output is correct |
95 |
Correct |
316 ms |
62536 KB |
Output is correct |
96 |
Correct |
388 ms |
68668 KB |
Output is correct |
97 |
Correct |
37 ms |
36472 KB |
Output is correct |
98 |
Correct |
32 ms |
35840 KB |
Output is correct |
99 |
Correct |
308 ms |
53376 KB |
Output is correct |
100 |
Correct |
39 ms |
36224 KB |
Output is correct |
101 |
Correct |
377 ms |
50552 KB |
Output is correct |
102 |
Correct |
457 ms |
66528 KB |
Output is correct |
103 |
Correct |
40 ms |
36728 KB |
Output is correct |
104 |
Correct |
42 ms |
36600 KB |
Output is correct |
105 |
Correct |
45 ms |
36224 KB |
Output is correct |
106 |
Correct |
36 ms |
36216 KB |
Output is correct |
107 |
Correct |
37 ms |
36352 KB |
Output is correct |
108 |
Correct |
2220 ms |
115148 KB |
Output is correct |
109 |
Correct |
1334 ms |
113480 KB |
Output is correct |
110 |
Correct |
852 ms |
122212 KB |
Output is correct |
111 |
Correct |
2949 ms |
204780 KB |
Output is correct |
112 |
Correct |
1895 ms |
167120 KB |
Output is correct |
113 |
Correct |
2105 ms |
168048 KB |
Output is correct |
114 |
Correct |
711 ms |
100792 KB |
Output is correct |
115 |
Correct |
743 ms |
101092 KB |
Output is correct |
116 |
Correct |
2837 ms |
107496 KB |
Output is correct |
117 |
Execution timed out |
3003 ms |
188116 KB |
Time limit exceeded |
118 |
Halted |
0 ms |
0 KB |
- |