bzoj1999 数据加强版(n <= 5e5)
较早的noip题,值得研究
重要结论:直径的最长性,任何从直径中离开直径的点到它离开的点的距离,都不会比直径的另一端到它离开的点长(否则就有新的直径出现了嘛)
在求不经过直径的最长距离的时候犯错了,要考虑的是最远的点到现在点的距离,而不是只考虑儿子,码力还是太差……
包括代码的简洁性,多开了冗余的数组,都应当思考仔细

Code:

#include <cstdio>
using namespace std; const int N = 5e5 + ; int n, s, tot = , head[N], fa[N], dis[N];
int pa = , pb = , maxv = , tmpMaxDis = ;
bool vis[N]; struct Edge {
int to, nxt, val;
} e[N << ]; inline void add(int from, int to, int val) {
e[++tot].to = to;
e[tot].val = val;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int max(int x, int y) {
return x > y ? x : y;
} inline int min(int x, int y) {
return x > y ? y : x;
} void dfs(int x, int fat) {
fa[x] = fat;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dis[y] = dis[x] + e[i].val;
dfs(y, x);
}
} inline void getDia() {
dfs(, );
for(int i = ; i <= n; i++)
if(dis[pa] < dis[i]) pa = i; dis[pa] = ;
dfs(pa, );
for(int i = ; i <= n; i++)
if(dis[pb] < dis[i]) pb = i; for(int x = pb; x != ; x = fa[x])
vis[x] = ;
} void dfs2(int x, int ndis) {
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fa[x] || vis[y]) continue;
tmpMaxDis = max(tmpMaxDis, ndis + e[i].val);
dfs2(y, ndis + e[i].val);
}
} inline void getMaxDis() {
for(int i = ; i <= n; i++)
if(vis[i]) {
tmpMaxDis = ;
dfs2(i, );
maxv = max(maxv, tmpMaxDis);
}
} int main() {
read(n), read(s);
for(int x, y, v, i = ; i < n; i++) {
read(x), read(y), read(v);
add(x, y, v), add(y, x, v);
} getDia();
getMaxDis(); int ans = << ;
/* for(int i = pb; i; i = fa[i])
maxv = max(maxv, maxDis[i]); for(int j = d.size() - 1, i = d.size() - 1; i >= 0; i--) {
if(j != 0)
for(; j >= 0; j--) {
if(dis[d[j]] - dis[d[i]] > s) {
j++;
break;
}
}
if(j == -1) j++;
int tmp = max(maxv, max(dis[d[i]], dis[pb] - dis[d[j]]));
ans = min(ans, tmp);
} */ for(int j = pb, i = pb; i; i = fa[i]) {
for(; fa[j] && dis[i] - dis[fa[j]] <= s; j = fa[j]);
int tmp = max(maxv, max(dis[j], dis[pb] - dis[i]));
ans = min(ans, tmp);
} printf("%d\n", ans); return ;
}

Luogu 1099 树网的核的更多相关文章

  1. luogu 2491 [SDOI2011]消防 / 1099 树网的核 单调队列 + 树上问题

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 300001 #define inf 1000000000 ...

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

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

  3. BZOJ 1099 树网的核

    题面 解题思路 搞了三个多小时.... noip时的数据很水,直接暴力n^3过. 我们考虑优化,首先可以贪心,我们要在直径上选肯定越插长越好,所以n^2其实就可以解决.但这还不够,根据直径的最长性,我 ...

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

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

  5. BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防

    一道树的直径 树网的核 BZOJ原题链接 树网的核 洛谷原题链接 消防 BZOJ原题链接 消防 洛谷原题链接 一份代码四倍经验,爽 显然要先随便找一条直径,然后直接枚举核的两个端点,对每一次枚举的核遍 ...

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

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

  7. NOIP2007 树网的核 [提高组]

    题目:树网的核 网址:https://www.luogu.com.cn/problem/P1099 题目描述 设 T=(V,E,W)T=(V,E,W) 是一个无圈且连通的无向图(也称为无根树),每条边 ...

  8. 树网的核[树 floyd]

    描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各边长度的集合,并设T ...

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

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

随机推荐

  1. c&c++ datetime

    时间函数之间的关系 struct tm { int tm_sec; // 代表目前秒数,正常范围0-59,但允许至61秒: int tm_min; // 代表目前分数,范围为0-59. int tm_ ...

  2. Dell 12G服务器 手动安装RedHat 6.X

    12代服务器,是DELL目前最新产品,有R720,R520,R620,R420,M420 等产品 以下是光盘直接安装Red Hat 6.X 的方法步骤: 1,选择安装盘对应的启动设备 开机按F11,选 ...

  3. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) C. Bear and Drawing

    题目链接:http://codeforces.com/contest/573/problem/C题目大意:在两行无限长的点列上面画n个点以及n-1条边使得构成一棵树,并且要求边都在同一平面上且除了节点 ...

  4. jquery中ajax异步调用接口

    之前写过一个原始的.无封装的页面,没有引入任何外部js,直接实例化Ajax的XmlRequest对象去异步调用接口,参见Ajax异步调用http接口后刷新页面,可对比一下. 现在我们用jquery包装 ...

  5. mybatis返回Date类型数据 格式化

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") public Date getC ...

  6. iptables防火墙工作原理

    iptables防火墙工作原理 简介:iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防火墙基于内核编码实现,具有非常稳定的性能和高效率: iptables属 ...

  7. 新机器连接老机器遇到的ERROR

    Ansible无法连接老旧机器 报错内容: [root@BI ansible]# ansible -i /etc/ansible/hosts GameServer -m ping 10.10.113. ...

  8. 简述FPGA时序约束理论

    FPGA时序约束简介. 时序约束的场景: 在简单电路中,当频率较低时,数字信号的边沿时间可以忽略时,无需考虑时序约束.但在复杂电路中,为了减少系统中各部分延时,使系统协同工作,提高运行频率,需要进行时 ...

  9. 终端启动tomcat报错 command not found 解决方法 (含启动和关闭命令)

    Tomcat配置步骤: 1.cd命令进入Tomcat安装路径的bin下 2.sudo chmod 755*.sh 输入appleID密码获得相关权限 3.sudo sh ./startup.sh启动T ...

  10. thinkphp中的_initialize方法

    子类的_initialize方法自动调用父类的_initialize方法.而php的构造函数construct,如果要调用父类的方法,必须在子类构造函数显示调用parent::__construct( ...