记录编号 577180 评测结果 AAAAWAAAAA
题目名称 盗取资料 最终得分 90
用户昵称 GravatarHeSn 是否通过 未通过
代码语言 C++ 运行时间 0.688 s
提交时间 2022-10-24 22:45:43 内存使用 6.66 MiB
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 100010;
int n, m, a[MAXN], ans, size[MAXN], dep[MAXN], rt, maxn, inc[MAXN];
vector<int> cd[MAXN];
int dfs(int x, int fa) {
	for(int i = 0; i < cd[x].size(); i ++) {
		int u = cd[x][i];
		if(u == fa) {
			continue;
		}
		dep[u] = dep[x] + 1;
		dfs(u, x);
	}
}
signed main() {
	freopen("dqzl.in", "r", stdin);
	freopen("dqzl.out", "w", stdout);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
	}
	for(int i = 1; i < n; i ++) {
		int x, y;
		cin >> x >> y;
		inc[x] ++;
		inc[y] ++;
		cd[x].push_back(y);
		cd[y].push_back(x);
	}
	for(int i = 1; i <= n; i ++) {
		if(inc[i] > maxn) {
			maxn = inc[i];
			rt = i;
		}
	}
	ans = 1e12;
	for(int i = 1; i <= n; i ++) {
		if(inc[i] == maxn) {
			dep[i] = 0;
			dfs(i, 0);
			int sum = 0;
			for(int i = 1; i <= n; i ++) {
				sum += a[i] * dep[i];
			}
			ans = min(ans, sum);
			break; 
		}
	}
	if(ans <= m) {
		cout << ans;
	}
	else {
		cout << "Poor Van";
	}
	return 0;
}