Live2D

# 【NOIP2018】赛道修建(考场)

题意：



int fa[N],ans=0;
int dfs1(int u)
{
int sum1=0,sum2=0;
{
int v=edge[i].to;
if(v==fa[u]) continue;
fa[v]=u;
sum2=max(sum2,dfs1(v)+edge[i].dis);
if(sum2>sum1) swap(sum2,sum1);
}
ans=max(ans,sum1+sum2);
return sum1;
}


ans即为所求

bool check(int x)
{
int d=0,now=0;
for(register int i=1;i<n;++i)
{
if(now+a[i]>=x)
{
now=0;
d++;
}
else now+=a[i];
}
return d>=m;
}


if(juhua)//菊花图直接判断即可
{
{
int vv=edge[i].to;
a[vv-1]=edge[i].dis;
}
sort(a+1,a+n,cmp);
int ans=0x3f3f3f3f;
for(register int i=1;i<=m;++i)
ans=min(ans,a[i]+a[2*m-i+1]);
printf("%d\n",ans);
return 0;
}


#include<bits/stdc++.h>
#define N 50005
#define mid ((l+r+1)>>1)
using namespace std;

int n,m,u,v,w;

struct Edge
{
int next,to,dis;
}edge[N<<1];

inline void add_edge(int from,int to,int dis)
{
edge[cnt].to=to;
edge[cnt].dis=dis;
}

{
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}

int fa[N],ans=0;
int dfs1(int u)
{
int sum1=0,sum2=0;
{
int v=edge[i].to;
if(v==fa[u]) continue;
fa[v]=u;
sum2=max(sum2,dfs1(v)+edge[i].dis);
if(sum2>sum1) swap(sum2,sum1);
}
ans=max(ans,sum1+sum2);
return sum1;
}

int a[N];
void dfs2(int u)
{
{
int v=edge[i].to;
if(v==fa[u]) continue;
fa[v]=u;
dfs2(v);
a[u]=edge[i].dis;
}
}

bool check(int x)
{
int d=0,now=0;
for(register int i=1;i<n;++i)
{
if(now+a[i]>=x)
{
now=0;
d++;
}
else now+=a[i];
}
return d>=m;
}

bool cmp(int a,int b) {return a>b;}

int main()
{
//	freopen("testdata.in","r",stdin);
bool juhua=1,lian=1;
int sum=0;
for(register int i=1;i<=n-1;++i)
{
sum+=w;
if(u!=1) juhua=0;
if(v!=u+1) lian=0;
}
if(m==1)//只有一条路，算直径
{
dfs1(1);
printf("%d\n",ans);
return 0;
}
if(lian)//地图是一条链，相当于序列二分答案
{
dfs2(1);
int l=1,r=sum;
while(l<=r)
{
//			cout<<l<<" "<<r<<" "<<mid<<endl;
if(check(mid)) l=mid;
else r=mid-1;
}
printf("%d\n",l);
return 0;
}
if(juhua)//菊花图直接判断即可
{
{
int vv=edge[i].to;
a[vv-1]=edge[i].dis;
}
sort(a+1,a+n,cmp);
int ans=0x3f3f3f3f;
for(register int i=1;i<=m;++i)
ans=min(ans,a[i]+a[2*m-i+1]);
printf("%d\n",ans);
return 0;
}
return 0;
}


update：正解已迁移至这里：点我

$$\text{这里是tqr，联系我请加QQ735748368}$$
posted @ 2019-10-25 10:59  tqr06  阅读(137)  评论(0编辑  收藏  举报