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

直径分两种情况

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

第一种情况直接树形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. 无人机一体化3DGIS服务平台

    随着无人机技术的发展,无人机携带多种设备为GIS应用提供多元化海量基础数据.无人机航测更是以快速.灵活.高效的数据获取方式,迅速扩大了现有的GIS市场,同时GIS行业的广泛应用也推动了无人机技术的发展 ...

  2. 如果判断条件过多,可以直接在computed里面去返回需要判断的数据

    bad <div class="offer-item_margin" v-show="offer.supplierName || offer.supplierSto ...

  3. OpenFOAM——平行平板间具有相对运动(库埃特流)

    本算例翻译整理自:http://the-foam-house5.webnode.es/products/chapter-1-plane-parallel-plates-case/ 这个算例研究了一个距 ...

  4. kubernetes监控终极方案-kube-promethues

    kube-promethues简介 前面我们学习了Heapster+cAdvisor方式监控,这是Prometheus Operator出现之前的k8s监控方案.后来出现了Prometheus Ope ...

  5. TICK/TIGK运维栈安装运行【上】

    TICK/TIGK运运维metrics栈包括 InfuluxDB:为时间序列数据专门定制的高性能数据存储.TSM引擎允许高速接收和数据压缩.完全go语言编写.编译成一个单一的二进制,没有外部依赖.简单 ...

  6. freeNAS nfs linux挂载

    新建存储块,设置权限为root wheel 备份修改/var/lib/nova 名称 新建/var/lib/nova 目录,修改目录的属主机权限 mv /var/lib/nova /var/lib/n ...

  7. 自定义Spring Boot内置tomcat的404页面

    spring boot 的相关404页面配置都是针对项目路径下的(如果配置了 context-path) 在context-path不为空的情况下,如果访问路径不带context-path,这时候会显 ...

  8. Linux下打开超大文件的方法

    Linux下打开超大文件方法 在Linux下用VIM打开大小几个G.甚至几十个G的文件时,是非常慢的. 这时,我们可以利用下面的方法分割文件,然后再打开. 1 查看文件的前多少行 head -1000 ...

  9. [E2E_L9]类化和级联化

    一.多车辆识别可能和车辆车牌分割: 这样一张图,可以识别多车辆和车牌,问题是如何区分并且配对.  0  1  7  8 是否是车牌可以通过图片的大小进行判断.而配对是前后顺序的. // ------- ...

  10. DispatcherServlet 被加载顺序

    javax.servlet.Servlet#init 0 -> javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) 1 ...