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

直径分两种情况

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

第一种情况直接树形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. Cocos Creator 功能介绍

    cc.Class({ extends: cc.Component, properties: { anim: cc.Animation }, playRun: function() { this.ani ...

  2. Hadoop 在启动或者停止的时候需要输入yes确认问题

    启动或者停止hadoop的时候,信息如下: Stopping namenodes on [hadoop1 hadoop2] The authenticity of host 'hadoop2 (172 ...

  3. 第06组 Alpha冲刺(3/4)

    队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11899921.html 作业博客 :https://edu.cnblogs.com/campus/f ...

  4. 剑指offer:链表中环的入口结点

    题目描述: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 思路分析: 这道题首先需要判断链表是否存在环,很快就能想到用快慢指针来判断. 由于快慢指针的相遇位置并不一定为链 ...

  5. 单细胞数据整合方法 | Comprehensive Integration of Single-Cell Data

    操作代码:https://satijalab.org/seurat/ 依赖的算法 CCA CANONICAL CORRELATION ANALYSIS | R DATA ANALYSIS EXAMPL ...

  6. SQLServer 截取函数 substring函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...

  7. odoo开发笔记 -- 多个子类继承同一个父类方法的执行顺序

    场景描述: odoo模块化开发的架构理念,科学&高效, 可以让很多业务场景,尽可能松耦合:让开发人员的主要精力,关注在当前的业务逻辑: 所谓「前人栽树,后人乘凉」,模块整体好比一棵大树, 开发 ...

  8. java生成订单编号

    随着项目用户数量的扩大,高并发随之而来.那么如何在当前系统生成唯一编号呢? 一台数据库可以用自增,集群呢?当然也有随之的解决方案,但是最好的还是在项目生成了唯一的编号再插入到数据库.而不是数据库插入了 ...

  9. 【JS】AJAX跨域-被调用方与调用方解决方案(二)

    解决跨域问题 跨域问题说明,参考[JS]AJAX跨域-JSONP解决方案(一) 实例,使用上一章([JS]AJAX跨域-JSONP解决方案(一))的实例 解决方案三(被调用方支持跨域-服务端代码解决) ...

  10. python之psutil模块

    简述 psutil是一个跨平台库(http://code.google.com/p/psutil/) ,能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系 ...