[洛谷题面]https://www.luogu.org/problemnew/show/P4221

这个题以及[CTSC2018 暴力写挂]都有类似的乱搞做法能通过考场数据。

具体搞法就是随一个起点,找一个离他最远(按题目要求计算的贡献最大)的点,让后再令 \(now=mxpoint\) 不断迭代上述过程。

然后整个上述过程最好也要不断重复进行,直到卡满时限为止就好。

多调随机种子就好。

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
using ll=long long;
const int N=100005;
int rr()
{
return rand()%10000*10000+rand()%10000;
}
struct node
{
int tot,h[N],nxt[N<<1],to[N<<1];
ll dis[N],w[N<<1];
void addedge(int u,int v,ll _w) {
nxt[++tot]=h[u]; h[u]=tot;
to[tot]=v; w[tot]=_w;
nxt[++tot]=h[v]; h[v]=tot;
to[tot]=u; w[tot]=_w;
}
int sz[N],dep[N],son[N];
int fa[N],top[N];
void dfs(int u,int father)
{
sz[u]=1;
fa[u]=father;
for(int i=h[u],v;i;i=nxt[i])
{
v=to[i];
if(v!=father)
{
dep[v]=dep[u]+1;
dis[v]=dis[u]+w[i];
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]])son[u]=v;
}
}
}
void pul(int u,int Top)
{
top[u]=Top;
if(son[u])pul(son[u],Top);
for(int i=h[u],v;i;i=nxt[i])
{
v=to[i];
if(v!=fa[u]&&v!=son[u])
pul(v,v);
}
}
int lca(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])
swap(u,v);
u=fa[top[u]];
}
return dep[u]<dep[v]?u:v;
}
ll dist(int u,int v)
{
return dis[u]+dis[v]-dis[lca(u,v)]*2;
}
}a[3];
int n;
ll dist(int i,int j)
{
return a[0].dist(i,j)+a[1].dist(i,j)+a[2].dist(i,j);
}
ll ans;
int main()
{
scanf("%d",&n);
int x,y; ll z;
for(int k=0;k<3;k++)
{
for(int i=2; i<=n; i++)
{
scanf("%d%d%lld",&x,&y,&z);
a[k].addedge(x,y,z);
}
a[k].dfs(1,0);
a[k].pul(1,1);
}
if(n <= 3000)
{
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
ans=max(ans,dist(i,j));
}
else
{
for(int k=0; k<40; k++)
{
int now=rr()%n+1;
for(int i=0,j; i<5; i++)
{
ll mx=-1;
for(int l=1; l<=n; l++)
{
ll ext=dist(now,l);
if(ext>mx)
{
mx=ext;
j=l;
}
}
ans=max(ans,mx);
now=j;
}
}
}
printf("%lld",ans);
return 0;
}

最新文章

  1. 学习C++.Primer.Plus 10 对象和类
  2. C#模拟鼠标键盘控制其他窗口(一)
  3. struts2 用if标签判断字符串包含
  4. 常用类String的方法
  5. Add Office 365 Azure Directory into Windows Azure
  6. HQL的一些语句总结
  7. Appium+Robotframework实现Android应用的自动化测试-2:Windows中启动Appium和模拟器
  8. 简单SSM配置详解
  9. POJ 1113 - Wall 凸包
  10. 《FLASH CC 2015 CANVAS 中文教程》——2、基本的交互(点击、触摸)事件
  11. mac 更改word的默认显示比例为125
  12. sort()方法的应用(二)
  13. 承上 DBlink 与 SCN | 新增视图找出外部 SCN 跳变
  14. The Salt Master has cached the public key报错解决办法
  15. H5 PWA技术以及小demo
  16. 工厂模式——Head First
  17. 2019.01.24 bzoj3125: CITY(轮廓线dp)
  18. Spring Boot Controller相应JSP页面 错误whitelabel error page
  19. Linux slab分配器【转】
  20. centos7通过阿里云配置docker加速镜像

热门文章

  1. python-第三方库的理解及某个函数的源代码
  2. 【转载】JDBC操作LOB字段
  3. C++转换构造函数和隐式转换函数
  4. js无法监听input中js改变值的变化
  5. 微信小程序使用wxParse实现接入富文本编辑
  6. go基础_控制语句
  7. ARM64架构下面安装mysql5.7.22
  8. 隐藏pyqt中调用matplotlib图片中的工具栏
  9. Springboot 中AOP的使用
  10. Python基础数据类型以及对应方法