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求一下, 第二种情况枚举删除的环边, 线段树 ...
随机推荐
- 【CodeForces】913 C. Party Lemonade
[题目]C. Party Lemonade [题意]给定n个物品,第i个物品重量为2^(i-1)价值为ci,每个物品可以无限取,求取总重量>=L的最小代价.1<=30<=n,1< ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- input复选框checkbox默认样式纯css修改
修改之前的样式 修改之后的样式 html <input type="checkbox" name="btn" id="btn1"&g ...
- 线程句柄和线程ID的区别
●CreateThread() API 用于创建线程. API 返回同时线程句柄,并通过参数得到线程标识符 (ID). 线程句柄有完全访问权创建线程对象. 运行线程时线程 ID 唯一标识线程在系统级别 ...
- VueJS $refs 在 ElementUI 中遇到的问题
表单验证的时候 $refs 拿不到 暂且是用 $nextTick 解决,具体原因有待研究 假入在 created 中注册时间来验证 validate,那就放在mounted中 或者...注册了 ev ...
- tyvj P1050 最长公共子序列
题目链接:http://tyvj.cn/p/1050 题解: 裸题,只是为了测试LCS模板写对没有…… #include<cstdio> #include<cstring> # ...
- Linux时间结构体和获得时间函数
关于Linux下时间编程的问题: 1. Linux下与时间有关的结构体 struct timeval { int tv_sec; int tv_usec; }; 其中tv_sec是由凌晨开始算起的秒数 ...
- Codefroces 735D Taxes(哥德巴赫猜想)
题目链接:http://codeforces.com/problemset/problem/735/D 题目大意:给一个n,n可以被分解成n1+n2+n3+....nk(1=<k<=n). ...
- Codeforces 821C Okabe and Boxes(模拟)
题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...
- (一) Mysql 简介及安装和配置
第一节:Mysql 简介 百度百科 第二节:Mysql 安装及配置 1,Mysql5.1 下载及安装 2,Mysql 数据库编码配置 utf-8 3,Mysql 图形界面 Sqlyog 下载及安装