大意: 给定一个基环树, 求删除一条环上的边使得直径最小.

直径分两种情况

  • 环上点延伸的树内的直径
  • 两个环上点的树内深度最大的点匹配

第一种情况直接树形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 (直径)的更多相关文章

  1. Codeforces 835F Roads in the Kingdom (环套树 + DP)

    题目链接 Roads in the Kingdom 题意  给出一个环套树的结构,现在要删去这个结构中的一条边,满足所有点依然连通. 删边之后的这个结构是一棵树,求所有删边情况中树的直径的最小值. 显 ...

  2. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  3. Codeforces 835 F Roads in the Kingdom(树形dp)

    F. Roads in the Kingdom(树形dp) 题意: 给一张n个点n条边的无向带权图 定义不便利度为所有点对最短距离中的最大值 求出删一条边之后,保证图还连通时不便利度的最小值 $n & ...

  4. 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 ...

  5. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  6. Codeforces 835F Roads in the Kingdom - 动态规划

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...

  7. 【CodeForces】835F Roads in the Kingdom

    一.题目 题目描述 王国有\(n\)座城市与\(n\)条有长度的街道,保证所有城市直接或间接联通,我们定义王国的直径为所有点对最短距离中的最大值,现因财政危机需拆除一条道路并同时要求所有城市仍然联通, ...

  8. Codeforces 1413F - Roads and Ramen(树的直径+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...

  9. CF835F Roads in the Kingdom/UOJ126 NOI2013 快餐店 树的直径

    传送门--CF 传送门--UOJ 题目要求基环树删掉环上的一条边得到的树的直径的最小值. 如果直接考虑删哪条边最优似乎不太可做,于是考虑另一种想法:枚举删掉的边并快速地求出当前的直径. 对于环上的点, ...

随机推荐

  1. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  2. GoCN每日新闻(2019-10-06)

    GoCN每日新闻(2019-10-06) 国庆专辑:GopherChina祝大家国庆节快乐 GoCN每日新闻(2019-10-06) 1. Go 1.14 有什么新变化 http://docs.goo ...

  3. hadoop jps 出现空指针错误

    在hadoop中安装jdk软件以后出现如下问题: 错误描述 [xxx@localhost jdk1..0_181]$ ./bin/jps Exception in thread "main& ...

  4. vim配置无插件

    其实,vim插件会影响编辑器的启动速度,虽然有些插件影响不大,我依然觉得不够,其实通过简易的状态栏,可以显示必要的信息,能自定义颜色和背景甚至透明就足够了. 一.自定义状态栏其实以下内容可以写在一行上 ...

  5. devops 运维平台相关知识

    1.https://choerodon.io/zh/community/ (代码 https://github.com/choerodon/choerodon) 猪齿鱼 2.https://www.o ...

  6. Linux系统学习(二)一Linux基本操作

    一.Linux的目录结构 1.1 Linux的目录结构图 1.2 目录内容 /:这就是根目录.对你的电脑来说,有且只有一个根目录.所有的东西,我是说所有的东西都是从这里开始.举个例子:当你在终端里输入 ...

  7. 使用lombok的@Builder的注解的一个坑

    一开发说项目报错 java.lang.Long,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lan ...

  8. Android dump命令查看某个apk是被谁安装的?

    adb shell dumpsys package packages > packageAll.txt     ORadb shell pm dump packages > package ...

  9. 取消Windows server 关机提示备注的方法

    打开“开始”-“运行”,在“打开”一栏中输入“gpedit.msc”命令打开组策略编辑器,依次展开“计算机配置”→“管理模板”→“系统”,双击右侧窗口出现的“显示‘关闭事件跟踪程序’”,将“未配置”改 ...

  10. [LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...