出题人居然是个哲学家。。
26%的程序,太SB了。。。本来我的想法也是二分+贪心,但是贪心是个怪怪的SX贪心。。
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<time.h>
#define inf 0x7fffffff
struct edge{
int u,v,id;
}e[];
int tot,go[],first[],next[];
int Tot,Go[],First[],Next[];
int c[],b[],son[],ans,vis[],pd;
int pass[],op[],C[],Cnt,block,sum,B[];
int id1,id2;
int n,k;
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void Dfs(int x,int fa){
if (vis[x]) return;
c[x]=b[x];son[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
if (pass[i]) continue;
Dfs(pur,x);
son[x]+=son[pur];
c[x]+=c[pur];
}
if (c[x]&&(n-c[x]>)) ans=std::min(ans,std::max(son[x],n-son[x]));
}
void sbpianfen(){
ans=inf;
Dfs(,);
printf("%d\n",ans);
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
pass[tot]=;
}
void add(int x,int y){
insert(x,y);op[tot]=tot+;insert(y,x);op[tot]=tot-;
}
void Insert(int x,int y){
Tot++;
Go[Tot]=y;
Next[Tot]=First[x];
First[x]=Tot;
}
void Add(int x,int y){
Insert(x,y);Insert(y,x);
}
int bfs(int x){
int h=,t=;
bool ck=;
c[]=x;vis[x]=;if (b[x]) ck=;
while (h<=t){
int now=c[h++];
for (int i=First[now];i;i=Next[i]){
int pur=Go[i];
if (vis[pur]) continue;
c[++t]=pur;
vis[pur]=;
if (b[pur]) ck=;
}
}
if (!ck) pd=;
return t;
}
void dFs(int x){
if (x==n){
pd=;
int bl=;
for (int i=;i<=n;i++)
First[i]=;
Tot=;
for (int i=;i<n;i++)
if (!e[i].id) Add(e[i].u,e[i].v);
for (int i=;i<=n;i++)
vis[i]=;
for (int i=;i<=n;i++)
if (!vis[i])
bl=std::max(bl,bfs(i));
if (pd==) ans=std::min(ans,bl);
return;
}
e[x].id=;
dFs(x+);
e[x].id=;
dFs(x+);
}
void sxpianfen(){
for (int i=;i<n;i++) e[i].id=;
dFs();
}
void clear(int x){
int h=,t=;
C[]=x;
bool ppp=;
if (b[x]) Cnt++,ppp=;
vis[x]=;b[x]=;
while (h<=t){
int now=C[h++];
for (int i=first[now];i;i=next[i]){
int pur=go[i];
if (pass[i]) continue;
if (vis[pur]) continue;
C[++t]=pur;
vis[pur]=;
if (b[pur]) Cnt++,ppp=;
b[pur]=;
}
}
if (ppp) {
pd=;
return;
}
if (t>block) pd=;
sum-=t;
}
void work(int x,int mid,int fa,int Edge){
if (c[x]==&&b[fa]==&&son[x]<=block) {
pass[Edge]=;
pass[op[Edge]]=;
clear(x);
id1=x;id2=fa;
if (pd) return;
return;
}
else
if (k-Cnt-c[x]==&&b[x]==&&sum-son[x]<=block){
pass[Edge]=;
pass[op[Edge]]=;
clear(fa);
id1=x;id2=fa;
if (pd) return;
return;
}
else
if (c[x]==&&son[x]<=block&&son[fa]>=block){
pass[Edge]=;
pass[op[Edge]]=;
clear(x);
id1=x;id2=fa;
if (pd) return;
return;
}else
if (k-Cnt-c[x]==&&sum-son[x]==block){
pass[Edge]=;
pass[op[Edge]]=;
clear(fa);
id1=x;id2=fa;
if (pd) return;
return;
}
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
if (vis[pur]) continue;
if (pass[i]) continue;
work(pur,mid,x,i);
}
}
bool check(int mid){
for (int i=;i<n;i++) std::swap(e[rand()%(n-)+],e[rand()%(n-)+]);
for (int i=;i<=n;i++) first[i]=;
tot=;
for (int i=;i<n;i++)
add(e[i].u,e[i].v);
block=mid;pd=;
for (int i=;i<=tot;i++)
pass[i]=;
for (int i=;i<=n;i++)
b[i]=B[i],vis[i]=;
id1=;id2=;vis[]=;
sum=n;Cnt=;
for (int i=;i<=k;i++){
if (vis[id1]&&vis[id2]) break;
if (k-Cnt==) break;
if (!vis[id1]){
Dfs(id1,);
work(id1,mid,,);
}else{
Dfs(id2,);
work(id2,mid,,);
}
if (pd) return ;
}
for (int i=;i<=n;i++)
if (!vis[i]) clear(i);
if (pd) return ;
return ;
}
void solve(){
int l=n/k,r=n,Ans=n;
while (l<=r){
int mid=(l+r)/;
if (check(mid)) r=mid-,Ans=mid;
else l=mid+;
}
printf("%d\n",Ans);
}
void solve2(){
int l=n/k,r=n,Ans=n;
while (l<=r){
int mid=(l+r)/;
if (check(mid)) r=mid-,Ans=mid;
else l=mid+;
}
int Tn=Ans;
l=n/k,r=n,Ans=n;
while (l<=r){
int mid=(l+r)/;
if (check(mid)) r=mid-,Ans=mid;
else l=mid+;
}
Tn=std::min(Tn,Ans);
printf("%d\n",Tn);
}
int main(){
//♂
freopen("deep.in","r",stdin);
freopen("deep.out","w",stdout);
srand(time(NULL));
ans=inf;
n=read();k=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
e[i].u=x;e[i].v=y;
}
for (int i=;i<=k;i++){
int x=read();
b[x]=;B[x]=;
}
if (n<=) {sxpianfen();printf("%d\n",ans);return ;}
if (k==) {printf("%d\n",n);return ;}
if (k==) {sbpianfen();return ;}
if (n<=) {solve();return ;}
if (k==) {solve2();return ;}
if (k==n) {printf("%d\n",);return ;}
if (k==n-) {printf("%d\n",);return ;}
solve();
}
AC程序不知道短到哪里去了
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define N 200005
int tot,go[N*],next[N*],first[N];
int n,k,b[N],fa[N],mx[N],sum[N],vis[N],c[N];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
void bfs(){
int h=,t=;for (int i=;i<=n;i++) vis[i]=;
c[]=;
vis[]=;
while (h<=t){
int now=c[h++];
for (int i=first[now];i;i=next[i]){
int pur=go[i];
if (vis[pur]) continue;
vis[pur]=;
c[++t]=pur;
fa[pur]=now;
}
}
}
bool check(int mid){
int f;
memset(sum,,sizeof sum);
memset(mx,,sizeof mx);
for (int i=n;i>=;i--){
int u=c[i];
if (b[u]) mx[u]=mid-;
else sum[u]++;
f=(mx[u]>=sum[u])?mx[u]-sum[u]:-sum[u];
if (u==&&f<) return ;
if (f<) sum[fa[u]]-=f;
else mx[fa[u]]=std::max(mx[fa[u]],f);
}
return ;
}
int main(){
n=read();k=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
for (int i=;i<=k;i++){
int x=read();
b[x]=;
}
bfs();
int l=n/k,r=n,ans;
while (l<=r){
int mid=(l+r)>>;
if (check(mid)) r=mid-,ans=mid;
else l=mid+;
}
printf("%d\n",ans);
}