提交记录 12861


用户 题目 状态 得分 用时 内存 语言 代码长度
luosiyuan noip18c. 【NOIP2018】赛道修建 Accepted 100 48.985 ms 6 MB + 928 KB C++11 3.31 KB
提交时间 评测时间
2020-06-13 19:47:14 2020-08-01 03:00:34
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rg register
#define in(s) freopen(s".in","r",stdin)
#define out(s) freopen(s".out","w",stdout)
inline int read(){
	int x=0,k=1;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();}
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+c-'0',c=getchar();
	return k*x;
}
inline void wr(rg int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) wr(x/10);
	putchar(x%10+'0');
}
const int N=5e4+10;
int n,m,tot,to[N<<1],c[N<<1],nxt[N<<1],head[N],sum,dep[N],sr[N][2],maxx,numm,ot[N],ww[N],a[N],minn=0x3f3f3f3f,he[N];
bool flag=1;
inline void add_edge(rg int u,rg int v,rg int w){
	to[++tot]=v;
	c[tot]=w;
	nxt[tot]=head[u];
	head[u]=tot;
	to[++tot]=u;
	c[tot]=w;
	nxt[tot]=head[v];
	head[v]=tot;
}
inline void dfs1(int x,int fa){
    for(int i=head[x];i;i=nxt[i]){
        int v=to[i];
    	if(v==fa)continue;
        dfs1(v,x);
        a[x]=c[i];
    }
}
inline bool check(rg int s){
	int num=0,now=0;
    for(int i=1;i<n;i++){
        if(now+a[i]>=s)now=0,num++;
        else now+=a[i];
    }
    return num>=m;
}
inline void dfs(rg int x,rg int fa){
	dep[x]=dep[fa]+1;
	for(rg int i=head[x];i;i=nxt[i]){
		int v=to[i];
		if(v==fa)continue;
		dfs(v,x);
		if(sr[v][0]+c[i]>sr[x][0])sr[x][1]=sr[x][0],sr[x][0]=sr[v][0]+c[i];
		else if(sr[v][0]+1>sr[x][1])sr[x][1]=sr[v][0]+c[i];
	}
	maxx=max(maxx,sr[x][0]+sr[x][1]);
}
inline bool cmp(int a,int b){
	return a>b;
}
inline void jh(){
    sort(ww+1,ww+n,cmp);
    int ans=2147483647;
    for(int i=1;i<=m;i++)
        ans=min(ans,ww[i]+ww[2*m-i+1]);
    wr(ans);
}
vector<int>ve[N];
inline int check1(rg int x,rg int mid1,rg int mid){
    int nums=0;
   	for(rg int i=ve[x].size()-1,j=0;j<i;){
   		if(i==mid1)i--;
   		if(j==mid1)j++;
   		if(j>=i)break;
		if(ve[x][i]+ve[x][j]<mid)j++;
		else nums++,i--,j++;
	}
    return nums;
}
inline void dfs3(rg int x,rg int fa,rg int mid){
	for(rg int i=head[x];i;i=nxt[i]){
		int v=to[i];
		if(v==fa)continue;
		dfs3(v,x,mid);
		he[v]+=c[i];
		if(he[v]>=mid)numm++;
        else ve[x].push_back(he[v]);
	}
	sort(ve[x].begin(),ve[x].end());
	int nm=0;
	for(rg int i=ve[x].size()-1,j=0;j<i;){
		if(ve[x][i]+ve[x][j]<mid)j++;
		else nm++,i--,j++;
	}
    numm+=nm;
    if((nm<<1)==ve[x].size())return;
    int l=0,r=ve[x].size()-1,as=0;
    while(l<=r){
    	int mid1=l+r>>1;
        if(check1(x,mid1,mid)>=nm)as=mid1,l=mid1+1;
        else r=mid1-1;
    }
    he[x]=ve[x][as];
}
int main(){
	#ifndef D
//	in("track");
//	out("track");
	#endif
	n=read(),m=read();
	for(rg int i=1,u,v,w;i<n;i++){
		u=read(),v=read(),w=read();
		ww[i]=w;
		sum+=w;
		minn=min(minn,w);
		if(v!=u+1)flag=0;
		add_edge(u,v,w);
		ot[u]++,ot[v]++;
	}
	if(m==1){
		dfs(1,0);
		wr(maxx);
		return 0;
	}
	if(ot[1]==n-1){
		jh();
		return 0;
	}
	if(flag){
		dfs1(1,0);
        int l=1,r=sum+1,ans=0;
        while(l<=r){
            int mid=l+r>>1;
            if(check(mid))ans=mid,l=mid+1;
            else r=mid-1;
        }
        wr(ans);
		return 0;
	}
	if(m==n-1){
		wr(minn);
		return 0;
	}
	int l=1,r=sum/m,ans=0;
	while(l<=r){
		memset(he,0,sizeof(he));
		for(rg int i=1;i<=n;i++)ve[i].clear(); 
		numm=0;
		int mid=l+r>>1;
		dfs3(1,0,mid);
		if(numm>=m)ans=mid,l=mid+1;
		else r=mid-1;
	} 
	wr(ans);
	return 0;
}
//10 3
//1 2 1
//2 3 2
//3 4 1
//4 5 2
//5 6 1
//6 7 2
//7 8 1
//8 9 2
//9 10 1

//8 3
//1 2 1
//1 3 2
//1 4 1
//1 5 2
//1 6 1
//1 7 2
//1 8 1

CompilationN/AN/ACompile OKScore: N/A

Testcase #1193.28 us1 MB + 220 KBAcceptedScore: 5

Testcase #2195.97 us1 MB + 216 KBAcceptedScore: 5

Testcase #3192.37 us1 MB + 212 KBAcceptedScore: 5

Testcase #4252.08 us1 MB + 272 KBAcceptedScore: 5

Testcase #51.564 ms2 MB + 368 KBAcceptedScore: 5

Testcase #62.366 ms2 MB + 792 KBAcceptedScore: 5

Testcase #73.035 ms2 MB + 244 KBAcceptedScore: 5

Testcase #85.028 ms2 MB + 924 KBAcceptedScore: 5

Testcase #9269.94 us1 MB + 332 KBAcceptedScore: 5

Testcase #104.201 ms4 MB + 660 KBAcceptedScore: 5

Testcase #116.338 ms6 MB + 928 KBAcceptedScore: 5

Testcase #12285.97 us1 MB + 416 KBAcceptedScore: 5

Testcase #13276.84 us1 MB + 416 KBAcceptedScore: 5

Testcase #14344.91 us1 MB + 424 KBAcceptedScore: 5

Testcase #15336.26 us1 MB + 428 KBAcceptedScore: 5

Testcase #16711.87 us1 MB + 484 KBAcceptedScore: 5

Testcase #17745.15 us1 MB + 472 KBAcceptedScore: 5

Testcase #1828.072 ms2 MB + 980 KBAcceptedScore: 5

Testcase #1925.695 ms3 MB + 120 KBAcceptedScore: 5

Testcase #2048.985 ms4 MB + 396 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2020-08-05 15:22:14 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用