一道树的直径

树网的核 BZOJ原题链接

树网的核 洛谷原题链接

消防 BZOJ原题链接

消防 洛谷原题链接

一份代码四倍经验,爽

显然要先随便找一条直径,然后直接枚举核的两个端点,对每一次枚举的核遍历核上的每个点,用\(dfs\)求出核外节点到核的最大值即可,时间复杂度为\(O(n^3)\),这在\(NOIP\)的原数据范围下是可以过的,但对于数据加强版就必须要优化了。

发现当枚举到直径上的某个点时,核的另一端在不超过\(s\)的前提下显然越远越好。这样就直接优化掉一个\(n\)了,但我们还可以继续优化。

设直径上的点为\(u_1,u_2,\dots,u_t\),当前枚举到的核的两端点为\(x_i,x_j\)。

根据直径的最长性,我们可以发现对于该核的偏心距实际上就是\(\max\{\max\limits_{k=1}^{t}\{d[u_k]\},dis(u_1,x_i),dis(x_j,u_t)\}\),数组\(d\)表示直径外节点(不经过直径上的点)到\(u_k\)的最大值,\(dis\)表示两点间的距离。

而\(\max\limits_{k=1}^{t}\{d[u_k]\}\)显然是个定值,至于\(dis\),我们可专门剖出直径上的所有边,然后用在枚举核的左端点时用两个变量维护即可,时间复杂度\(O(n)\)。

#include<cstdio>
using namespace std;
const int N = 5e5 + 10;
struct dd {
int dis, x;
};
dd D[N], a[N];
int fi[N], di[N << 1], da[N << 1], ne[N << 1], l, ma;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline int maxn(int x, int y)
{
return x > y ? x : y;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
inline void add(int x, int y, int z)
{
di[++l] = y;
da[l] = z;
ne[l] = fi[x];
fi[x] = l;
}
void dfs(int x, int fa, int dis, int la)
{
int i, y;
if (dis > ma)
{
ma = dis;
D[0].x = x;
}
D[x].x = fa;
D[x].dis = la;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (y != fa)
dfs(y, x, dis + da[i], da[i]);
}
}
void dfs_2(int x, int dis)
{
int i, y;
v[x] = 1;
if (dis > ma)
ma = dis;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (!v[y])
dfs_2(y, dis + da[i]);
}
}
int main()
{
int i, j, n, m, x, y, z, s = 0, k = 0, tail = 0, head = 0, an = 1e9;
n = re();
m = re();
for (i = 1; i < n; i++)
{
x = re();
y = re();
z = re();
add(x, y, z);
add(y, x, z);
}
dfs(1, 0, 0, 0);
ma = 0;
dfs(D[0].x, 0, 0, 0);
for (i = D[0].x; i; i = D[i].x)
{
v[i] = 1;
a[++k].x = i;
a[k].dis = D[i].dis;
}
ma = 0;
for (i = 1; i <= k; i++)
dfs_2(a[i].x, 0);
for (j = 1; j < n; j++)
if (s + a[j].dis <= m)
s += a[j].dis;
else
break;
for (i = j; i < n; i++)
tail += a[i].dis;
an = minn(an, maxn(ma, maxn(head, tail)));
for (i = 1; i < n; i++)
{
s -= a[i].dis;
head += a[i].dis;
for (; j < n; j++)
if (s + a[j].dis <= m)
{
s += a[j].dis;
tail -= a[j].dis;
}
else
break;
an = minn(an, maxn(ma, maxn(head, tail)));
}
printf("%d", an);
return 0;
}

BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防的更多相关文章

  1. 洛谷P1099 BZOJ1999 树网的核 [搜索,树的直径]

    洛谷传送门,BZOJ传送门 树网的核 Description 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T为树网(treenetwork),其中V ...

  2. [洛谷P2491] [SDOI2011]消防

    洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...

  3. [BZOJ1999] 树网的核 [数据加强版] (树的直径)

    传送门 如果只是想验证算法正确性这里是洛谷数据未加强版 Description 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T为树网(treenet ...

  4. [BZOJ1999][codevs1167][Noip2007]Core树网的核

    [BZOJ1999][codevs1167][Noip2007]Core树网的核 试题描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T为树网(t ...

  5. 【BZOJ2282】[Sdoi2011]消防 树形DP+双指针法+单调队列

    [BZOJ2282][Sdoi2011]消防 Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这 ...

  6. bzoj1999 (洛谷1099) 树网的核——dfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1999  https://www.luogu.org/problemnew/show/P109 ...

  7. 洛谷 1099 ( bzoj 1999 ) [Noip2007]Core树网的核

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1999 <算法竞赛进阶指南>346页.https://www.cnblogs.co ...

  8. 洛谷1099 [NOIP2007] 树网的核

    链接https://www.luogu.org/problemnew/show/P1099 题目描述 设T=(V,E,W)是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称TTT为 ...

  9. 【新知识】队列&bfs【洛谷p1996约瑟夫问题&洛谷p1451求细胞数量】

    (是时候为五一培训准备真正的技术了qwq) part1  队列(FIFO) 算法简介: FIFO:First In First Out(先进先出) 队列是限定在一端进行插入,另一端进行删除的特殊线性表 ...

随机推荐

  1. PS-Lite源码分析

    PS-Lite源码分析 http://blog.csdn.net/kangroger/article/details/73307685

  2. window上安装 MongoDB 及其 PHP扩展

    window上安装 MongoDB 及其 PHP扩展   工具/原料   window MongoDB MongoDB 方法/步骤     MongoDB 下载 MongoDB提供了可用于32位和64 ...

  3. 1.3.5、CDH 搭建Hadoop在安装之前(端口---Cloudera Search使用的端口)

    Cloudera Search使用的端口 在下表中,每个端口的“ 访问要求”列通常是“内部”或“外部”.在此上下文中,“内部”表示端口仅用于组件之间的通信; “外部”表示该端口可用于内部或外部通信. ...

  4. Java8中的[方法引用]“双冒号”——走进Java Lambda(四)

    前面的章节我们提及到过双冒号运算符,双冒号运算就是Java中的[方法引用],[方法引用]的格式是 类名::方法名 注意是方法名哦,后面没有括号“()”哒.为啥不要括号,因为这样的是式子并不代表一定会调 ...

  5. Java 面向切面 AOP

    参考: :http://www.blogjava.net/supercrsky/articles/174368.html AOP: Aspect Oriented Programming 即面向切面编 ...

  6. angularjs $watch

    参考 https://blog.csdn.net/u010451286/article/details/50635839 scope.$watch("field1 + field2" ...

  7. 【Django】ORM操作MySQL数据库遇到的一些问题

    关于查询操作: 1.exact和iexact exact相当于=   iexact相当于like(但是这里的like和数据库的不一样,没有给后面条件加上%%所以这里like和=的作用相似) artic ...

  8. 二进制中1的个数(python)

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. # -*- coding:utf-8 -*- class Solution: def NumberOf1(self, n): ...

  9. 第二章 向量(a)接口与实现

  10. poj2970 The lazy programmer 【优先队列】

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...