zoukankan      html  css  js  c++  java
  • 赛道修建

    原版复制大佬的题解

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #define LL long long 
    #define maxn 50010
    #define mid ((l+r)>>1)
    using namespace std;
    struct edge{
    	int node,next,val;
    	edge(int a=0,int b=0,int c=0)
    	{ node=a, next=b,val=c; }
    }l[maxn<<1];
    int head[maxn],cnt,n,m;
    vector<int> son[maxn];
    LL f[maxn];
    int subans[maxn];
    void add(int a,int b,int c)
    {
    	l[++cnt]=edge(b,head[a],c);
    	head[a]=cnt;
    }
    int check(int u,int pos,int tot,LL x)
    {
    	int res=0,l=0;
    	for(register int r=tot-1;r;--r)
    	{
    		r-=r==pos;
    		while(l<r&&son[u][l]+son[u][r]<x) ++l;
    		l+=l==pos;//不能是pos这条链  
    		if(l>=r) break;
    		++res; ++l;
    	}
    	return res;
    }
    void dfs(int u,int fa,LL x)
    {
    	f[u]=subans[u]=0;
    	son[u].clear();//vector 从0开始存 
    	for(register int i=head[u];i;i=l[i].next)
    	{
    		int v=l[i].node;
    		if(v==fa) continue;
    		dfs(v,u,x);
    		f[v]+=l[i].val;
    		if(f[v]>=x) subans[u]++;
    		else son[u].push_back(f[v]);
    	}
    	int tot=son[u].size();
    	sort(son[u].begin(),son[u].end());
    	int l=0,r=tot,sub=0,res;
    	for(register int r=tot-1;r;--r)
    	{//两两组合所能组成的最多的答案数 
    		while(l<r&&son[u][l]+son[u][r]<x) ++l;
    		if(l>=r) break;
    		++sub; ++l;
    	}
    	subans[u]+=sub;//两两组合所能组成的最多的答案数  
    	if(sub*2==tot) return ;
    	l=0,r=tot-1;//在不能组成答案的半链中找最大的一条向上继承  
    	while(l<=r)
    	{
    		int tem=check(u,mid,tot,x);//判断如果选mid这条链,能不能构成sub条符合答案的赛道  
    		if(tem==sub) res=mid,l=mid+1;
    		else r=mid-1;
    	}//其余的废掉了  
    	f[u]=son[u][res];
    }
    bool check(LL x)
    {
    	int tem=0;
    	dfs(1,0,x);
    	for(register int i=1;i<=n;i++)
    	 tem+=subans[i];
    	return tem>=m;
    }
    int main()
    {
    	LL l=0,r=0,ans,c;
    	scanf("%d%d",&n,&m);
    	for(register int i=1,a,b;i<n;i++)
    	{
    		scanf("%d%d%lld",&a,&b,&c);
    		add(a,b,c); add(b,a,c); r+=c;
    	}
    	r/=(LL)m;
    	while(l<=r)
    	{
    		if(check(mid)) ans=mid,l=mid+1;
    		else r=mid-1;
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    C#异常断电后重新启动项目出现配置未初始化错误
    TFS: 解决The build agent error
    删除TFS中的项目
    将现有项目添加到TFS中
    Typora开启行内公式
    Markdown上下标内容多于一项
    小甲鱼python基础教程飞机大战源码及素材
    Git 将本地库添加到远程仓库
    C# float与UInt16互转
    C++的重载流输出运算符
  • 原文地址:https://www.cnblogs.com/karryW/p/10657076.html
Copyright © 2011-2022 走看看