比赛 20191022轻松模拟测试 评测结果 WWWWWWWWWW
题目名称 原谅 最终得分 0
用户昵称 CoolBoy小逴 运行时间 0.178 s
代码语言 C++ 内存使用 59.44 MiB
提交时间 2019-10-22 17:31:53
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <ctime>
using namespace std;

const int maxn = 1e6 + 10;

int n, k, u, v, l, r, t;
int cnt, maxx, top, tot, mid, qwq, rht, ans, QaQ, tim;
int a[maxn], b[maxn], head[maxn], fa[maxn], son[maxn], s[maxn];

struct Edge{
	int u, v, next;
}e[maxn << 1];

struct node{
	int x, w;
	friend bool operator < (node a, node b){
		return a.w > b.w;
	}
};

queue<int>Q;

int f_;
char ch_;
template <class T>
	inline T read(T &x_){
		x_ = 0, f_ = 1, ch_ = getchar();
		while (!isdigit(ch_)) {if (ch_ == '-') f_ = -1; ch_ = getchar();}
		while (isdigit(ch_)){x_ = (x_ << 3) + (x_ << 1) + ch_ - 48; ch_ = getchar();}
		return x_ *= f_;
	}
	
inline int Max (int x, int y){
	return (x > y) ? x : y;
}

void add (int u, int v){
	e[++cnt].u = u;
	e[cnt].v = v;
	e[cnt].next = head[u];
	head[u] = cnt;
	return;
}

void dfs (int x, int f){
	fa[x] = f;
	for (register int i = head[x]; i ; i = e[i].next){
		if (e[i].v == f) continue;
		dfs (e[i].v, x);
		son[x] = Max(son[x], a[e[i].v]);
		son[x] = Max(son[x], son[e[i].v]);
	}
}

/*bool judge (int x){
	while (Q.size()) Q.pop();
	Q.push(s);
	tot = 0;
	b[++tot] = s;
	while (!Q.empty()){
		top = Q.front();
		Q.pop();
		qwq = 0;
		for (register int i = head[top]; i;i = e[i].next){
			if (e[i].v == fa[top]) continue;
			qwq = Max (qwq, son[e[i].v]);
		}
		cout << qwq << '\n';
		for (register int i = head[top]; i;i = e[i].next){
			if (e[i].v == fa[top]) continue;
			if (qwq > a[e[i].v]) continue;
			if (a[e[i].v] < x) continue;
			Q.push(e[i].v);
			b[++tot] = e[i].v;
		}
	}
	if (tot > k) return false;
//	cout << tot << '\n';
	QaQ = Max (QaQ, tot);
	return true;
}*/

void bfs (int x){
	Q.push(x);
	tot = 0;
	b[++tot] = x;
	while (!Q.empty()){
		top = Q.front();
		Q.pop();
		qwq = 0;
		for (register int i = head[top];i;i = e[i].next){
			if (e[i].v == fa[top]) continue;
			qwq = Max (qwq, son[e[i].v]);
		}
		for (register int i = head[top];i;i = e[i].next){
			if (e[i].v == fa[top]) continue;
			if (a[e[i].v] < qwq) continue;
			Q.push(e[i].v);
			tot++;
			b[tot] = e[i].v;
		}
	}
	/*for (register int i = 1;i <= tot; ++i) cout << b[i] << ' ';
	cout << '\n';*/
}

int main(){
	freopen ("green.in", "r", stdin);
	freopen ("green.out", "w", stdout);
	srand(time(0));
	read(n);
	read(k);
	for (register int i = 1;i <= n; ++i) {
		read(a[i]);
		if (maxx < a[i]) maxx = a[i], t = i;
	}
	for (register int i = 1;i < n; ++i) {
		read(u); read(v);		
		++u, ++v;
		add (u, v);
		add (v, u);
	}
	dfs (t, t);
	for (register int i = 1;i <= n; ++i){
		if (a[i] == maxx){
			bfs(i);
			if (tot <= k)
				ans = Max(ans, tot);
		}
	}
	printf ("%d\n", ans-1);
	/*l = 0; r = k;
	while (l <= r){
		mid = (l + r) >> 1;
		if (judge(mid)){
			ans = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	cout << ans << '\n';*/
//	++k;
//	printf ("%d\n", rand() % k);
	return 0;
}