#include <bits/stdc++.h>
using namespace std;
const int mxn=1e2+5;
int n,k,mark[8],comp[8],par[mxn],s[mxn],ans=10,sum[mxn][8],sz[8],szz[8];
vector<int>v[mxn];
bool dfs(int z,int x){
bool res=1;
for(int j=1;j<=x;j++)
sum[z][j]=0;
sum[z][comp[s[z]]]++;
for(auto i:v[z]){
if(par[z]!=i){
par[i]=z;
res&=dfs(i,x);
for(int j=1;j<=x;j++)
sum[z][j]+=sum[i][j];
}
}
bool cnt=1;
for(int j=1;j<=x;j++){
if(sum[z][j]==szz[j] || sum[z][j]==0)cnt=1;
else{
cnt=0;
break;
}
}
if(cnt && z!=1)return 0;
return res;
}
void solve(int i,int x){
if(i>k){
for(int j=1;j<=x;j++){
if(!mark[j])return ;
}
if(dfs(1,x)){
ans=min(ans,k-x);
}
return ;
}
for(int j=1;j<=x;j++){
comp[i]=j;
mark[j]++;
szz[j]+=sz[i];
solve(i+1,x);
mark[j]--;
szz[j]-=sz[i];
}
}
int main(){
cin>>n>>k;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1;i<=n;i++){
cin>>s[i];
sz[s[i]]++;
}
for(int i=k;i>=1;i--){
solve(1,i);
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
360 KB |
Output is correct |
2 |
Correct |
9 ms |
356 KB |
Output is correct |
3 |
Correct |
7 ms |
360 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
78 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
7 ms |
356 KB |
Output is correct |
12 |
Correct |
69 ms |
424 KB |
Output is correct |
13 |
Correct |
16 ms |
352 KB |
Output is correct |
14 |
Correct |
69 ms |
352 KB |
Output is correct |
15 |
Correct |
8 ms |
352 KB |
Output is correct |
16 |
Correct |
15 ms |
356 KB |
Output is correct |
17 |
Correct |
15 ms |
608 KB |
Output is correct |
18 |
Correct |
101 ms |
424 KB |
Output is correct |
19 |
Correct |
15 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
552 KB |
Output is correct |
21 |
Correct |
95 ms |
420 KB |
Output is correct |
22 |
Correct |
63 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
23 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
360 KB |
Output is correct |
2 |
Correct |
9 ms |
356 KB |
Output is correct |
3 |
Correct |
7 ms |
360 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
78 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
7 ms |
356 KB |
Output is correct |
12 |
Correct |
69 ms |
424 KB |
Output is correct |
13 |
Correct |
16 ms |
352 KB |
Output is correct |
14 |
Correct |
69 ms |
352 KB |
Output is correct |
15 |
Correct |
8 ms |
352 KB |
Output is correct |
16 |
Correct |
15 ms |
356 KB |
Output is correct |
17 |
Correct |
15 ms |
608 KB |
Output is correct |
18 |
Correct |
101 ms |
424 KB |
Output is correct |
19 |
Correct |
15 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
552 KB |
Output is correct |
21 |
Correct |
95 ms |
420 KB |
Output is correct |
22 |
Correct |
63 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
23 ms |
440 KB |
Output is correct |
25 |
Runtime error |
72 ms |
1032 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
360 KB |
Output is correct |
2 |
Correct |
9 ms |
356 KB |
Output is correct |
3 |
Correct |
7 ms |
360 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
78 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
7 ms |
356 KB |
Output is correct |
12 |
Correct |
69 ms |
424 KB |
Output is correct |
13 |
Correct |
16 ms |
352 KB |
Output is correct |
14 |
Correct |
69 ms |
352 KB |
Output is correct |
15 |
Correct |
8 ms |
352 KB |
Output is correct |
16 |
Correct |
15 ms |
356 KB |
Output is correct |
17 |
Correct |
15 ms |
608 KB |
Output is correct |
18 |
Correct |
101 ms |
424 KB |
Output is correct |
19 |
Correct |
15 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
552 KB |
Output is correct |
21 |
Correct |
95 ms |
420 KB |
Output is correct |
22 |
Correct |
63 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
23 ms |
440 KB |
Output is correct |
25 |
Runtime error |
71 ms |
1104 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
360 KB |
Output is correct |
2 |
Correct |
9 ms |
356 KB |
Output is correct |
3 |
Correct |
7 ms |
360 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
78 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
7 ms |
356 KB |
Output is correct |
12 |
Correct |
69 ms |
424 KB |
Output is correct |
13 |
Correct |
16 ms |
352 KB |
Output is correct |
14 |
Correct |
69 ms |
352 KB |
Output is correct |
15 |
Correct |
8 ms |
352 KB |
Output is correct |
16 |
Correct |
15 ms |
356 KB |
Output is correct |
17 |
Correct |
15 ms |
608 KB |
Output is correct |
18 |
Correct |
101 ms |
424 KB |
Output is correct |
19 |
Correct |
15 ms |
348 KB |
Output is correct |
20 |
Correct |
14 ms |
552 KB |
Output is correct |
21 |
Correct |
95 ms |
420 KB |
Output is correct |
22 |
Correct |
63 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
23 ms |
440 KB |
Output is correct |
25 |
Runtime error |
72 ms |
1032 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |