#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)2e5 + 100;
vector<int> Z[N];
vector<int> T[N];
int id[N];
bool ban[N];
int par[N];
int subt[N];
bool now[N];
vector<int> mark;
void dfs(int u, int pr){
subt[u] = 1;
now[u]=true;
mark.push_back(u);
for(auto x : T[u]){
if(ban[x] || x == pr) continue;
dfs(x, u);
subt[u] += subt[x];
}
}
int up[N];
void dfs1(int u, int pr){
up[u] = pr;
for(auto x : T[u]){
if(ban[x] || x == pr) continue;
dfs1(x, u);
}
}
int fin(int u){
if(par[u]==u)
return u;
return par[u]=fin(par[u]);
}
int res;
void decomp(int node){
mark.clear();
dfs(node, -1);
int pr = -1;
bool is = true;
int sz = subt[node];
while(is){
is = false;
for(auto x : T[node]){
if(x == pr || ban[x]) continue;
if(subt[x] > sz/2){
pr = node;
node = x;
is = true;
break;
}
}
}
dfs1(node, node);
set<int> has, need;
need.insert(id[node]);
int cur = 0;
bool valid = true;
int hh;
while(!need.empty() && valid){
auto it = need.begin();
cur ++ ;
for(auto x : Z[(*it)]){
if(!now[x]){ // big optimization
valid=false;
break;
}
}
if(!valid) break;
set<int> piss;
for(auto x : Z[(*it)]){
hh = x;
while(hh != node){
hh=fin(hh);
if(!need.count(id[hh]) && !has.count(id[hh]) && !piss.count(id[hh])){
piss.insert(id[hh]);
}
par[hh] = up[hh];
}
}
has.insert(*it);
need.erase(it);
for(auto q : piss)
need.insert(q);
}
if(valid){
res = min(res, cur);
}
for(auto q : mark){
now[q]=false;
par[q]=q;
}
ban[node]=true;
for(auto x : T[node]){
if(!ban[x])
decomp(x);
}
}
int main(){
fastIO;
int n, k;
cin >> n >> k;
for(int i =1; i <= n; i ++ )
par[i]=i;
res = k;
int u, v;
for(int i = 1; i < n; i ++ ){
cin >> u >> v;
T[u].push_back(v);
T[v].push_back(u);
}
for(int i = 1; i <= n; i ++ ){
cin >> id[i];
Z[id[i]].push_back(i);
}
decomp(1);
cout << res - 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
14 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9804 KB |
Output is correct |
10 |
Correct |
13 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
14 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9804 KB |
Output is correct |
10 |
Correct |
13 ms |
9728 KB |
Output is correct |
11 |
Correct |
14 ms |
9856 KB |
Output is correct |
12 |
Correct |
16 ms |
9888 KB |
Output is correct |
13 |
Correct |
15 ms |
9980 KB |
Output is correct |
14 |
Correct |
9 ms |
9856 KB |
Output is correct |
15 |
Correct |
11 ms |
9984 KB |
Output is correct |
16 |
Correct |
11 ms |
9984 KB |
Output is correct |
17 |
Correct |
16 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
9984 KB |
Output is correct |
19 |
Correct |
9 ms |
9984 KB |
Output is correct |
20 |
Correct |
9 ms |
9984 KB |
Output is correct |
21 |
Correct |
18 ms |
9984 KB |
Output is correct |
22 |
Correct |
17 ms |
10112 KB |
Output is correct |
23 |
Correct |
18 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
10 ms |
9984 KB |
Output is correct |
26 |
Correct |
16 ms |
10112 KB |
Output is correct |
27 |
Correct |
10 ms |
9984 KB |
Output is correct |
28 |
Correct |
10 ms |
9984 KB |
Output is correct |
29 |
Correct |
19 ms |
9984 KB |
Output is correct |
30 |
Correct |
10 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
894 ms |
36440 KB |
Output is correct |
2 |
Correct |
291 ms |
39788 KB |
Output is correct |
3 |
Correct |
906 ms |
39028 KB |
Output is correct |
4 |
Correct |
286 ms |
39796 KB |
Output is correct |
5 |
Correct |
989 ms |
37428 KB |
Output is correct |
6 |
Correct |
287 ms |
39720 KB |
Output is correct |
7 |
Correct |
947 ms |
38448 KB |
Output is correct |
8 |
Correct |
293 ms |
40944 KB |
Output is correct |
9 |
Correct |
1151 ms |
38672 KB |
Output is correct |
10 |
Correct |
1121 ms |
36680 KB |
Output is correct |
11 |
Correct |
1147 ms |
39064 KB |
Output is correct |
12 |
Correct |
1146 ms |
41136 KB |
Output is correct |
13 |
Correct |
980 ms |
36400 KB |
Output is correct |
14 |
Correct |
857 ms |
41384 KB |
Output is correct |
15 |
Correct |
784 ms |
41068 KB |
Output is correct |
16 |
Correct |
931 ms |
37244 KB |
Output is correct |
17 |
Correct |
923 ms |
37608 KB |
Output is correct |
18 |
Correct |
959 ms |
37792 KB |
Output is correct |
19 |
Correct |
965 ms |
40208 KB |
Output is correct |
20 |
Correct |
1024 ms |
42224 KB |
Output is correct |
21 |
Correct |
8 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
9 ms |
9728 KB |
Output is correct |
3 |
Correct |
9 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
8 ms |
9728 KB |
Output is correct |
6 |
Correct |
8 ms |
9728 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
14 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9804 KB |
Output is correct |
10 |
Correct |
13 ms |
9728 KB |
Output is correct |
11 |
Correct |
14 ms |
9856 KB |
Output is correct |
12 |
Correct |
16 ms |
9888 KB |
Output is correct |
13 |
Correct |
15 ms |
9980 KB |
Output is correct |
14 |
Correct |
9 ms |
9856 KB |
Output is correct |
15 |
Correct |
11 ms |
9984 KB |
Output is correct |
16 |
Correct |
11 ms |
9984 KB |
Output is correct |
17 |
Correct |
16 ms |
9984 KB |
Output is correct |
18 |
Correct |
9 ms |
9984 KB |
Output is correct |
19 |
Correct |
9 ms |
9984 KB |
Output is correct |
20 |
Correct |
9 ms |
9984 KB |
Output is correct |
21 |
Correct |
18 ms |
9984 KB |
Output is correct |
22 |
Correct |
17 ms |
10112 KB |
Output is correct |
23 |
Correct |
18 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
10 ms |
9984 KB |
Output is correct |
26 |
Correct |
16 ms |
10112 KB |
Output is correct |
27 |
Correct |
10 ms |
9984 KB |
Output is correct |
28 |
Correct |
10 ms |
9984 KB |
Output is correct |
29 |
Correct |
19 ms |
9984 KB |
Output is correct |
30 |
Correct |
10 ms |
9984 KB |
Output is correct |
31 |
Correct |
894 ms |
36440 KB |
Output is correct |
32 |
Correct |
291 ms |
39788 KB |
Output is correct |
33 |
Correct |
906 ms |
39028 KB |
Output is correct |
34 |
Correct |
286 ms |
39796 KB |
Output is correct |
35 |
Correct |
989 ms |
37428 KB |
Output is correct |
36 |
Correct |
287 ms |
39720 KB |
Output is correct |
37 |
Correct |
947 ms |
38448 KB |
Output is correct |
38 |
Correct |
293 ms |
40944 KB |
Output is correct |
39 |
Correct |
1151 ms |
38672 KB |
Output is correct |
40 |
Correct |
1121 ms |
36680 KB |
Output is correct |
41 |
Correct |
1147 ms |
39064 KB |
Output is correct |
42 |
Correct |
1146 ms |
41136 KB |
Output is correct |
43 |
Correct |
980 ms |
36400 KB |
Output is correct |
44 |
Correct |
857 ms |
41384 KB |
Output is correct |
45 |
Correct |
784 ms |
41068 KB |
Output is correct |
46 |
Correct |
931 ms |
37244 KB |
Output is correct |
47 |
Correct |
923 ms |
37608 KB |
Output is correct |
48 |
Correct |
959 ms |
37792 KB |
Output is correct |
49 |
Correct |
965 ms |
40208 KB |
Output is correct |
50 |
Correct |
1024 ms |
42224 KB |
Output is correct |
51 |
Correct |
8 ms |
9728 KB |
Output is correct |
52 |
Correct |
650 ms |
26348 KB |
Output is correct |
53 |
Correct |
676 ms |
26348 KB |
Output is correct |
54 |
Correct |
675 ms |
26216 KB |
Output is correct |
55 |
Correct |
707 ms |
26460 KB |
Output is correct |
56 |
Correct |
666 ms |
26344 KB |
Output is correct |
57 |
Correct |
651 ms |
26216 KB |
Output is correct |
58 |
Correct |
714 ms |
30448 KB |
Output is correct |
59 |
Correct |
623 ms |
29548 KB |
Output is correct |
60 |
Correct |
719 ms |
28904 KB |
Output is correct |
61 |
Correct |
927 ms |
29160 KB |
Output is correct |
62 |
Correct |
259 ms |
39784 KB |
Output is correct |
63 |
Correct |
261 ms |
39796 KB |
Output is correct |
64 |
Correct |
277 ms |
41068 KB |
Output is correct |
65 |
Correct |
248 ms |
39912 KB |
Output is correct |
66 |
Correct |
443 ms |
32812 KB |
Output is correct |
67 |
Correct |
434 ms |
32844 KB |
Output is correct |
68 |
Correct |
451 ms |
32808 KB |
Output is correct |
69 |
Correct |
454 ms |
32844 KB |
Output is correct |
70 |
Correct |
461 ms |
32812 KB |
Output is correct |
71 |
Correct |
464 ms |
32808 KB |
Output is correct |
72 |
Correct |
483 ms |
32816 KB |
Output is correct |
73 |
Correct |
460 ms |
32128 KB |
Output is correct |
74 |
Correct |
394 ms |
32808 KB |
Output is correct |
75 |
Correct |
435 ms |
32944 KB |
Output is correct |
76 |
Correct |
877 ms |
34284 KB |
Output is correct |
77 |
Correct |
856 ms |
33260 KB |
Output is correct |
78 |
Correct |
780 ms |
37488 KB |
Output is correct |
79 |
Correct |
808 ms |
35948 KB |
Output is correct |
80 |
Correct |
870 ms |
41580 KB |
Output is correct |
81 |
Correct |
879 ms |
38768 KB |
Output is correct |
82 |
Correct |
989 ms |
39036 KB |
Output is correct |
83 |
Correct |
976 ms |
36460 KB |
Output is correct |
84 |
Correct |
953 ms |
41068 KB |
Output is correct |
85 |
Correct |
946 ms |
39868 KB |
Output is correct |
86 |
Correct |
840 ms |
36080 KB |
Output is correct |
87 |
Correct |
848 ms |
37316 KB |
Output is correct |
88 |
Correct |
712 ms |
36592 KB |
Output is correct |
89 |
Correct |
716 ms |
33520 KB |
Output is correct |
90 |
Correct |
684 ms |
33132 KB |
Output is correct |
91 |
Correct |
720 ms |
35088 KB |
Output is correct |
92 |
Correct |
781 ms |
34096 KB |
Output is correct |
93 |
Correct |
791 ms |
33648 KB |
Output is correct |
94 |
Correct |
762 ms |
33132 KB |
Output is correct |
95 |
Correct |
694 ms |
34408 KB |
Output is correct |
96 |
Correct |
718 ms |
33384 KB |
Output is correct |
97 |
Correct |
783 ms |
35052 KB |
Output is correct |