bfs噶了
查看原帖
bfs噶了
1633249
wmmyh楼主2025/1/16 08:28

赛时对了,现在噶了,无语。

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
int n, k, u, v, i;
int a[maxn], ans = 0;
vector<int> f[maxn];
queue<pair<int, int> > q;
bool vis[maxn];
void bfs(int u) {
	memset(vis, 0, sizeof(vis));
	ans = max(ans, 1);
	q.push(make_pair(u,a[u])); vis[u] = 1;
	while (!q.empty()) {
		u = q.front().first;
		int len = q.front().second;
		q.pop();
		for (int i=0; i < f[u].size(); i++) {
			v = f[u][i];
			if (!vis[v]) {
				if (len + a[v] > k) continue;
				ans = max(ans, len + a[v]);
				q.push(make_pair(v, len + a[v])); vis[v] = 1;

			}
		}
	}
}
int main() {
	cin >> n >> k;
	for(i = 1; i <= n; i++) cin>>a[i];
	for (i = 1; i < n; i++) {
		cin >> u >> v;
		f[u].push_back(v);
		f[v].push_back(u);
	}
	for (i = 1; i <= n; i++) bfs(i);
	cout << ans;
}
2025/1/16 08:28
加载中...