#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=2e5+6;
int n,k;
int c[MAXN];
vector<int>g[MAXN];
vector<int>nodes[MAXN];
int used3[MAXN],t;
bool cantuse[MAXN];
int ans;
bool used[MAXN];
bool used1[MAXN];
bool used2[MAXN];
int subtree[MAXN],par[MAXN];
void dfs(int u,int p,int t)
{
par[u]=p;
used3[u]=t;
subtree[u]=1;
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
dfs(xd,u,t);
subtree[u]+=subtree[xd];
}
}
int find_centroid(int u,int p,int n1)
{
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
if(subtree[xd]>n1/2)
{
return find_centroid(xd,u,n1);
}
}
return u;
}
void solve(int u)
{
t++;
dfs(u,0,t);
int cen=find_centroid(u,0,subtree[u]);
dfs(cen,0,t);
queue<int>q;
//cout<<"&& "<<u<<" "<<cen<<endl;
if(cantuse[c[cen]]==0)
{
bool imposibel=0;
for(auto xd:nodes[c[cen]])
{
q.push(xd);
if(used3[xd]!=t){imposibel=1;break;}
}
int cnt=1;
vector<int>vc;
vc.push_back(c[cen]);
used2[c[cen]]=1;cantuse[c[cen]]=1;
if(!imposibel)
while(!q.empty())
{
u=q.front();q.pop();
//cout<<u<<" ";
used1[u]=1;
if(par[u]==0)continue;
if(used1[par[u]]==0&&used2[c[par[u]]]==0&&cantuse[c[par[u]]])
{
imposibel=1;
break;
}
if(used2[c[par[u]]]==0)
{
//cout<<" *"<<c[par[u]]<<"* ";
used2[c[par[u]]]=1;vc.push_back(c[par[u]]);
cantuse[c[par[u]]]=1;
for(auto xd:nodes[c[par[u]]])
{
if(used3[xd]!=t){imposibel=1;break;}
q.push(xd);
}
cnt++;
}
}
//cout<<endl;
//cout<<imposibel<<" "<<cnt<<endl;
if(!imposibel)ans=min(ans,cnt);
for(auto xd:vc){used2[xd]=0;cantuse[xd]=0;}
}
used[cen]=1;
for(auto xd:g[cen])
{
if(used[xd]==0)
{
solve(xd);
}
}
}
int main()
{
cin>>n>>k;
ans=k;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
cin>>c[i];
nodes[c[i]].push_back(i);
}
solve(1);
cout<<ans-1<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9668 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
8 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9668 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
8 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9652 KB |
Output is correct |
11 |
Correct |
9 ms |
9844 KB |
Output is correct |
12 |
Correct |
9 ms |
9832 KB |
Output is correct |
13 |
Correct |
8 ms |
9804 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
9 ms |
9932 KB |
Output is correct |
16 |
Correct |
9 ms |
9932 KB |
Output is correct |
17 |
Correct |
10 ms |
9932 KB |
Output is correct |
18 |
Correct |
9 ms |
9932 KB |
Output is correct |
19 |
Correct |
8 ms |
9932 KB |
Output is correct |
20 |
Correct |
9 ms |
9932 KB |
Output is correct |
21 |
Correct |
9 ms |
9932 KB |
Output is correct |
22 |
Correct |
9 ms |
9964 KB |
Output is correct |
23 |
Correct |
11 ms |
9932 KB |
Output is correct |
24 |
Correct |
9 ms |
9932 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
9 ms |
9932 KB |
Output is correct |
27 |
Correct |
9 ms |
9968 KB |
Output is correct |
28 |
Correct |
10 ms |
9932 KB |
Output is correct |
29 |
Correct |
11 ms |
10044 KB |
Output is correct |
30 |
Correct |
9 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
776 ms |
38084 KB |
Output is correct |
2 |
Correct |
356 ms |
38596 KB |
Output is correct |
3 |
Correct |
749 ms |
37868 KB |
Output is correct |
4 |
Correct |
381 ms |
38632 KB |
Output is correct |
5 |
Correct |
791 ms |
35084 KB |
Output is correct |
6 |
Correct |
365 ms |
41924 KB |
Output is correct |
7 |
Correct |
785 ms |
38780 KB |
Output is correct |
8 |
Correct |
362 ms |
41848 KB |
Output is correct |
9 |
Correct |
842 ms |
37052 KB |
Output is correct |
10 |
Correct |
836 ms |
34668 KB |
Output is correct |
11 |
Correct |
826 ms |
37420 KB |
Output is correct |
12 |
Correct |
871 ms |
40060 KB |
Output is correct |
13 |
Correct |
843 ms |
34180 KB |
Output is correct |
14 |
Correct |
897 ms |
40488 KB |
Output is correct |
15 |
Correct |
841 ms |
40100 KB |
Output is correct |
16 |
Correct |
853 ms |
35116 KB |
Output is correct |
17 |
Correct |
864 ms |
35624 KB |
Output is correct |
18 |
Correct |
850 ms |
35948 KB |
Output is correct |
19 |
Correct |
941 ms |
38872 KB |
Output is correct |
20 |
Correct |
842 ms |
41328 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9668 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
7 ms |
9676 KB |
Output is correct |
9 |
Correct |
8 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9652 KB |
Output is correct |
11 |
Correct |
9 ms |
9844 KB |
Output is correct |
12 |
Correct |
9 ms |
9832 KB |
Output is correct |
13 |
Correct |
8 ms |
9804 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
9 ms |
9932 KB |
Output is correct |
16 |
Correct |
9 ms |
9932 KB |
Output is correct |
17 |
Correct |
10 ms |
9932 KB |
Output is correct |
18 |
Correct |
9 ms |
9932 KB |
Output is correct |
19 |
Correct |
8 ms |
9932 KB |
Output is correct |
20 |
Correct |
9 ms |
9932 KB |
Output is correct |
21 |
Correct |
9 ms |
9932 KB |
Output is correct |
22 |
Correct |
9 ms |
9964 KB |
Output is correct |
23 |
Correct |
11 ms |
9932 KB |
Output is correct |
24 |
Correct |
9 ms |
9932 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
9 ms |
9932 KB |
Output is correct |
27 |
Correct |
9 ms |
9968 KB |
Output is correct |
28 |
Correct |
10 ms |
9932 KB |
Output is correct |
29 |
Correct |
11 ms |
10044 KB |
Output is correct |
30 |
Correct |
9 ms |
9932 KB |
Output is correct |
31 |
Correct |
776 ms |
38084 KB |
Output is correct |
32 |
Correct |
356 ms |
38596 KB |
Output is correct |
33 |
Correct |
749 ms |
37868 KB |
Output is correct |
34 |
Correct |
381 ms |
38632 KB |
Output is correct |
35 |
Correct |
791 ms |
35084 KB |
Output is correct |
36 |
Correct |
365 ms |
41924 KB |
Output is correct |
37 |
Correct |
785 ms |
38780 KB |
Output is correct |
38 |
Correct |
362 ms |
41848 KB |
Output is correct |
39 |
Correct |
842 ms |
37052 KB |
Output is correct |
40 |
Correct |
836 ms |
34668 KB |
Output is correct |
41 |
Correct |
826 ms |
37420 KB |
Output is correct |
42 |
Correct |
871 ms |
40060 KB |
Output is correct |
43 |
Correct |
843 ms |
34180 KB |
Output is correct |
44 |
Correct |
897 ms |
40488 KB |
Output is correct |
45 |
Correct |
841 ms |
40100 KB |
Output is correct |
46 |
Correct |
853 ms |
35116 KB |
Output is correct |
47 |
Correct |
864 ms |
35624 KB |
Output is correct |
48 |
Correct |
850 ms |
35948 KB |
Output is correct |
49 |
Correct |
941 ms |
38872 KB |
Output is correct |
50 |
Correct |
842 ms |
41328 KB |
Output is correct |
51 |
Correct |
6 ms |
9676 KB |
Output is correct |
52 |
Correct |
637 ms |
25400 KB |
Output is correct |
53 |
Correct |
686 ms |
25404 KB |
Output is correct |
54 |
Correct |
660 ms |
25508 KB |
Output is correct |
55 |
Correct |
678 ms |
25428 KB |
Output is correct |
56 |
Correct |
674 ms |
25308 KB |
Output is correct |
57 |
Correct |
707 ms |
25332 KB |
Output is correct |
58 |
Correct |
687 ms |
28756 KB |
Output is correct |
59 |
Correct |
658 ms |
29048 KB |
Output is correct |
60 |
Correct |
761 ms |
28336 KB |
Output is correct |
61 |
Correct |
815 ms |
27944 KB |
Output is correct |
62 |
Correct |
364 ms |
41836 KB |
Output is correct |
63 |
Correct |
354 ms |
41920 KB |
Output is correct |
64 |
Correct |
367 ms |
41788 KB |
Output is correct |
65 |
Correct |
357 ms |
41924 KB |
Output is correct |
66 |
Correct |
545 ms |
33896 KB |
Output is correct |
67 |
Correct |
552 ms |
33724 KB |
Output is correct |
68 |
Correct |
532 ms |
33780 KB |
Output is correct |
69 |
Correct |
628 ms |
34012 KB |
Output is correct |
70 |
Correct |
549 ms |
33868 KB |
Output is correct |
71 |
Correct |
560 ms |
33804 KB |
Output is correct |
72 |
Correct |
545 ms |
33868 KB |
Output is correct |
73 |
Correct |
595 ms |
32996 KB |
Output is correct |
74 |
Correct |
545 ms |
33952 KB |
Output is correct |
75 |
Correct |
522 ms |
33824 KB |
Output is correct |
76 |
Correct |
809 ms |
32276 KB |
Output is correct |
77 |
Correct |
711 ms |
30636 KB |
Output is correct |
78 |
Correct |
832 ms |
35580 KB |
Output is correct |
79 |
Correct |
834 ms |
33564 KB |
Output is correct |
80 |
Correct |
857 ms |
40792 KB |
Output is correct |
81 |
Correct |
847 ms |
37248 KB |
Output is correct |
82 |
Correct |
875 ms |
37576 KB |
Output is correct |
83 |
Correct |
892 ms |
33960 KB |
Output is correct |
84 |
Correct |
877 ms |
39868 KB |
Output is correct |
85 |
Correct |
884 ms |
38312 KB |
Output is correct |
86 |
Correct |
824 ms |
33576 KB |
Output is correct |
87 |
Correct |
830 ms |
35524 KB |
Output is correct |
88 |
Correct |
733 ms |
36404 KB |
Output is correct |
89 |
Correct |
721 ms |
32560 KB |
Output is correct |
90 |
Correct |
755 ms |
32320 KB |
Output is correct |
91 |
Correct |
723 ms |
34796 KB |
Output is correct |
92 |
Correct |
762 ms |
33232 KB |
Output is correct |
93 |
Correct |
846 ms |
33220 KB |
Output is correct |
94 |
Correct |
752 ms |
32444 KB |
Output is correct |
95 |
Correct |
727 ms |
33788 KB |
Output is correct |
96 |
Correct |
735 ms |
32524 KB |
Output is correct |
97 |
Correct |
777 ms |
34588 KB |
Output is correct |