\(>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的更多相关文章

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

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

  2. 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 ...

  3. 【CodeForces】835F Roads in the Kingdom

    一.题目 题目描述 王国有\(n\)座城市与\(n\)条有长度的街道,保证所有城市直接或间接联通,我们定义王国的直径为所有点对最短距离中的最大值,现因财政危机需拆除一条道路并同时要求所有城市仍然联通, ...

  4. Codeforces 835F Roads in the Kingdom (环套树 + DP)

    题目链接 Roads in the Kingdom 题意  给出一个环套树的结构,现在要删去这个结构中的一条边,满足所有点依然连通. 删边之后的这个结构是一棵树,求所有删边情况中树的直径的最小值. 显 ...

  5. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  6. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  7. Codeforces 835F Roads in the Kingdom - 动态规划

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...

  8. codeforces:Roads in the Kingdom分析和实现

    题目大意:国家有n个城市,还有n条道路,每条道路连通两个不同的城市,n条道路使得所有n个城市相互连通.现在国家经费不足,要关闭一条道路.国家的不便度定义为国家中任意两个不同的城市之间的距离的最大值,那 ...

  9. Roads in the Kingdom CodeForces - 835F (直径)

    大意: 给定一个基环树, 求删除一条环上的边使得直径最小. 直径分两种情况 环上点延伸的树内的直径 两个环上点的树内深度最大的点匹配 第一种情况直接树形dp求一下, 第二种情况枚举删除的环边, 线段树 ...

随机推荐

  1. IO流-读取写入缓冲区

    例如FileReader和FileWriter在读取的时候是读一次或者写一次就请求磁盘,这样使用的时间非常的长,效率比较低,因此引入BufferedReader和BufferedWriter作为读取和 ...

  2. node、npm及node_modules中依赖的版本更新

    好久没用node了,想重新拾起来发现node还有相关模块的版本都太低了,使用npm install全是报版本低的警告. 这里记录一下,版本管理和node_modules更新的方法. 我用的是Windo ...

  3. js_网页导出pdf文件

    打印当前页面,一开始我认为是需要输出pdf的,后来了解的需求是能够打印就可以了.需求既然都研究了,记录下. 更好的打印方式,window.print();会弹出打印对话框,打印的是window.doc ...

  4. hdu 2063 过山车 二分匹配(匈牙利算法)

    简单题hdu2063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Ot ...

  5. Java八种基本类型

    boolean 二进制位: true ,false   byte 二进制位:8 -128 - 127   -2的7次方到2的7次方-1 char 二进制位:16 0 - 65535   short 二 ...

  6. 安装ssh-keygen

    转载自:http://www.daoan.com/forums/index.php?forumid=5&mods=topicdisplay&postid=4 sudo apt-get ...

  7. 64_t4

    texlive-hardwrap-svn21396.0.2-33.fc26.2.noarch.rpm 24-May-2017 15:41 35930 texlive-harmony-doc-svn15 ...

  8. selenium只打开一个浏览器窗口

    from selenium.webdriver import Remote from selenium.webdriver.chrome import options from selenium.co ...

  9. 62.Unique Paths---dp

    题目链接 题目大意:给一个m*n的方格,从左上角走到右下角,中间无任何障碍,问有多少种走法. 法一:DFS,超时,简单模板深搜,无任何剪枝,结果一半的数据超时.代码如下: public int uni ...

  10. Ubuntu 10.04 分辨率调整

    最近学长们看了我的本本都在问我,显卡驱动是不是出现什么问题了···分辨率这么差.当时我的分辨率是1024X768,于是我就想修改我的屏幕分辨率改成1280X800.本来很简单的事情,我做起来却非常的曲 ...