Roads in the Kingdom CodeForces - 835F (直径)
大意: 给定一个基环树, 求删除一条环上的边使得直径最小.
直径分两种情况
- 环上点延伸的树内的直径
- 两个环上点的树内深度最大的点匹配
第一种情况直接树形dp求一下, 第二种情况枚举删除的环边, 线段树维护一下即可.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+10;
int n,dep[N],vis[N];
struct _ {int to,w;} fa[N];
vector<_> g[N];
int a[N], v[N], top;
ll mx[N], b[N], sum[N], ans, ans2;
struct node {
ll m1,m2,v;
node operator + (const node &rhs) const {
node ret;
ret.m1=max(m1,rhs.m1);
ret.m2=max(m2,rhs.m2);
ret.v=max(v,rhs.v);
ret.v=max(ret.v,m2+rhs.m1);
return ret;
}
} tr[N<<2]; void get(int x, int y) {
if (dep[x]<dep[y]) return;
v[++top] = y;
a[top+1] = fa[y].w;
for (; x!=y; x=fa[x].to) v[++top] = x, a[top+1] = fa[x].w;
}
void dfs(int x, int f) {
dep[x] = dep[f]+1;
for (_ e:g[x]) if (e.to!=f) {
int y = e.to;
fa[y] = {x,e.w};
if (dep[y]) get(x,y);
else dfs(y,x);
}
}
void dfs2(int x) {
vis[x] = 1;
for (_ e:g[x]) if (!vis[e.to]) {
int y = e.to;
dfs2(y);
ans = max(ans, mx[x]+mx[y]+e.w);
mx[x] = max(mx[x], mx[y]+e.w);
}
} void build(int o, int l, int r) {
if (l==r) tr[o].m1=b[l]+sum[l],tr[o].m2=b[l]-sum[l],tr[o].v=0;
else {
build(ls),build(rs);
tr[o]=tr[lc]+tr[rc];
}
}
node qry(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return tr[o];
if (mid>=qr) return qry(ls,ql,qr);
if (mid<ql) return qry(rs,ql,qr);
return qry(ls,ql,qr)+qry(rs,ql,qr);
} int main() {
scanf("%d", &n);
REP(i,1,n) {
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
g[u].pb({v,w});
g[v].pb({u,w});
}
dfs(1,0);
REP(i,1,top) vis[v[i]]=1;
REP(i,1,top) dfs2(v[i]),b[i]=mx[v[i]];
REP(i,top+1,2*top-1) b[i] = b[i-top];
REP(i,top+2,2*top-1) a[i] = a[i-top];
REP(i,1,2*top-1) sum[i] = sum[i-1]+a[i];
build(1,1,2*top-1);
ans2 = 1e18;
REP(i,1,top) ans2 = min(ans2, qry(1,1,2*top-1,i,i+top-1).v);
printf("%lld\n",max(ans,ans2));
}
Roads in the Kingdom CodeForces - 835F (直径)的更多相关文章
- Codeforces 835F Roads in the Kingdom (环套树 + DP)
题目链接 Roads in the Kingdom 题意 给出一个环套树的结构,现在要删去这个结构中的一条边,满足所有点依然连通. 删边之后的这个结构是一棵树,求所有删边情况中树的直径的最小值. 显 ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 835 F Roads in the Kingdom(树形dp)
F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...
- codeforces 427 div.2 F. Roads in the Kingdom
F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- Codeforces 835F Roads in the Kingdom - 动态规划
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...
- 【CodeForces】835F Roads in the Kingdom
一.题目 题目描述 王国有\(n\)座城市与\(n\)条有长度的街道,保证所有城市直接或间接联通,我们定义王国的直径为所有点对最短距离中的最大值,现因财政危机需拆除一条道路并同时要求所有城市仍然联通, ...
- Codeforces 1413F - Roads and Ramen(树的直径+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...
- CF835F Roads in the Kingdom/UOJ126 NOI2013 快餐店 树的直径
传送门--CF 传送门--UOJ 题目要求基环树删掉环上的一条边得到的树的直径的最小值. 如果直接考虑删哪条边最优似乎不太可做,于是考虑另一种想法:枚举删掉的边并快速地求出当前的直径. 对于环上的点, ...
随机推荐
- Codevs 2009 大dota英雄 2013年省队选拔赛辽宁(状压DP)
2009 大dota英雄 2013年省队选拔赛辽宁 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 话说退役后的生活好无聊啊,以 ...
- C语言函数内局部变量释放的坑
首先把代码贴上来: #include <stdio.h> #include<windows.h> int f(int **iptr){ ; *iptr = &a; ; ...
- Echarts的简单入门
5 分钟上手 ECharts 获取 ECharts 你可以通过以下几种方式获取 ECharts. 从官网下载界面选择你需要的版本下载,根据开发者功能和体积上的需求,我们提供了不同打包的下载,如果你在体 ...
- MongoDB 高级查询_aggregate聚合管道
MongoDB 聚合管道(AggregationPipeline) 使用聚合管道可以对集合中的文档进行变换和组合.实际项目应用主要是表关联查询.数据的统计. MongoDB 中使用 db.COLLEC ...
- 腾讯云手动搭建nginx+php-fpm并自启动
自己一点小爱好,搭建了一个小网站植物大战僵尸百科, 使用的是腾讯云,市场里的镜像不好用,所以自己手动搭建一波. centos 7 编译安装 php-7.2.11的步骤 在官网下载php-7.2.11的 ...
- 刷题记录:2018HCTF&admin
目录 刷题记录:2018HCTF&admin 一.前言 二.正文 1.解题过程 2.解题方法 刷题记录:2018HCTF&admin 一.前言 经过一个暑假的学习,算是正经一条web狗 ...
- 【大数据应用技术】作业九|安装关系型数据库MySQL 安装大数据处理框架Hadoop
本次作业的要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 1.安装MySql 按ctrl+alt+t打开终端窗口,安 ...
- TICK/TIGK运维栈安装运行 docker【中】
InfluxDB docker search influxdb docker pull influxdb docker run -d -p 8086:8086 -v /var/lib/influxdb ...
- VMware7.1安装教程
VMWare是一个"虚拟PC"软件公司.它的产品可以使你在一台机器上同时运行二个或更多Windows.DOS.LINUX系 统.与"多启动"系统相比,VMWar ...
- PHP系列 | 编译安装msgpack-php
Msgpack 是一个 PECL 扩展,此扩展提供用于与 MessagePack 序列化通信的 API. MessagePack 是一个基于二进制高效的对象序列化类库,可用于跨语言通信.它可以像JSO ...