[BZOJ 4890][TJOI2017]城市
$ \color{green} {solution : }$
我们可以暴力枚举断边,然后 $ O(n) $ 的跑一次换根 $ dp $,然后复杂度是 $ O(n * n) $ 的
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
template <typename T> inline void G(T &x) {
x = 0; char o; bool f = false;
for ( ; !isdigit(o = getchar()); ) {
if( o == '-') {
f = true;
}
}
for ( ; isdigit(o); o = getchar()) {
x = (x << 1) + (x << 3) + (o & 15);
}
if( f) {
x = ~x + 1;
}
}
template <typename T> inline bool check_Max(T &x, const T &y) {
return x < y ? x = y, false : true;
}
template <typename T> inline bool check_Min(T &x, const T &y) {
return x > y ? x = y, false : true;
}
struct Edge {
int v, w; Edge *to;
Edge ( int v, int w, Edge *to) : v(v), w(w), to(to) {}
Edge () {}
}*head[maxn], pool[maxn<<1], *pis = pool;
int di[maxn], ndi[maxn];
inline void dfs1(int u, int pre, int &Mx) {
di[u] = ndi[u] = 0;
for ( Edge *now = head[u]; now; now = now->to) if( now->v ^ pre) {
dfs1(now->v, u, Mx);
check_Max(ndi[u], di[now->v] + now->w);
if(ndi[u] > di[u]) {
swap(ndi[u], di[u]);
}
}
check_Max(Mx, ndi[u] + di[u]);
}
inline void dfs2(int u, int pre, int &Mx) {
check_Min(Mx, di[u]);
for ( Edge *now = head[u]; now; now = now->to) if( now->v ^ pre) {
if( di[u] == di[now->v] + now->w) {
check_Max(ndi[now->v], ndi[u] + now->w);
}
else {
check_Max(ndi[now->v], di[u] + now->w);
}
if( ndi[now->v] > di[now->v]) {
swap(di[now->v], ndi[now->v]);
}
dfs2(now->v, u, Mx);
}
}
int n;
struct line {
int l, r, w;
}data[maxn];
int main() {
G(n);
for ( register int i = 1; i < n; ++ i) {
G(data[i].l); G(data[i].r); G(data[i].w);
int u = data[i].l, v = data[i].r, w = data[i].w;
head[u] = new (pis ++) Edge(v, w, head[u]); head[v] = new (pis ++) Edge(u, w, head[v]);
}
int ans = 0x7fffffff;
for ( register int i = 1; i < n; ++ i) {
int mx1 = 0, mx2 = 0, ret = 0;
dfs1(data[i].l, data[i].r, mx1); dfs1(data[i].r, data[i].l, mx2);
check_Max(ret, mx1); check_Max(ret, mx2);
int mx3 = di[data[i].l], mx4 = di[data[i].r];
dfs2(data[i].l, data[i].r, mx3); dfs2(data[i].r, data[i].l, mx4);
check_Max(ret, mx3 + mx4 + data[i].w);
check_Min(ans, ret);
}
printf("%d\n",ans);
return 0;
}

[BZOJ 4890][TJOI2017]城市的更多相关文章
- BZOJ 4890: [Tjoi2017]城市 树形dp
标签:树形dp,枚举,树的直径 一上来看到这个题就慌了,只想到了 $O(n^3)$ 的做法. 碰到这种题时要一步一步冷静地去分析,观察数据范围. 首先,$n\leqslant 5000$,所以可以先 ...
- bzoj4890[Tjoi2017]城市(树的半径)
4890: [Tjoi2017]城市 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 149 Solved: 91[Submit][Status][D ...
- 【BZOJ4890】[TJOI2017]城市(动态规划)
[BZOJ4890][TJOI2017]城市(动态规划) 题面 BZOJ 洛谷 题解 数据范围都这样了,显然可以暴力枚举断开哪条边. 然后求出两侧直径,暴力在直径上面找到一个点,使得其距离直径两端点的 ...
- [洛谷P3761] [TJOI2017]城市
洛谷题目链接:[TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速 ...
- Luogu 3761 [TJOI2017]城市
BZOJ 4890. 在树上断开一条边之后会形成两个联通块,如果要使这一条边接回去之后保持一棵树的形态,那么必须在两个联通块之间各找一个点连接. 那么,对于每一条可能断开的边,它产生的答案是以下两者的 ...
- 换根DP+树的直径【洛谷P3761】 [TJOI2017]城市
P3761 [TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公 ...
- [TJOI2017]城市(树的直径)
[TJOI2017]城市 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有ri座城市,<-1条高速公路,保证了任意两运城市之间都可以通过高速公路相互可达, ...
- BZOJ4890 & 洛谷3761:[TJOI2017]城市——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4890 https://www.luogu.org/problemnew/show/P3761 从加 ...
- [bzoj 4887] [Tjoi2017]可乐
传送门 Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市 ...
随机推荐
- Linux网络配置之虚拟网卡的配置(Red Hat 6.5)
怎么查看当前的网络状态,这里就不着重描述,用ifconfig命令就可以.我们直接进入主题. red hat系统中的网卡配置在这个目录中:/etc/sysconfig/network-scripts 1 ...
- ethtool -p eth0 物理口一个灯在不停的闪烁
摘自:https://blog.csdn.net/morigejile/article/details/78598645 你的 服务器有多个网卡并且已经配置好运行当中,你却没记得eth0.eth1. ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
- Word文档如何发CSDN博客
目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...
- org.slf4j.impl.SimpleLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
查看日志信息: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/app/a ...
- emacs-ide配置
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CEDET Configuration ;;;;;;;;;; ...
- WCF服务编程 读书笔记——第1章 WCF基础(2)
续:第1章 WCF基础(1) 元数据交换 服务有两种方案可以发布自己的元数据.一种是基于HTTP-GET协议提供元数据, 另一种则是后面将要讨论的使用专门的终结点的方式.WCF能够为服务自动提供基于H ...
- 编写高质量代码改善C#程序的157个建议——建议99:重写时不应使用子类参数
建议99:重写时不应使用子类参数 重写时,如果使用了子类参数,可能会偏离设计者的预期目标.比如,存在一个如下继承体系: class Employee { } class Manager : Emplo ...
- Linux 判断进程是否已经运行的程序
bool ServerProcess::isAlreadyRunning() { #ifndef __linux__ WarningLog(<<"can't check if p ...
- Delphi XE8 iOS与Android移动应用开发(APP开发)教程[完整中文版]
https://item.taobao.com/item.htm?id=536584650957&toSite=main