time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In the Kingdom K., there are n towns numbered with integers from 1 to n. The towns are connected by n bi-directional roads numbered with integers from 1 to n. The i-th road connects the towns ui and vi and its length is li. There is no more than one road between two towns. Also, there are no roads that connect the towns with itself.

Let's call the inconvenience of the roads the maximum of the shortest distances between all pairs of towns.

Because of lack of money, it was decided to close down one of the roads so that after its removal it is still possible to reach any town from any other. You have to find the minimum possible inconvenience of the roads after closing down one of the roads.

Input

The first line contains the integer n (3 ≤ n ≤ 2·105) — the number of towns and roads.

The next n lines contain the roads description. The i-th from these lines contains three integers ui, vi, li (1 ≤ ui, vi ≤ n, 1 ≤ li ≤ 109) — the numbers of towns connected by the i-th road and the length of the i-th road. No road connects a town to itself, no two roads connect the same towns.

It's guaranteed that it's always possible to close down one of the roads so that all the towns are still reachable from each other.

Output

Print a single integer — the minimum possible inconvenience of the roads after the refusal from one of the roads.

Examples
Input
3
1 2 4
2 3 5
1 3 1
Output
5
Input
5
2 3 7
3 1 9
4 1 8
3 5 4
4 5 5
Output
18

题意 :

  给定n个点 n条边 (保证联通 ,边无向 ,无重边 ,无自环)

  断掉某条边以后 保证联通的情况下,图上有一条最长路

  求最长路的最小值

思路 :

  n个联通的点 n条边

  即 树上多了一个环

  断掉的边只能在环上

  所以 题意转化为 从环上断掉一个点以后 树上直径的最小化

  首先 dfs 找出环

  然后得到非环上边的最长路(求以环上点为根的子树直径)

  之后得到环上点到其子树的最长路 记为 点  i  的权值  val [ i ]

  样例二:

      

  得到 当前最长直径是 点 3 的子树  长度为 7   记录下来为   L1  (我在这里错了几次)

  得到每个点的权值

val   [ 1 ]  =    0

val   [ 3 ]  =    7

val   [ 4 ]  =    0

val   [ 5 ]  =    0

然后得到环上的 前缀路径和 与后缀路径和

  point     1  ->  3  ->   5  ->  4  ->  1  ->  3  ->  5  ->  4

  road         9        4        5       8       9       4        5

  pre- >    0       9        13     18     26      35     39       44  ->

  suf <-   44       35        31      26      18        9       5       0   <-

  然后  容易知道  到点 i  的  “前缀和”+ “点权值”  (pre [ i ] + val [ i ])  表示  环上第一个点(上图中为1)到 i 的 子树的最长路径

  由于  “树上 离任意一点最远的一定是  直径的某个端点”

  这样由区间最大值就可以得到 一个直径端点   然后往 “前面” 和 “后面” 找到 离端点最远的另一个点 就是直径的第二个端点

  每次的区间大小为环的长度 (表示断掉一条边以后的路径前缀和) 每次向右移动一次 得到 第二个直径  并更新 ans

  区间最大值的维护可以通过线段树      维护一个前缀的  一个后缀的

  最后答案输出 max(ans,L1)  即可

  

    

 #include <bits/stdc++.h>

 #define mp make_pair
