WA15pts求调
查看原帖
WA15pts求调
600877
__Raincoat__楼主2025/8/4 08:29

15pts

#include<bits/stdc++.h>
#define int long long
#define ri register int
using namespace std;
const int maxn = 5e4+3;
int u,v,w,n,m,l,r,cnt,mid;
bool vis[maxn],flag; 
struct edge{
	int v,w;
};
vector<edge> g[maxn];
multiset<int>c[maxn];
void get_tot(int x,int k){
	vis[x] = 1;
	for(auto p:g[x]){
		if(!vis[p.v]){
			if(p.w>=k)cnt++;
			else c[x].insert(p.w);
			
			get_tot(p.v,k);
			
			if(!c[p.v].empty()){
				register auto cend = --c[p.v].end();
				c[x].insert(*cend + p.w);
				c[x].erase(p.w);
				c[p.v].erase(cend);
			}
		}
	}
	register auto lb = c[x].lower_bound(k - *c[x].begin());
	while(c[x].size()>=2 && lb != c[x].end()){
		cnt++;
		c[x].erase(lb);
		c[x].erase(c[x].begin());
		lb = c[x].lower_bound(k - *c[x].begin());
	}
	lb = c[x].lower_bound(k);
	while(lb != c[x].end()){
		c[x].erase(lb);
		cnt++;
		lb = c[x].lower_bound(k);
	}
} 
bool check(int k){
	cnt = 0;
	for(int i=1;i<=n;i++){
		vis[i] = 0;
		c[i].clear();
	}
	get_tot(1,k);
	return cnt >= m;
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n-1;i++){
		cin>>u>>v>>w;
		g[u].push_back({v,w});
		g[v].push_back({u,w});
		r += w; 
	}
	while(l < r - 1){
		mid = (l+r)>>1;
		if(check(mid))l = mid;性 
		else r = mid-1;
	}
	cout<<mid<<'\n';
	return 0;
}

(轻微压行) 疑似二分有误?

2025/8/4 08:29
加载中...