目录
- 代码答疑
P7043 「MCOI-03」村国
- @ 2026-2-26 20:45:39
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int cap = 2e6 + 5;
int t, n;
ll m, a[cap];
int idx, h[cap], nxt[cap * 2], to[cap * 2];
void add(int u, int v) {
idx++;
to[idx] = v;
nxt[idx] = h[u];
h[u] = idx;
}
int com(int u, int v) {
ll dif = a[u] - a[v] + (u < v ? 1 : 0);
if (m < dif) {
return u;
}
ll rem = m - dif;
if (rem % 2 == 0) {
return v;
}
return u;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n >> m;
int u = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] > a[u] || (a[i] == a[u] && i < u)) {
u = i;
}
}
idx = 0;
for (int i = 1; i <= n; i++) {
h[i] = 0;
}
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
add(x, y);
add(y, x);
}
int v = 0;
for (int i = h[u]; i; i = nxt[i]) {
int w = to[i];
if (v == 0 || a[w] > a[v] || (a[w] == a[v] && w < v)) {
v = w;
}
}
if (v == 0) {
cout << u << '\n';
continue;
}
cout << com(u, v) << '\n';
}
return 0;
}
0 条评论
目前还没有评论...
京公网安备11010802045784号
YIZHIYANG 一只羊 LV 8