#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(u) u.begin(),u.end()
#define sz(u) (int)(u.size())
#define INF (int)(1e9)
using namespace std;
const int MAXN=2e5+5;
int n,k,ans=INF;
vector<vector<int>> tr(MAXN),grp(MAXN),cd(MAXN);
int col[MAXN],subsz[MAXN],parcd[MAXN],par[MAXN],dpth[MAXN],in[MAXN],dpthcd[MAXN];
bool blocked[MAXN];
vector<vector<int>> lca(2*MAXN,vector<int>(20));
int root=-1;
vector<int> eul;
void calc_sz(int u, int pr)
{
subsz[u]=1;
for(int v:tr[u])
{
if(v==pr || blocked[v]) continue;
calc_sz(v,u);
subsz[u]+=subsz[v];
}
}
int find_centroid(int u, int pr, int tar)
{
for(int v:tr[u])
{
if(v==pr || blocked[v]) continue;
if(subsz[v]>tar) return find_centroid(v,u,tar);
}
return u;
}
void decomp(int u, int pr, int d=0)
{
calc_sz(u,pr);
int centroid=find_centroid(u,pr,subsz[u]/2);
blocked[centroid]=1;
parcd[centroid]=pr;
dpthcd[centroid]=d;
if(pr==-1) root=centroid;
else
{
cd[centroid].pb(pr);
cd[pr].pb(centroid);
}
for(int v:tr[centroid])
{
if(blocked[v]) continue;
decomp(v,centroid,d+1);
}
}
void makeul(int u, int pr, int d)
{
in[u]=sz(eul);
eul.pb(u);
dpth[u]=d;
par[u]=pr;
d++;
for(int v:tr[u])
{
if(pr==v) continue;
makeul(v,u,d);
eul.pb(u);
}
}
int getlca(int u, int v)
{
int l=in[u], r=in[v];
if(l>r) swap(l,r);
int lg=log2(r-l+1);
if(dpth[lca[l][lg]]<=dpth[lca[r-(1<<lg)+1][lg]]) return lca[l][lg];
return lca[r-(1<<lg)+1][lg];
}
int lcacd(int u, int v)
{
if(dpthcd[u]==dpthcd[v])
{
if(u==v) return u;
else return lcacd(parcd[u],parcd[v]);
}
if(dpthcd[v]<dpthcd[u]) swap(u,v);
return lcacd(u,parcd[v]);
}
void solve(int rt, int pr)
{
unordered_map<int,int> visver;
unordered_map<int,int> rep;
int lc=rt, used=0;
queue<int> q;
q.push(rt);
visver[rt]=1;
rep[col[rt]]=rt;
bool bug=0;
while(!q.empty())
{
int u=q.front(); q.pop();
visver[u]=2;
if(u!=lc)
{
int mypar=par[u];
if(visver[mypar]==0 && col[mypar]!=col[u] && !rep.count(col[mypar]))
{
if(lcacd(mypar,rt)!=rt)
{
bug=1;
break;
}
q.push(mypar);
visver[mypar]=1;
rep[col[mypar]]=mypar;
}
}
if(rep[col[u]]!=u) continue;
used++;
for(int v:grp[col[u]])
{
if(v==u) continue;
int newlc=getlca(lc,v);
if(newlc!=lc)
{
if(visver[lc]==2)
{
if(lcacd(par[lc],rt)!=rt)
{
bug=1;
break;
}
if(!rep.count(col[par[lc]]))
{
q.push(par[lc]);
visver[par[lc]]=1;
rep[col[par[lc]]]=par[lc];
}
}
lc=newlc;
}
if(lcacd(v,rt)!=rt)
{
bug=1;
break;
}
visver[v]=1;
q.push(v);
}
if(bug) break;
}
if(!bug) ans=min(ans,used);
for(int v:cd[rt])
{
if(v==pr) continue;
solve(v,rt);
}
}
int main()
{
//freopen("test.in","r",stdin);
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>k;
for(int i=0;i<n-1;i++)
{
int u,v; cin>>u>>v;
u--,v--;
tr[u].pb(v);
tr[v].pb(u);
}
for(int i=0;i<n;i++)
{
cin>>col[i];
col[i]--;
grp[col[i]].pb(i);
}
decomp(0,-1);
makeul(0,-1,0);
for(int i=0;i<sz(eul);i++) lca[i][0]=eul[i];
for(int j=1;j<20;j++)
for(int i=0;i+(1<<j)<=sz(eul);i++)
lca[i][j]=(dpth[lca[i][j-1]]<=dpth[lca[i+(1<<(j-1))][j-1]]?lca[i][j-1]:lca[i+(1<<(j-1))][j-1]);
solve(root,-1);
cout<<ans-1<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
61432 KB |
Output is correct |
2 |
Correct |
58 ms |
61432 KB |
Output is correct |
3 |
Correct |
51 ms |
61432 KB |
Output is correct |
4 |
Correct |
51 ms |
61560 KB |
Output is correct |
5 |
Correct |
58 ms |
61432 KB |
Output is correct |
6 |
Correct |
56 ms |
61464 KB |
Output is correct |
7 |
Correct |
51 ms |
61432 KB |
Output is correct |
8 |
Correct |
57 ms |
61432 KB |
Output is correct |
9 |
Correct |
59 ms |
61432 KB |
Output is correct |
10 |
Correct |
51 ms |
61432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
61432 KB |
Output is correct |
2 |
Correct |
58 ms |
61432 KB |
Output is correct |
3 |
Correct |
51 ms |
61432 KB |
Output is correct |
4 |
Correct |
51 ms |
61560 KB |
Output is correct |
5 |
Correct |
58 ms |
61432 KB |
Output is correct |
6 |
Correct |
56 ms |
61464 KB |
Output is correct |
7 |
Correct |
51 ms |
61432 KB |
Output is correct |
8 |
Correct |
57 ms |
61432 KB |
Output is correct |
9 |
Correct |
59 ms |
61432 KB |
Output is correct |
10 |
Correct |
51 ms |
61432 KB |
Output is correct |
11 |
Correct |
53 ms |
61816 KB |
Output is correct |
12 |
Correct |
54 ms |
61816 KB |
Output is correct |
13 |
Correct |
62 ms |
61816 KB |
Output is correct |
14 |
Correct |
61 ms |
61816 KB |
Output is correct |
15 |
Correct |
56 ms |
61688 KB |
Output is correct |
16 |
Correct |
62 ms |
61688 KB |
Output is correct |
17 |
Correct |
59 ms |
61820 KB |
Output is correct |
18 |
Correct |
59 ms |
61816 KB |
Output is correct |
19 |
Correct |
53 ms |
61816 KB |
Output is correct |
20 |
Correct |
61 ms |
61816 KB |
Output is correct |
21 |
Correct |
56 ms |
61824 KB |
Output is correct |
22 |
Correct |
53 ms |
61816 KB |
Output is correct |
23 |
Correct |
56 ms |
61728 KB |
Output is correct |
24 |
Correct |
55 ms |
61816 KB |
Output is correct |
25 |
Correct |
72 ms |
61944 KB |
Output is correct |
26 |
Correct |
56 ms |
61944 KB |
Output is correct |
27 |
Correct |
61 ms |
61944 KB |
Output is correct |
28 |
Correct |
62 ms |
61816 KB |
Output is correct |
29 |
Correct |
55 ms |
61816 KB |
Output is correct |
30 |
Correct |
71 ms |
61820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
761 ms |
100732 KB |
Output is correct |
2 |
Correct |
451 ms |
101228 KB |
Output is correct |
3 |
Correct |
744 ms |
100592 KB |
Output is correct |
4 |
Correct |
438 ms |
101192 KB |
Output is correct |
5 |
Correct |
757 ms |
99436 KB |
Output is correct |
6 |
Correct |
440 ms |
101100 KB |
Output is correct |
7 |
Correct |
750 ms |
101508 KB |
Output is correct |
8 |
Correct |
453 ms |
106344 KB |
Output is correct |
9 |
Correct |
1102 ms |
106088 KB |
Output is correct |
10 |
Correct |
911 ms |
104168 KB |
Output is correct |
11 |
Correct |
999 ms |
106604 KB |
Output is correct |
12 |
Correct |
923 ms |
108648 KB |
Output is correct |
13 |
Correct |
987 ms |
103960 KB |
Output is correct |
14 |
Correct |
922 ms |
108776 KB |
Output is correct |
15 |
Correct |
932 ms |
108524 KB |
Output is correct |
16 |
Correct |
974 ms |
104544 KB |
Output is correct |
17 |
Correct |
934 ms |
105892 KB |
Output is correct |
18 |
Correct |
911 ms |
105068 KB |
Output is correct |
19 |
Correct |
926 ms |
107624 KB |
Output is correct |
20 |
Correct |
924 ms |
109416 KB |
Output is correct |
21 |
Correct |
51 ms |
61560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
61432 KB |
Output is correct |
2 |
Correct |
58 ms |
61432 KB |
Output is correct |
3 |
Correct |
51 ms |
61432 KB |
Output is correct |
4 |
Correct |
51 ms |
61560 KB |
Output is correct |
5 |
Correct |
58 ms |
61432 KB |
Output is correct |
6 |
Correct |
56 ms |
61464 KB |
Output is correct |
7 |
Correct |
51 ms |
61432 KB |
Output is correct |
8 |
Correct |
57 ms |
61432 KB |
Output is correct |
9 |
Correct |
59 ms |
61432 KB |
Output is correct |
10 |
Correct |
51 ms |
61432 KB |
Output is correct |
11 |
Correct |
53 ms |
61816 KB |
Output is correct |
12 |
Correct |
54 ms |
61816 KB |
Output is correct |
13 |
Correct |
62 ms |
61816 KB |
Output is correct |
14 |
Correct |
61 ms |
61816 KB |
Output is correct |
15 |
Correct |
56 ms |
61688 KB |
Output is correct |
16 |
Correct |
62 ms |
61688 KB |
Output is correct |
17 |
Correct |
59 ms |
61820 KB |
Output is correct |
18 |
Correct |
59 ms |
61816 KB |
Output is correct |
19 |
Correct |
53 ms |
61816 KB |
Output is correct |
20 |
Correct |
61 ms |
61816 KB |
Output is correct |
21 |
Correct |
56 ms |
61824 KB |
Output is correct |
22 |
Correct |
53 ms |
61816 KB |
Output is correct |
23 |
Correct |
56 ms |
61728 KB |
Output is correct |
24 |
Correct |
55 ms |
61816 KB |
Output is correct |
25 |
Correct |
72 ms |
61944 KB |
Output is correct |
26 |
Correct |
56 ms |
61944 KB |
Output is correct |
27 |
Correct |
61 ms |
61944 KB |
Output is correct |
28 |
Correct |
62 ms |
61816 KB |
Output is correct |
29 |
Correct |
55 ms |
61816 KB |
Output is correct |
30 |
Correct |
71 ms |
61820 KB |
Output is correct |
31 |
Correct |
761 ms |
100732 KB |
Output is correct |
32 |
Correct |
451 ms |
101228 KB |
Output is correct |
33 |
Correct |
744 ms |
100592 KB |
Output is correct |
34 |
Correct |
438 ms |
101192 KB |
Output is correct |
35 |
Correct |
757 ms |
99436 KB |
Output is correct |
36 |
Correct |
440 ms |
101100 KB |
Output is correct |
37 |
Correct |
750 ms |
101508 KB |
Output is correct |
38 |
Correct |
453 ms |
106344 KB |
Output is correct |
39 |
Correct |
1102 ms |
106088 KB |
Output is correct |
40 |
Correct |
911 ms |
104168 KB |
Output is correct |
41 |
Correct |
999 ms |
106604 KB |
Output is correct |
42 |
Correct |
923 ms |
108648 KB |
Output is correct |
43 |
Correct |
987 ms |
103960 KB |
Output is correct |
44 |
Correct |
922 ms |
108776 KB |
Output is correct |
45 |
Correct |
932 ms |
108524 KB |
Output is correct |
46 |
Correct |
974 ms |
104544 KB |
Output is correct |
47 |
Correct |
934 ms |
105892 KB |
Output is correct |
48 |
Correct |
911 ms |
105068 KB |
Output is correct |
49 |
Correct |
926 ms |
107624 KB |
Output is correct |
50 |
Correct |
924 ms |
109416 KB |
Output is correct |
51 |
Correct |
51 ms |
61560 KB |
Output is correct |
52 |
Correct |
783 ms |
95592 KB |
Output is correct |
53 |
Correct |
781 ms |
95464 KB |
Output is correct |
54 |
Correct |
785 ms |
95424 KB |
Output is correct |
55 |
Correct |
800 ms |
95544 KB |
Output is correct |
56 |
Correct |
775 ms |
95336 KB |
Output is correct |
57 |
Correct |
828 ms |
95544 KB |
Output is correct |
58 |
Correct |
809 ms |
95336 KB |
Output is correct |
59 |
Correct |
762 ms |
91448 KB |
Output is correct |
60 |
Correct |
756 ms |
90412 KB |
Output is correct |
61 |
Correct |
887 ms |
93052 KB |
Output is correct |
62 |
Correct |
444 ms |
101100 KB |
Output is correct |
63 |
Correct |
448 ms |
101100 KB |
Output is correct |
64 |
Correct |
449 ms |
103660 KB |
Output is correct |
65 |
Correct |
472 ms |
101224 KB |
Output is correct |
66 |
Correct |
719 ms |
98312 KB |
Output is correct |
67 |
Correct |
865 ms |
98148 KB |
Output is correct |
68 |
Correct |
663 ms |
98360 KB |
Output is correct |
69 |
Correct |
827 ms |
98408 KB |
Output is correct |
70 |
Correct |
664 ms |
98408 KB |
Output is correct |
71 |
Correct |
645 ms |
98232 KB |
Output is correct |
72 |
Correct |
634 ms |
98284 KB |
Output is correct |
73 |
Correct |
629 ms |
97628 KB |
Output is correct |
74 |
Correct |
651 ms |
98320 KB |
Output is correct |
75 |
Correct |
669 ms |
98384 KB |
Output is correct |
76 |
Correct |
1141 ms |
99520 KB |
Output is correct |
77 |
Correct |
1056 ms |
99308 KB |
Output is correct |
78 |
Correct |
1070 ms |
105100 KB |
Output is correct |
79 |
Correct |
1102 ms |
103404 KB |
Output is correct |
80 |
Correct |
943 ms |
109112 KB |
Output is correct |
81 |
Correct |
1068 ms |
107140 KB |
Output is correct |
82 |
Correct |
1002 ms |
106344 KB |
Output is correct |
83 |
Correct |
961 ms |
103788 KB |
Output is correct |
84 |
Correct |
955 ms |
108484 KB |
Output is correct |
85 |
Correct |
929 ms |
107116 KB |
Output is correct |
86 |
Correct |
946 ms |
103272 KB |
Output is correct |
87 |
Correct |
913 ms |
105612 KB |
Output is correct |
88 |
Correct |
859 ms |
103272 KB |
Output is correct |
89 |
Correct |
792 ms |
100172 KB |
Output is correct |
90 |
Correct |
773 ms |
99820 KB |
Output is correct |
91 |
Correct |
822 ms |
101992 KB |
Output is correct |
92 |
Correct |
791 ms |
100716 KB |
Output is correct |
93 |
Correct |
835 ms |
100584 KB |
Output is correct |
94 |
Correct |
851 ms |
100056 KB |
Output is correct |
95 |
Correct |
805 ms |
101256 KB |
Output is correct |
96 |
Correct |
823 ms |
100200 KB |
Output is correct |
97 |
Correct |
876 ms |
101804 KB |
Output is correct |