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求一下, 第二种情况枚举删除的环边, 线段树 ...
随机推荐
- LintCode 190: Next Permutation
LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...
- 【BZOJ】2154: Crash的数字表格 莫比乌斯反演
[题意]给定n,m,求Σlcm(i,j),1<=i<=n,1<=j<=m,n,m<=10^7. [算法]数论(莫比乌斯反演) [题解] $$ans=\sum_{i\leq ...
- 使用Forms Authentication
using System; using System.Web; using System.Web.Security; namespace AuthTest { public class Aut ...
- canvas h5制作写字板
<!DOCTYPE html><html><head> <meta charset="utf-8"> <script type ...
- 从docker到docker-compose部署一个nginx+flask+mysql+redis应用
目的是把一个flask项目的mysql数据库.redis数据库.flask应用.nginx服务分别装到四个容器中,然后用docker-compose命令同时启动与关闭 一.安装docker Docke ...
- eclipse运行Android项目出现“The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. ”
重启eclipse之后仍然出现同样错误,此时可以尝试一下方法: cmd打开命令窗口: 之后重启eclipse,基本可以解决问题!
- Unity 添加鼠标右键事件
把此类放到 Editor下使用就OK using UnityEngine; using System.Collections; using System.Collections.Generic; us ...
- 41、和为S的连续正数序列
一.题目 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- 14 - 函数参数检测-inspect模块
目录 1 python类型注解 2 函数定义的弊端 3 函数文档 4 函数注解 4.1 annotation属性 5 inspect模块 5.1 常用方法 5.2 signature类 5.3 par ...
- linux用户修改用户shell
要拒绝系统用户登录,可以将其shell设置为/usr/sbin/nologin或者/bin/false # usermod -s /usr/sbin/nologin username 或者 # use ...