#define pb push_back
#define lson l,mid,pos<<1
#define rson mid+1,r,pos<<1|1
#define fi first
#define se second using namespace std; typedef long long LL;
typedef pair<long long ,int> pli; const long long INF = 0x3f3f3f3f3f3f3f3f; vector <int > nt[];
vector <int > cc[];
int noloop[];
int cnt[];
LL val[];
LL toval[];
LL id[];
int idfrm[];
pli tree1[<<];
pli tree2[<<];
LL pre[];
LL suf[];
int mark=;
LL zz010=;
void dfs1(int x,int fa)
{
noloop[x]=;
for (int i=;i<nt[x].size();i++){
if (fa==nt[x][i])continue;
cnt[nt[x][i]]--;
if (cnt[nt[x][i]]==)dfs1(nt[x][i],x);
}
}
LL dfs2(int x,int fa)
{
LL ret=;
LL ret2=;
for (int i=;i<nt[x].size();i++){
if (fa==nt[x][i]||noloop[nt[x][i]]==)continue;
LL tmp=dfs2(nt[x][i],x)+cc[x][i];
if (tmp>ret){
ret2=ret;
ret=tmp;
}else if (tmp>ret2)ret2=tmp;
}
zz010=max(zz010,ret+ret2);
return val[x]=ret;
}
void dfs3(int x,int fa)
{
idfrm[mark]=x;
id[x]=mark;
for (int i=;i<nt[x].size();i++){
if (noloop[nt[x][i]]||nt[x][i]==fa||id[nt[x][i]])continue;
toval[mark++]=cc[x][i];
dfs3(nt[x][i],x);
return ;
}
for (int i=;i<nt[x].size();i++){
if (id[nt[x][i]]!=)continue;
toval[mark]=cc[x][i];
return ;
}
}
void push_up(pli tree[],int pos)
{
tree[pos]=max(tree[pos<<],tree[pos<<|]);
}
void upd(pli tree[],int l,int r,int pos,int x,long long val)
{
if (l==r){
tree[pos].fi=val;
tree[pos].se=x;
return ;
}
int mid=(l+r)>>;
if (x<=mid)upd(tree,lson,x,val);
else upd(tree,rson,x,val);
push_up(tree,pos);
}
pli query(pli tree[],int l,int r,int pos,int l1,int r1)
{
if (l1>r1)return mp(-INF,);
if (l>=l1&&r1>=r){
return tree[pos];
}
pli mx=mp(-INF,);
int mid=(l+r)>>;
if (mid>=l1)mx=query(tree,lson,l1,r1);
if (mid+<=r1)mx=max(mx,query(tree,rson,l1,r1));
return mx;
}
int main()
{
int n;
scanf("%d",&n);
int a,b;
long long v;
for (int i=;i<n;i++){
scanf("%d%d%I64d",&a,&b,&v);
nt[a].pb(b);
nt[b].pb(a);
cc[a].pb(v);
cc[b].pb(v);
cnt[a]++;
cnt[b]++;
}
for (int i=;i<=n;i++){
if (nt[i].size()==)dfs1(i,-);
}
for (int i=;i<=n;i++){
if (noloop[i]==)dfs2(i,-);
}
for (int i=;i<=n;i++){
if (!noloop[i]){
dfs3(i,-);
break;
}
}
long long tmp=;
for (int i=;i<=mark;i++){
idfrm[i+mark]=idfrm[i];
toval[i+mark]=toval[i];
}
toval[]=toval[mark];
for (int i=;i<=*mark;i++){
pre[i]=pre[i-]+toval[i-];
upd(tree1,,*mark,,i,val[idfrm[i]]+pre[i]);
}
for (int i=*mark;i>=;i--){
suf[i]=suf[i+]+toval[i];
upd(tree2,,*mark,,i,val[idfrm[i]]+suf[i]);
}
LL ans=INF;
int flag=;
for (int i=;i<=mark;i++){
pli now_mx=query(tree1,,*mark,,i,i+mark-);
pli tmp1=query(tree1,,*mark,,now_mx.se+,i+mark-);
pli tmp2=query(tree2,,*mark,,i,now_mx.se-);
LL dis1=0LL,dis2=0LL,dis3=,dis4=;
if (tmp1.se!=)dis1=val[idfrm[now_mx.se]]+val[idfrm[tmp1.se]]+pre[tmp1.se]-pre[now_mx.se];
if (tmp2.se!=)dis2=val[idfrm[now_mx.se]]+val[idfrm[tmp2.se]]-pre[tmp2.se]+pre[now_mx.se];
ans=min(ans,max(dis1,dis2));
} cout<<max(ans,zz010);
return ;
}

最后   数组记得开大点   前缀和与后缀和  为 环上点数量的两倍

  

codeforces 427 div.2 F. Roads in the Kingdom的更多相关文章

  1. Codeforces 835 F. Roads in the Kingdom

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

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

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

  3. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  8. Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块

    Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ​ ...

  9. CodeForces 835C - Star sky | Codeforces Round #427 (Div. 2)

    s <= c是最骚的,数组在那一维开了10,第八组样例直接爆了- - /* CodeForces 835C - Star sky [ 前缀和,容斥 ] | Codeforces Round #4 ...

随机推荐

  1. luogu P1041 传染病控制

    题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...

  2. 【bzoj4950】【 [Wf2017]Mission Improbable】贪心+二分图匹配

    (上不了p站我要死了,侵权度娘背锅) Description 那是春日里一个天气晴朗的好日子,你准备去见见你的老朋友Patrick,也是你之前的犯罪同伙.Patrick在编程竞赛 上豪赌输掉了一大笔钱 ...

  3. 转换vmware的vmdk格式到qcow2或者raw格式

    qemu-img convert  xxxx-disk1.vmdk -f  vmdk  -O qcow2 xxxx-disk1.qcow2 qemu-img convert xxxx-disk1.vm ...

  4. XCTest(一)

    target:  + 来添加target,可以选择工程中不同的target目标来添加 class: +来添加class, 可以按照不同的测试功能来分类,比如NormalFunctionTest, UI ...

  5. 愤怒的TryCatch

    本文地址:http://www.cnblogs.com/likeli/p/5719230.html 前言 本文不提供任何搭梯子之类的内容,我在这里仅仅讨论网络爬虫遇到的IP封杀,然后使用Tor如何对抗 ...

  6. 完全理解Gson(3):Gson反序列化

    完全理解Gson(2):Gson序列化 完全理解Gson(1):简单入门 本文延续前一篇文章,继续介绍简单基本的Gson用法.这篇文章我们将介绍如何将复杂的JSON对象解析为Java对象,其中Java ...

  7. apache几种限制ip的方法

    参考文档来源:  http://jingyan.baidu.com/article/4b07be3c193d1648b380f3a9.html1.  禁止访问某些文件/目录增加Files选项来控制,比 ...

  8. httpd配置文件中重写函数Rewrite

    [RewriteCond%{HTTP_HOST}^(www\.)?xxx\.com$] 这是重写条件,前面%{HTTP_HOST}表示当前访问的网址,只是指前缀部分,格式是www.xxx.com不包括 ...

  9. ISP模块之色彩增强算法--HSV空间Saturation通道调整 .

    色彩增强不同于彩色图像增强,图像增强的一般处理方式为直方图均衡化等,目的是为了增强图像局部以及整体对比度.而色彩增强的目的是为了使的原有的不饱和的色彩信息变得饱和.丰富起来.对应于Photoshop里 ...

  10. OpenCV入门笔记(三) 图片处理

    OpenCV中提供了非常多处理图片的强大函数,能够对非常多格式的图片 加特效.有点实现Photoshop里的工具的感觉. 以下先介绍一些简单和常见的特效. 二值化(Image Threshold) 參 ...