求助RE
查看原帖
求助RE
461359
huangrenheluogu楼主2023/10/20 09:54
#include<bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
int n, m, fir[N], nxt[N << 1], son[N << 1], tot, w[N << 1], x, y, z, l, r, mid, ans, maxn;
multiset<int>s[N];
inline void add(int x, int y, int z){
	nxt[++tot] = fir[x];
	fir[x] = tot;
	son[tot] = y;
	w[tot] = z;
}
inline int dfs(int x, int ff){
	s[x].clear();
	int res = 0;
	for(int i = fir[x]; i ; i = nxt[i]){
		if(son[i] == ff) continue ;
		int tem = dfs(son[i], x) + w[i];
		if(tem >= mid) tot++;
		else s[x].insert(tem);
	}
	while(!s[x].empty()){
		auto now = s[x].begin();
		auto now1 = s[x].lower_bound(mid - *now);
		if(now1 == s[x].end()){
			res = max(res, *now);
			s[x].erase(now);
		}
		else if(*now < *now1){
			tot++;
			s[x].erase(now);
			s[x].erase(now1);
		}
		else if(*now == *now1){
			if(s[x].count(*now1) > 1){
				tot++;
				s[x].erase(now);
				s[x].erase(now1);
			}
			else{
				now1++;
				if(now1 == s[x].end()){
					res = max(res, *now);
					s[x].erase(now);
				}
				else{
					s[x].erase(now1), s[x].erase(now);
					tot++;
				}
			}
		}
	}
	return res;
}
int main(){
//	freopen("P5021_5.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 1; i < n; i++){
		scanf("%d%d%d", &x, &y, &z);
		add(x, y, z), add(y, x, z);
//		maxn = max(maxn, z);
		r += z;
	}
	r /= m;
	while(l <= r){
		mid = l + r >> 1;
		tot = 0;
		dfs(1, 0);
		if(tot >= m) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	printf("%d", ans);
	return 0;
}
2023/10/20 09:54
加载中...