#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,k,nr[200005],par[200005],dp[21][200005],in[200005],out[200005],niv[200005];
bool use[200005],isheavy[200005];
vector<int> mynodes[200005];
int cul[200005],poz[200005],where[200005],last[200005],chains,lg[200005],timp;
int nrcomp,nrcul[200005],nrnodes[200005],nrmuchii[200005],comp[200005];
vector<vector<int>> arb;
vector<vector<vector<int>>> aint;
vector<int> muchii[200005];
vector<pii> init;
vector<int> st;
vector<int> who;
bool isancestor(int a,int b)
{
return in[a]<=in[b]&&out[a]>=out[b];
}
int LCA(int a,int b)
{
if(niv[a]>niv[b])
swap(a,b);
if(isancestor(a,b))
return a;
for(int i=18;i>=0;i--)
if(dp[i][a]!=0&&!isancestor(dp[i][a],b))
a=dp[i][a];
return par[a];
}
void initdfs(int nod)
{
timp++;
in[nod]=timp;
nr[nod]=1;
dp[0][nod]=par[nod];
for(int i=1;i<=18;i++)
dp[i][nod]=dp[i-1][dp[i-1][nod]];
int who=0,maxim=0;
for(int i:muchii[nod])
if(i!=par[nod])
{
par[i]=nod;
niv[i]=niv[nod]+1;
initdfs(i);
nr[nod]+=nr[i];
if(nr[i]>maxim)
{
maxim=nr[i];
who=i;
}
}
if(who!=0)
isheavy[who]=1;
out[nod]=timp;
}
void buildheavy()
{
for(int i=1;i<=n;i++)
{
int nod=i;
bool ok=1;
for(int j:muchii[nod])
if(j!=par[nod]&&isheavy[j])
{
ok=0;
break;
}
if(!ok)
continue;
chains++;
while(nod!=0)
{
lg[chains]++;
poz[nod]=lg[chains];
where[nod]=chains;
last[chains]=nod;
if(!isheavy[nod])
break;
nod=par[nod];
}
}
arb.resize(chains+1);
aint.resize(chains+1);
for(int i=1;i<=chains;i++)
{
arb[i].resize(4*lg[i]+5);
aint[i].resize(4*lg[i]+5);
}
}
void update(int ind,int nod,int st,int dr,int p,int val)
{
if(st==dr)
{
arb[ind][nod]=val;
return;
}
int mij=(st+dr)/2;
if(p<=mij)
update(ind,nod*2,st,mij,p,val);
else
update(ind,nod*2+1,mij+1,dr,p,val);
arb[ind][nod]=max(arb[ind][nod*2],arb[ind][nod*2+1]);
}
int query(int ind,int nod,int st,int dr,int a,int b)
{
if(st>=a&&dr<=b)
return arb[ind][nod];
int rez=-1;
int mij=(st+dr)/2;
if(a<=mij)
rez=max(rez,query(ind,nod*2,st,mij,a,b));
if(b>mij)
rez=max(rez,query(ind,nod*2+1,mij+1,dr,a,b));
return rez;
}
void plsput(int nod,int val)
{
int ind=where[nod];
update(ind,1,1,lg[ind],poz[nod],val);
}
int chmin(int a,int lca)
{
int rez=-1;
while(niv[a]>=niv[lca])
{
int ind=where[a];
int p=poz[a];
if(where[lca]==ind)
{
int val=query(ind,1,1,lg[ind],p,poz[lca]);
rez=max(rez,val);
break;
}
int val=query(ind,1,1,lg[ind],p,lg[ind]);
rez=max(rez,val);
a=par[last[ind]];
}
return rez;
}
int getmin(int a,int b)
{
int lca=LCA(a,b);
return max(chmin(a,lca),chmin(b,lca));
}
void upd(int ind,int nod,int st,int dr,int a,int b,int val)
{
if(st>=a&&dr<=b)
{
aint[ind][nod].push_back(val);
return;
}
int mij=(st+dr)/2;
if(a<=mij)
upd(ind,nod*2,st,mij,a,b,val);
if(b>mij)
upd(ind,nod*2+1,mij+1,dr,a,b,val);
}
void getnodes(int ind,int nod,int st,int dr,int p)
{
for(int i:aint[ind][nod])
who.push_back(i);
aint[ind][nod].clear();
if(st==dr)
return;
int mij=(st+dr)/2;
if(p<=mij)
getnodes(ind,nod*2,st,mij,p);
else
getnodes(ind,nod*2+1,mij+1,dr,p);
}
void add(int a,int lca,int val)
{
while(niv[a]>=niv[lca])
{
int ind=where[a];
int p=poz[a];
if(where[lca]==ind)
{
upd(ind,1,1,lg[ind],p,poz[lca],val);
break;
}
upd(ind,1,1,lg[ind],p,lg[ind],val);
a=par[last[ind]];
}
}
void plsadd(int a,int b,int val)
{
int lca=LCA(a,b);
add(a,lca,val);
add(b,lca,val);
}
void dfs1(int nod)
{
use[nod]=1;
for(int x:mynodes[nod])
plsput(x,-1);
for(int i=1;i<mynodes[nod].size();i++)
{
int a=mynodes[nod][0];
int b=mynodes[nod][i];
int x=getmin(a,b);
while(x>=1)
{
dfs1(x);
x=getmin(a,b);
}
}
st.push_back(nod);
}
void dfs2(int nod)
{
use[nod]=1;
comp[nod]=nrcomp;
nrcul[nrcomp]++;
for(int i:mynodes[nod])
{
who.clear();
vector<int> aux;
int ind=where[i];
int p=poz[i];
getnodes(ind,1,1,lg[ind],p);
aux=who;
for(int j:aux)
if(!use[j])
dfs2(j);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
muchii[a].push_back(b);
muchii[b].push_back(a);
init.push_back({a,b});
}
for(int i=1;i<=n;i++)
{
cin>>cul[i];
mynodes[cul[i]].push_back(i);
}
niv[1]=1;
initdfs(1);
buildheavy();
for(int i=1;i<=n;i++)
plsput(i,cul[i]);
for(int i=1;i<=k;i++)
if(!use[i])
dfs1(i);
reverse(st.begin(),st.end());
for(int i=1;i<=n;i++)
use[i]=0;
for(int i=1;i<=k;i++)
{
for(int j=1;j<mynodes[i].size();j++)
{
int a=mynodes[i][0];
int b=mynodes[i][j];
plsadd(a,b,i);
}
}
for(int i:st)
if(!use[i])
{
nrcomp++;
dfs2(i);
}
/*for(int i=1;i<=k;i++)
cout<<comp[i]<<' ';
cout<<'\n';*/
for(int i=1;i<=n;i++)
{
int c=comp[cul[i]];
nrnodes[c]++;
}
for(pii p:init)
{
int a=p.first;
int b=p.second;
a=comp[cul[a]];
b=comp[cul[b]];
if(a==b)
nrmuchii[a]++;
}
int ans=1e9;
for(int i=1;i<=nrcomp;i++)
if(nrmuchii[i]+1==nrnodes[i])
ans=min(ans,nrcul[i]);
cout<<ans-1;
return 0;
}
Compilation message
capital_city.cpp: In function 'void dfs1(int)':
capital_city.cpp:198:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
198 | for(int i=1;i<mynodes[nod].size();i++)
| ~^~~~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:260:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
260 | for(int j=1;j<mynodes[i].size();j++)
| ~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31320 KB |
Output is correct |
2 |
Correct |
5 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31372 KB |
Output is correct |
6 |
Correct |
5 ms |
31372 KB |
Output is correct |
7 |
Correct |
5 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31340 KB |
Output is correct |
10 |
Correct |
5 ms |
31320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31320 KB |
Output is correct |
2 |
Correct |
5 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31372 KB |
Output is correct |
6 |
Correct |
5 ms |
31372 KB |
Output is correct |
7 |
Correct |
5 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31340 KB |
Output is correct |
10 |
Correct |
5 ms |
31320 KB |
Output is correct |
11 |
Correct |
9 ms |
32092 KB |
Output is correct |
12 |
Correct |
7 ms |
32092 KB |
Output is correct |
13 |
Correct |
7 ms |
32160 KB |
Output is correct |
14 |
Correct |
7 ms |
32092 KB |
Output is correct |
15 |
Correct |
8 ms |
32152 KB |
Output is correct |
16 |
Correct |
8 ms |
32088 KB |
Output is correct |
17 |
Correct |
7 ms |
32092 KB |
Output is correct |
18 |
Correct |
7 ms |
32092 KB |
Output is correct |
19 |
Correct |
7 ms |
32092 KB |
Output is correct |
20 |
Correct |
7 ms |
32092 KB |
Output is correct |
21 |
Correct |
7 ms |
32092 KB |
Output is correct |
22 |
Correct |
7 ms |
32068 KB |
Output is correct |
23 |
Correct |
8 ms |
31836 KB |
Output is correct |
24 |
Correct |
7 ms |
32088 KB |
Output is correct |
25 |
Correct |
8 ms |
31836 KB |
Output is correct |
26 |
Correct |
8 ms |
32020 KB |
Output is correct |
27 |
Correct |
8 ms |
32088 KB |
Output is correct |
28 |
Correct |
8 ms |
32288 KB |
Output is correct |
29 |
Correct |
8 ms |
32088 KB |
Output is correct |
30 |
Correct |
8 ms |
32088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
587 ms |
104228 KB |
Output is correct |
2 |
Correct |
259 ms |
105376 KB |
Output is correct |
3 |
Correct |
559 ms |
104448 KB |
Output is correct |
4 |
Correct |
263 ms |
105408 KB |
Output is correct |
5 |
Correct |
571 ms |
102076 KB |
Output is correct |
6 |
Correct |
266 ms |
105408 KB |
Output is correct |
7 |
Correct |
528 ms |
102528 KB |
Output is correct |
8 |
Correct |
288 ms |
104752 KB |
Output is correct |
9 |
Correct |
515 ms |
98772 KB |
Output is correct |
10 |
Correct |
521 ms |
96188 KB |
Output is correct |
11 |
Correct |
504 ms |
99640 KB |
Output is correct |
12 |
Correct |
529 ms |
103352 KB |
Output is correct |
13 |
Correct |
528 ms |
96108 KB |
Output is correct |
14 |
Correct |
594 ms |
103988 KB |
Output is correct |
15 |
Correct |
532 ms |
103384 KB |
Output is correct |
16 |
Correct |
540 ms |
97516 KB |
Output is correct |
17 |
Correct |
518 ms |
97672 KB |
Output is correct |
18 |
Correct |
504 ms |
98192 KB |
Output is correct |
19 |
Correct |
571 ms |
102664 KB |
Output is correct |
20 |
Correct |
503 ms |
104236 KB |
Output is correct |
21 |
Correct |
6 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31320 KB |
Output is correct |
2 |
Correct |
5 ms |
31324 KB |
Output is correct |
3 |
Correct |
6 ms |
31324 KB |
Output is correct |
4 |
Correct |
6 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31372 KB |
Output is correct |
6 |
Correct |
5 ms |
31372 KB |
Output is correct |
7 |
Correct |
5 ms |
31320 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31340 KB |
Output is correct |
10 |
Correct |
5 ms |
31320 KB |
Output is correct |
11 |
Correct |
9 ms |
32092 KB |
Output is correct |
12 |
Correct |
7 ms |
32092 KB |
Output is correct |
13 |
Correct |
7 ms |
32160 KB |
Output is correct |
14 |
Correct |
7 ms |
32092 KB |
Output is correct |
15 |
Correct |
8 ms |
32152 KB |
Output is correct |
16 |
Correct |
8 ms |
32088 KB |
Output is correct |
17 |
Correct |
7 ms |
32092 KB |
Output is correct |
18 |
Correct |
7 ms |
32092 KB |
Output is correct |
19 |
Correct |
7 ms |
32092 KB |
Output is correct |
20 |
Correct |
7 ms |
32092 KB |
Output is correct |
21 |
Correct |
7 ms |
32092 KB |
Output is correct |
22 |
Correct |
7 ms |
32068 KB |
Output is correct |
23 |
Correct |
8 ms |
31836 KB |
Output is correct |
24 |
Correct |
7 ms |
32088 KB |
Output is correct |
25 |
Correct |
8 ms |
31836 KB |
Output is correct |
26 |
Correct |
8 ms |
32020 KB |
Output is correct |
27 |
Correct |
8 ms |
32088 KB |
Output is correct |
28 |
Correct |
8 ms |
32288 KB |
Output is correct |
29 |
Correct |
8 ms |
32088 KB |
Output is correct |
30 |
Correct |
8 ms |
32088 KB |
Output is correct |
31 |
Correct |
587 ms |
104228 KB |
Output is correct |
32 |
Correct |
259 ms |
105376 KB |
Output is correct |
33 |
Correct |
559 ms |
104448 KB |
Output is correct |
34 |
Correct |
263 ms |
105408 KB |
Output is correct |
35 |
Correct |
571 ms |
102076 KB |
Output is correct |
36 |
Correct |
266 ms |
105408 KB |
Output is correct |
37 |
Correct |
528 ms |
102528 KB |
Output is correct |
38 |
Correct |
288 ms |
104752 KB |
Output is correct |
39 |
Correct |
515 ms |
98772 KB |
Output is correct |
40 |
Correct |
521 ms |
96188 KB |
Output is correct |
41 |
Correct |
504 ms |
99640 KB |
Output is correct |
42 |
Correct |
529 ms |
103352 KB |
Output is correct |
43 |
Correct |
528 ms |
96108 KB |
Output is correct |
44 |
Correct |
594 ms |
103988 KB |
Output is correct |
45 |
Correct |
532 ms |
103384 KB |
Output is correct |
46 |
Correct |
540 ms |
97516 KB |
Output is correct |
47 |
Correct |
518 ms |
97672 KB |
Output is correct |
48 |
Correct |
504 ms |
98192 KB |
Output is correct |
49 |
Correct |
571 ms |
102664 KB |
Output is correct |
50 |
Correct |
503 ms |
104236 KB |
Output is correct |
51 |
Correct |
6 ms |
31324 KB |
Output is correct |
52 |
Correct |
713 ms |
116400 KB |
Output is correct |
53 |
Correct |
702 ms |
116548 KB |
Output is correct |
54 |
Correct |
725 ms |
117116 KB |
Output is correct |
55 |
Correct |
788 ms |
116460 KB |
Output is correct |
56 |
Correct |
741 ms |
116104 KB |
Output is correct |
57 |
Correct |
750 ms |
116440 KB |
Output is correct |
58 |
Correct |
908 ms |
109064 KB |
Output is correct |
59 |
Correct |
941 ms |
108692 KB |
Output is correct |
60 |
Correct |
871 ms |
114368 KB |
Output is correct |
61 |
Correct |
937 ms |
115028 KB |
Output is correct |
62 |
Correct |
264 ms |
105648 KB |
Output is correct |
63 |
Correct |
258 ms |
105468 KB |
Output is correct |
64 |
Correct |
257 ms |
104904 KB |
Output is correct |
65 |
Correct |
287 ms |
105364 KB |
Output is correct |
66 |
Correct |
483 ms |
120256 KB |
Output is correct |
67 |
Correct |
471 ms |
115696 KB |
Output is correct |
68 |
Correct |
508 ms |
118972 KB |
Output is correct |
69 |
Correct |
574 ms |
118184 KB |
Output is correct |
70 |
Correct |
530 ms |
119488 KB |
Output is correct |
71 |
Correct |
504 ms |
118728 KB |
Output is correct |
72 |
Correct |
491 ms |
119544 KB |
Output is correct |
73 |
Correct |
520 ms |
116624 KB |
Output is correct |
74 |
Correct |
519 ms |
120404 KB |
Output is correct |
75 |
Correct |
549 ms |
119232 KB |
Output is correct |
76 |
Correct |
642 ms |
107168 KB |
Output is correct |
77 |
Correct |
960 ms |
104916 KB |
Output is correct |
78 |
Correct |
522 ms |
97660 KB |
Output is correct |
79 |
Correct |
546 ms |
95316 KB |
Output is correct |
80 |
Correct |
499 ms |
104352 KB |
Output is correct |
81 |
Correct |
590 ms |
100284 KB |
Output is correct |
82 |
Correct |
600 ms |
100432 KB |
Output is correct |
83 |
Correct |
497 ms |
95940 KB |
Output is correct |
84 |
Correct |
550 ms |
102992 KB |
Output is correct |
85 |
Correct |
589 ms |
100816 KB |
Output is correct |
86 |
Correct |
498 ms |
96104 KB |
Output is correct |
87 |
Correct |
498 ms |
96648 KB |
Output is correct |
88 |
Correct |
704 ms |
110000 KB |
Output is correct |
89 |
Correct |
648 ms |
104916 KB |
Output is correct |
90 |
Correct |
665 ms |
104096 KB |
Output is correct |
91 |
Correct |
687 ms |
107400 KB |
Output is correct |
92 |
Correct |
702 ms |
105916 KB |
Output is correct |
93 |
Correct |
662 ms |
105792 KB |
Output is correct |
94 |
Correct |
674 ms |
105144 KB |
Output is correct |
95 |
Correct |
715 ms |
106668 KB |
Output is correct |
96 |
Correct |
674 ms |
105868 KB |
Output is correct |
97 |
Correct |
816 ms |
109228 KB |
Output is correct |