比赛 |
20191022轻松模拟测试 |
评测结果 |
WAWWAWWWWA |
题目名称 |
原谅 |
最终得分 |
30 |
用户昵称 |
BYVoid |
运行时间 |
0.706 s |
代码语言 |
C++ |
内存使用 |
46.36 MiB |
提交时间 |
2019-10-22 17:25:24 |
显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<cstring>
#define re register
#define maxn 2000005
using namespace std;
inline int read() {
int s=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=(s<<3)+(s<<1)+ch-'0';
ch=getchar();
}
return s*f;
}
int max(int x,int y){
return x>y?x:y;
}
int n,k,x,y,ans;
struct E{
int y,nxt;
}edge[maxn];
int head[maxn],tot;
void add(int x,int y){
edge[++tot].y=y;
edge[tot].nxt=head[x];
head[x]=tot;
}
int val[maxn];
int cnt,siz,num;
int tree[maxn];
bool vis[maxn],check[maxn];
void dfs(int x){
vis[x]=1;
siz++;
for(re int i=head[x];i;i=edge[i].nxt){
int y=edge[i].y;
if(vis[y])continue;
if(check[y])dfs(y);
}
}
int main() {
freopen("green.in","r",stdin);
freopen("green.out","w",stdout);
n=read();
k=read();
for(re int i=0;i<n;++i){
val[i]=read();
}
for(re int i=1;i<n;++i){
x=read();
y=read();
add(x,y);
add(y,x);
}
int l=0,r=10000000;
while(l<=r){
int mid=(l+r)>>1;
//printf("*%d *%d *%d\n",l,mid,r);
cnt=0;num=0;
memset(check,0,sizeof(check));
memset(vis,0,sizeof(vis));
for(re int i=0;i<n;++i){
if(val[i]>=mid){
tree[++cnt]=i;
check[i]=1;
}
}
for(re int i=1;i<=cnt;++i){
if(!vis[tree[i]]){
siz=0;
dfs(tree[i]);
num=max(num,siz);
// printf("siz=%d?\n",siz);
}
}
if(num<=k){
ans=num;
r=mid-1;
}
else l=mid+1;
}
printf("%d\n",ans);
return 0;
}
/*
*/