Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\)
题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗树,并最小化删掉环边后的树的直径。
\(n \leq 2 \times 10^5\) 树的边权 $ \leq 10^9 $
解题思路 :
考虑最终树的直径可能由两部分组成,答案是其中的最大值
第一种就是外向树内的直径的最大值,这个只需要随便\(dp\)一下即可,这里不过多讨论
第二种情况树的直径经过原来的环,必然是环上的一段加上两端对应的外向树的 \(maxdep\)
设 \(l_i\) 为将环展开后环上第 \(i\) 个点到环左端点的距离,\(dep_i\) 为 环上第 \(i\) 个点对应的外向树的最大深度
设选取的环的左端点为 \(x\) 右端点为 \(y\) ,那么经过环上 \(x \rightarrow y\) 一段的树的直径就是可以表示为
\(l_y - l_x + dep_x + dep_y\)
问题自此转化为对于每一种把环展开的方式,求 \(\max(l_y - l_x + dep_x + dep_y)\)
即 \(\max(l_y + dep_y) - \min(l_x - dep_x) \ y \neq x\) 用两个\(set\) 分别维护即可
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + Qch - 48;
if(f) x = -x;
}
#define int ll
#define N (600005)
int a[N], b[N], head[N], nxt[N], cnt;
int st[N], ct[N], vis[N], success;
int g[N], dep[N], c[N], s[N], tot, top, n;
struct Node{
int val, id;
bool operator < (const Node &A) const{ return val < A.val; }
};
multiset<Node> s1, s2;
inline void add(int x, int y, int z){
a[++cnt] = y, b[cnt] = z; nxt[cnt] = head[x], head[x] = cnt;
}
inline void Getcircle(int u, int fa){
vis[u] = 1;
for(int p = head[u]; p; p = nxt[p]){
int v = a[p];
if(v == fa) continue;
if(!vis[v]){
st[++top] = v, ct[top] = b[p];
Getcircle(v, u);
}
else{
int pos = 1;
for(int i = 1; i <= top; i++)
if(st[i] == v){ pos = i; break; }
for(int i = pos; i <= top; i++)
c[++tot] = st[i], s[tot] = ct[i];
s[1] = b[p];
return (void) (success = 1);
}
if(success) return;
}
if(success) return; vis[u] = 0, top--;
}
inline int dfs(int u, int fa){
int mx = u;
for(int p = head[u]; p; p = nxt[p]){
int v = a[p];
if(v != fa && !vis[v]){
dep[v] = dep[u] + b[p];
int now = dfs(v, u);
if(dep[now] > dep[mx]) mx = now;
}
}
return mx;
}
inline int dfs2(int u, int fa, int dis, int t){
int res = dis;
for(int p = head[u]; p; p = nxt[p]){
int v = a[p];
if(v != fa){
if(vis[v] && t) continue;
if(!vis[v]){
int now = dfs2(v, u, dis + b[p], t);
if(now > res) res = now;
}
else{
int now = dfs2(v, u, dis + b[p], 1);
if(now > res) res = now;
}
}
}
return res;
}
inline ll calc1(){
multiset<Node>::iterator it2 = s2.begin();
int id = it2 -> id;
s1.erase(s1.find((Node){s[id] + g[id], id}));
multiset<Node>::iterator it1 = s1.end(); it1--;
int ans = it1 -> val - it2 -> val;
s1.insert((Node){s[id] + g[id], id});
return ans;
}
inline ll calc2(){
multiset<Node>::iterator it1 = s1.end(); it1--;
int id = it1 -> id;
s2.erase(s2.find((Node){s[id] - g[id], id}));
multiset<Node>::iterator it2 = s2.begin();
int ans = it1 -> val - it2 -> val;
s2.insert((Node){s[id] - g[id], id});
return ans;
}
inline ll calc(){ return max(calc1(), calc2()); }
main(){
read(n);
if(n <= 2) return puts("0"), 0;
for(int i = 1, x, y, z; i <= n; i++){
read(x), read(y), read(z);
add(x, y, z), add(y, x, z);
}
ll ans = 0;
st[++top] = 1, Getcircle(1, 0);
for(int i = 1; i <= n; i++) vis[i] = 0;
for(int i = 1; i <= tot; i++) vis[c[i]] = 1;
for(int i = 1; i <= tot; i++){
int mx = dfs(c[i], 0);
g[i] = dep[mx], ans = max(ans, g[i]);
ans = max(ans, dfs2(mx, 0, 0, mx == c[i] ? 1 : 0));
}
for(int i = 1; i <= tot; i++) g[i+tot] = g[i];
for(int i = 1; i <= tot; i++) s[i+tot] = s[i];
tot *= 2;
for(int i = 1; i <= tot; i++) s[i] += s[i-1];
for(int i = 1; i <= tot / 2; i++){
s1.insert((Node){s[i] + g[i], i});
s2.insert((Node){s[i] - g[i], i});
}
ll tmp = calc();
for(int i = 2; i <= tot / 2; i++){
int l = i, r = i + tot / 2 - 1;
s1.insert((Node){s[r] + g[r], r});
s2.insert((Node){s[r] - g[r], r});
s1.erase(s1.find((Node){s[l-1] + g[l-1], l - 1}));
s2.erase(s2.find((Node){s[l-1] - g[l-1], l - 1}));
tmp = min(tmp, calc());
}
cout << Max(ans, tmp);
return 0;
}
Codeforces 835 F. Roads in the Kingdom的更多相关文章
- 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 ...
- 【CodeForces】835F Roads in the Kingdom
一.题目 题目描述 王国有\(n\)座城市与\(n\)条有长度的街道,保证所有城市直接或间接联通,我们定义王国的直径为所有点对最短距离中的最大值,现因财政危机需拆除一条道路并同时要求所有城市仍然联通, ...
- Codeforces 835F Roads in the Kingdom (环套树 + DP)
题目链接 Roads in the Kingdom 题意 给出一个环套树的结构,现在要删去这个结构中的一条边,满足所有点依然连通. 删边之后的这个结构是一棵树,求所有删边情况中树的直径的最小值. 显 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 835F Roads in the Kingdom - 动态规划
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...
- codeforces:Roads in the Kingdom分析和实现
题目大意:国家有n个城市,还有n条道路,每条道路连通两个不同的城市,n条道路使得所有n个城市相互连通.现在国家经费不足,要关闭一条道路.国家的不便度定义为国家中任意两个不同的城市之间的距离的最大值,那 ...
- Roads in the Kingdom CodeForces - 835F (直径)
大意: 给定一个基环树, 求删除一条环上的边使得直径最小. 直径分两种情况 环上点延伸的树内的直径 两个环上点的树内深度最大的点匹配 第一种情况直接树形dp求一下, 第二种情况枚举删除的环边, 线段树 ...
随机推荐
- 引用类型 ( 对象定义 )——Object 类型
本文地址:http://www.cnblogs.com/veinyin/p/7607100.html 创建实例 new 操作符后跟构造函数 var people = new Object(); pe ...
- PHP脚本运行时间
http://www.cnblogs.com/zqifa/p/php-16.html PHP设置脚本最大执行时间的三种方法 php.ini 中缺省的最长执行时间是 30 秒,这是由 php.ini 中 ...
- 【leetcode 简单】第十一题 搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输 ...
- react 修改state某一属性值
1.state // 筛选框相关数据 searchSelect: { term: { value: '学期', key: '', options: [] }, type_of_personnel: { ...
- bzoj 1072 状压DP
我们用w[i][j]来表示,i是一个二进制表示我们选取了s中的某些位,j表示这些位%d为j,w[i][j]则表示这样情况下的方案数,那么我们可以得到转移.w[i|(1<<k)][(j*10 ...
- jQuery()方法的第二个参数详解
关于jQuery()方法的第二个参数,有下面这几种用法: 1.jQuery(selector, [context]) 这种用法,相当于 $(context).find(selector) 或者 con ...
- idea 控制台乱码
第一步:修改intellij idea配置文件: 找到intellij idea安装目录,bin文件夹下面idea64.exe.vmoptions和idea.exe.vmoptions这两个文件,分别 ...
- 多维尺度变换MDS(Multidimensional Scaling)
流形学习(Manifold Learning)是机器学习中一大类算法的统称,流形学习是非线性的降维方法(an approach to non-linear dimensionality reducti ...
- puppet practice
目标 试验环境有两台主机(VM)构成,一台是master,一台是agent,完成以下工作: 新建用户newuser; 安装 ubuntu-cloud-keyring package,更改文件/etc/ ...
- java经典面试题大全
基本概念 操作系统中 heap 和 stack 的区别 什么是基于注解的切面实现 什么是 对象/关系 映射集成模块 什么是 Java 的反射机制 什么是 ACID BS与CS的联系与区别 Cookie ...