#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=2e5+9;
vector<int>a[maxn];
int sub[maxn];
int st[maxn][18];
int dep[maxn];
int col[maxn];
int c[maxn];
vector<int>ver[maxn];
void dfs(int u,int par){
sub[u]=1;
for (auto &v:a[u]){
if (v==par)continue;
st[v][0]=u;
for1(i,1,17)st[v][i]=st[st[v][i-1]][i-1];
dep[v]=dep[u]+1;
dfs(v,u);
sub[u]+=sub[v];
if (a[u][0]==par||sub[a[u][0]]<sub[v]){
swap(v,a[u][0]);
}
}
}
int tme=0;
int h[maxn],pos[maxn],rev[maxn];
void hld(int u,int par,int head){
h[u]=head;
pos[u]=++tme;
rev[pos[u]]=u;
if (a[u][0]!=par){
hld(a[u][0],u,head);
}
for (auto v:a[u]){
if (v==par||v==a[u][0])continue;
hld(v,u,v);
}
}
int lca(int u,int v){
if (u==0)return v;
if (v==0)return u;
if (dep[u]<dep[v])swap(u,v);
int k=dep[u]-dep[v];
for1(i,0,17){
if (k>>i&1)u=st[u][i];
}
if (u==v)return u;
for2(i,17,0){
if (!st[u][i]||!st[v][i])continue;
if (st[u][i]!=st[v][i]){
u=st[u][i];
v=st[v][i];
}
}
return st[u][0];
}
int f[maxn];
int findset(int u){
if (f[u]<0)return u;
return f[u]=findset(f[u]);
}
void unite(int u,int v){
u=findset(u),v=findset(v);
if (u==v)return ;
if (f[u]>f[v])swap(u,v);
f[u]+=f[v];
col[u]=lca(col[u],col[v]);
for (auto v1:ver[v]){
ver[u].pb(v1);
}
vector<int>().swap(ver[v]);
f[v]=u;
}
int num[maxn],low[maxn],tim=0;
int n,k;
stack<int>scc;
struct ITgetmin{
vector<int>st;
void resz(int _n){
st.resize(4*_n+9);
}
void update(int id,int l,int r,int u,int val){
if (l>u||r<u)return ;
if (l==r){
st[id]=val;
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,u,val);
update(id*2+1,mid+1,r,u,val);
st[id]=min(st[id*2],st[id*2+1]);
}
int get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return n+1;
if (u<=l&&r<=v)return st[id];
int mid=(l+r)/2;
return min(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
};
pii combine(const pii &p, const pii &q){
if (p.se>q.se)return p;
else return q;
}
struct IT{
vector<pii>st;
void resz(int _n){
st.resize(4*_n+9);
}
void update(int id,int l,int r,int u,int val){
if (l>u||r<u)return ;
if (l==r){
st[id]={l,val};
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,u,val);
update(id*2+1,mid+1,r,u,val);
st[id]=combine(st[id*2],st[id*2+1]);
}
pii get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return {0,0};
if (u<=l&&r<=v)return st[id];
int mid=(l+r)/2;
return combine(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
};
ITgetmin st1;
IT st2;
void dfs(int u){
num[u]=low[u]=++tim;
scc.push(u);
//cerr<<"DFS "<<u<<'\n';
for (auto v:ver[u]){
st1.update(1,1,n,pos[v],num[u]);
st2.update(1,1,n,pos[v],num[u]);
}
for (auto v:ver[u]){
int v1=v;
while (h[v1]!=h[col[u]]){
int l=pos[h[v1]],r=pos[v1];
while (true){
pii tmp=st2.get(1,1,n,l,r);
if (tmp.se!=n+1)break;
dfs(c[rev[tmp.fi]]);
}
v1=st[h[v1]][0];
}
int l=pos[col[u]],r=pos[v1];
if (l>r)swap(l,r);
while (true){
pii tmp=st2.get(1,1,n,l,r);
if (tmp.se!=n+1)break;
dfs(c[rev[tmp.fi]]);
}
}
for (auto v:ver[u]){
int v1=v;
while (h[v1]!=h[col[u]]){
int l=pos[h[v1]],r=pos[v1];
low[u]=min(low[u],st1.get(1,1,n,l,r));
v1=st[h[v1]][0];
}
int l=pos[col[u]],r=pos[v1];
if (l>r)swap(l,r);
low[u]=min(low[u],st1.get(1,1,n,l,r));
}
if (num[u]==low[u]){
while (scc.top()!=u){
int v=scc.top();
scc.pop();
unite(u,v);
}
scc.pop();
for (auto v:ver[findset(u)]){
st1.update(1,1,n,pos[v],n+1);
}
}
else {
for (auto v:ver[u]){
st1.update(1,1,n,pos[v],low[u]);
}
}
}
int bit[maxn];
void add(int pos1,int val){
for(;pos1<=n;pos1+=(pos1-(pos1&(pos1-1))))bit[pos1]+=val;
}
int get(int pos1){
int sum=0;
for(;pos1>=1;pos1-=(pos1-(pos1&(pos1-1))))sum+=bit[pos1];
return sum;
}
int get(int l,int r){
if (l>r)return 0;
return get(r)-get(l-1);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>k;
if (n==1){
cout<<0;
return 0;
}
for1(i,1,n-1){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
dfs(1,0);
hld(1,0,1);
for1(i,1,n){
cin>>c[i];
ver[c[i]].pb(i);
col[c[i]]=lca(col[c[i]],i);
}
st1.resz(n);
for1(i,1,n){
st1.update(1,1,n,i,n+1);
}
st2.resz(n);
for1(i,1,n){
st2.update(1,1,n,i,n+1);
}
for1(i,1,k)f[i]=-1;
for1(i,1,k){
if (!num[i]){
dfs(i);
}
}
int ans=k-1;
for1(i,1,n)add(i,1);
for1(i,1,k){
if (f[i]<0){
for (auto v:ver[i]){
add(pos[v],-1);
}
int sum=0;
for (auto v:ver[i]){
int u=v;
while(h[u]!=h[col[i]]){
int l=pos[h[u]],r=pos[u];
sum+=get(l,r);
u=st[h[u]][0];
}
int l=pos[col[i]],r=pos[u];
if (l>r)swap(l,r);
sum+=get(l,r);
}
if (sum==0){
ans=min(ans,abs(f[i])-1);
}
for (auto v:ver[i]){
add(pos[v],1);
}
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9740 KB |
Output is correct |
3 |
Correct |
4 ms |
9688 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9740 KB |
Output is correct |
3 |
Correct |
4 ms |
9688 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
10196 KB |
Output is correct |
12 |
Correct |
8 ms |
10196 KB |
Output is correct |
13 |
Correct |
10 ms |
10244 KB |
Output is correct |
14 |
Correct |
8 ms |
10212 KB |
Output is correct |
15 |
Correct |
7 ms |
10196 KB |
Output is correct |
16 |
Correct |
8 ms |
10196 KB |
Output is correct |
17 |
Correct |
7 ms |
10196 KB |
Output is correct |
18 |
Correct |
9 ms |
10196 KB |
Output is correct |
19 |
Correct |
8 ms |
10196 KB |
Output is correct |
20 |
Correct |
7 ms |
10196 KB |
Output is correct |
21 |
Correct |
7 ms |
10196 KB |
Output is correct |
22 |
Correct |
7 ms |
10324 KB |
Output is correct |
23 |
Correct |
7 ms |
10324 KB |
Output is correct |
24 |
Correct |
7 ms |
10196 KB |
Output is correct |
25 |
Correct |
8 ms |
10312 KB |
Output is correct |
26 |
Correct |
7 ms |
10324 KB |
Output is correct |
27 |
Correct |
7 ms |
10300 KB |
Output is correct |
28 |
Correct |
8 ms |
10196 KB |
Output is correct |
29 |
Correct |
7 ms |
10256 KB |
Output is correct |
30 |
Correct |
8 ms |
10264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
511 ms |
64972 KB |
Output is correct |
2 |
Correct |
285 ms |
65228 KB |
Output is correct |
3 |
Correct |
496 ms |
64548 KB |
Output is correct |
4 |
Correct |
304 ms |
65224 KB |
Output is correct |
5 |
Correct |
525 ms |
61908 KB |
Output is correct |
6 |
Correct |
284 ms |
65392 KB |
Output is correct |
7 |
Correct |
471 ms |
61672 KB |
Output is correct |
8 |
Correct |
278 ms |
64668 KB |
Output is correct |
9 |
Correct |
473 ms |
59988 KB |
Output is correct |
10 |
Correct |
497 ms |
57988 KB |
Output is correct |
11 |
Correct |
474 ms |
60376 KB |
Output is correct |
12 |
Correct |
489 ms |
63308 KB |
Output is correct |
13 |
Correct |
507 ms |
57252 KB |
Output is correct |
14 |
Correct |
503 ms |
62840 KB |
Output is correct |
15 |
Correct |
472 ms |
63132 KB |
Output is correct |
16 |
Correct |
490 ms |
57892 KB |
Output is correct |
17 |
Correct |
501 ms |
58552 KB |
Output is correct |
18 |
Correct |
501 ms |
58860 KB |
Output is correct |
19 |
Correct |
468 ms |
61984 KB |
Output is correct |
20 |
Correct |
487 ms |
64216 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9740 KB |
Output is correct |
3 |
Correct |
4 ms |
9688 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
10196 KB |
Output is correct |
12 |
Correct |
8 ms |
10196 KB |
Output is correct |
13 |
Correct |
10 ms |
10244 KB |
Output is correct |
14 |
Correct |
8 ms |
10212 KB |
Output is correct |
15 |
Correct |
7 ms |
10196 KB |
Output is correct |
16 |
Correct |
8 ms |
10196 KB |
Output is correct |
17 |
Correct |
7 ms |
10196 KB |
Output is correct |
18 |
Correct |
9 ms |
10196 KB |
Output is correct |
19 |
Correct |
8 ms |
10196 KB |
Output is correct |
20 |
Correct |
7 ms |
10196 KB |
Output is correct |
21 |
Correct |
7 ms |
10196 KB |
Output is correct |
22 |
Correct |
7 ms |
10324 KB |
Output is correct |
23 |
Correct |
7 ms |
10324 KB |
Output is correct |
24 |
Correct |
7 ms |
10196 KB |
Output is correct |
25 |
Correct |
8 ms |
10312 KB |
Output is correct |
26 |
Correct |
7 ms |
10324 KB |
Output is correct |
27 |
Correct |
7 ms |
10300 KB |
Output is correct |
28 |
Correct |
8 ms |
10196 KB |
Output is correct |
29 |
Correct |
7 ms |
10256 KB |
Output is correct |
30 |
Correct |
8 ms |
10264 KB |
Output is correct |
31 |
Correct |
511 ms |
64972 KB |
Output is correct |
32 |
Correct |
285 ms |
65228 KB |
Output is correct |
33 |
Correct |
496 ms |
64548 KB |
Output is correct |
34 |
Correct |
304 ms |
65224 KB |
Output is correct |
35 |
Correct |
525 ms |
61908 KB |
Output is correct |
36 |
Correct |
284 ms |
65392 KB |
Output is correct |
37 |
Correct |
471 ms |
61672 KB |
Output is correct |
38 |
Correct |
278 ms |
64668 KB |
Output is correct |
39 |
Correct |
473 ms |
59988 KB |
Output is correct |
40 |
Correct |
497 ms |
57988 KB |
Output is correct |
41 |
Correct |
474 ms |
60376 KB |
Output is correct |
42 |
Correct |
489 ms |
63308 KB |
Output is correct |
43 |
Correct |
507 ms |
57252 KB |
Output is correct |
44 |
Correct |
503 ms |
62840 KB |
Output is correct |
45 |
Correct |
472 ms |
63132 KB |
Output is correct |
46 |
Correct |
490 ms |
57892 KB |
Output is correct |
47 |
Correct |
501 ms |
58552 KB |
Output is correct |
48 |
Correct |
501 ms |
58860 KB |
Output is correct |
49 |
Correct |
468 ms |
61984 KB |
Output is correct |
50 |
Correct |
487 ms |
64216 KB |
Output is correct |
51 |
Correct |
5 ms |
9684 KB |
Output is correct |
52 |
Correct |
777 ms |
55428 KB |
Output is correct |
53 |
Correct |
753 ms |
55232 KB |
Output is correct |
54 |
Correct |
764 ms |
55312 KB |
Output is correct |
55 |
Correct |
784 ms |
55288 KB |
Output is correct |
56 |
Correct |
746 ms |
55168 KB |
Output is correct |
57 |
Correct |
765 ms |
55260 KB |
Output is correct |
58 |
Correct |
569 ms |
55680 KB |
Output is correct |
59 |
Correct |
522 ms |
55808 KB |
Output is correct |
60 |
Correct |
648 ms |
55244 KB |
Output is correct |
61 |
Correct |
601 ms |
54948 KB |
Output is correct |
62 |
Correct |
290 ms |
68988 KB |
Output is correct |
63 |
Correct |
290 ms |
68984 KB |
Output is correct |
64 |
Correct |
277 ms |
68592 KB |
Output is correct |
65 |
Correct |
316 ms |
68976 KB |
Output is correct |
66 |
Correct |
443 ms |
59700 KB |
Output is correct |
67 |
Correct |
454 ms |
59512 KB |
Output is correct |
68 |
Correct |
455 ms |
59664 KB |
Output is correct |
69 |
Correct |
441 ms |
59712 KB |
Output is correct |
70 |
Correct |
476 ms |
59588 KB |
Output is correct |
71 |
Correct |
485 ms |
59616 KB |
Output is correct |
72 |
Correct |
520 ms |
59620 KB |
Output is correct |
73 |
Correct |
441 ms |
59024 KB |
Output is correct |
74 |
Correct |
441 ms |
59644 KB |
Output is correct |
75 |
Correct |
463 ms |
59720 KB |
Output is correct |
76 |
Correct |
486 ms |
60480 KB |
Output is correct |
77 |
Correct |
487 ms |
60772 KB |
Output is correct |
78 |
Correct |
489 ms |
62640 KB |
Output is correct |
79 |
Correct |
474 ms |
60412 KB |
Output is correct |
80 |
Correct |
482 ms |
67256 KB |
Output is correct |
81 |
Correct |
472 ms |
64256 KB |
Output is correct |
82 |
Correct |
566 ms |
64396 KB |
Output is correct |
83 |
Correct |
472 ms |
60908 KB |
Output is correct |
84 |
Correct |
492 ms |
66724 KB |
Output is correct |
85 |
Correct |
515 ms |
64728 KB |
Output is correct |
86 |
Correct |
499 ms |
60344 KB |
Output is correct |
87 |
Correct |
490 ms |
62184 KB |
Output is correct |
88 |
Correct |
523 ms |
63020 KB |
Output is correct |
89 |
Correct |
504 ms |
59268 KB |
Output is correct |
90 |
Correct |
522 ms |
58380 KB |
Output is correct |
91 |
Correct |
563 ms |
61256 KB |
Output is correct |
92 |
Correct |
526 ms |
59572 KB |
Output is correct |
93 |
Correct |
513 ms |
59820 KB |
Output is correct |
94 |
Correct |
549 ms |
59148 KB |
Output is correct |
95 |
Correct |
525 ms |
60700 KB |
Output is correct |
96 |
Correct |
557 ms |
59260 KB |
Output is correct |
97 |
Correct |
510 ms |
61208 KB |
Output is correct |