题目链接

bzoj3124: [Sdoi2013]直径

题解

发现所有直径都经过的边

一定在一条直径上,并且是连续的

在一条直径上找这段区间的两个就好了

代码

#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gc getchar()
#define pc putchar
#define int long long
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9') { if(c == '-') f =- 1;c = gc;}
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
int n;
const int maxn = 400007;
struct node {
int v,w,next;
} edge[maxn << 1];
int head[maxn],num = 0;
inline void add_edge(int u,int v,int w) {
edge[++ num].v = v; edge[num].w = w; edge[num].next = head[u]; head[u] = num;
}
int st,mx,dis[maxn],fa[maxn];
void dfs(int x,int Fa) {
if(dis[x] > mx) {
mx = dis[x]; st = x;
}
fa[x] = Fa;
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
if(v == Fa) continue;
dis[v] = dis[x] + edge[i].w;
dfs(v,x);
}
}
int chain[maxn];
int rdis[maxn],MX;
bool vis[maxn];
void rdfs(int x,int Fa) {
vis[x] = true;
MX = std::max(MX,rdis[x]);
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
if(vis[v] || v == Fa) continue;
rdis[v] = rdis[x] + edge[i].w;
rdfs(v,x);
}
}
main() {
//freopen("3.in","r",stdin);
n = read();
for(int u,v,w,i = 1;i < n;++ i) {
u = read(),v = read(),w = read();
add_edge(u,v,w);
add_edge(v,u,w);
}
dfs(1,0);
dis[st] = 0;
mx = 0;
dfs(st,0);
int len = 0;
while(st) {
chain[++ len] = st;
vis[st] = true;
st = fa[st];
}
int l = len,r = 1;
for(int i = len;i >= 1;-- i) {
MX = 0;
rdfs(chain[i],0);
if(!MX) continue;
if(MX == dis[chain[i]]) l = i;
if(MX == mx - dis[chain[i]]) {r = i;break;}
}
print(mx);
pc('\n');
print(l - r);
}
/*
6
3 1 1000
1 4 10
4 2 100
4 5 50
4 6 100
*/


bzoj3124: [Sdoi2013]直径 树形dp two points的更多相关文章

  1. BZOJ3124: [Sdoi2013]直径 (树形DP)

    题意:给一颗树 第一问求直径 第二问求有多少条边是所有直径都含有的 题解:求直径就不说了 解第二问需要自己摸索出一些性质 任意记录一条直径后 跑这条直径的每一个点  如果以这个点不经过直径能到达最远的 ...

  2. 【BZOJ3124】[Sdoi2013]直径 树形DP(不用结论)

    [BZOJ3124][Sdoi2013]直径 Description 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节 ...

  3. [SDOI2013] 直径 - 树形dp

    对于给定的一棵树,其直径的长度是多少,以及有多少条边满足所有的直径都经过该边. Solution 有点意思 先随便求一条直径(两次DFS即可),不妨设为 \(s,t\),我们知道要求的这些边一定都在这 ...

  4. 2014 Super Training #9 E Destroy --树的直径+树形DP

    原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...

  5. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

  6. [10.12模拟赛] 老大 (二分/树的直径/树形dp)

    [10.12模拟赛] 老大 题目描述 因为 OB 今年拿下 4 块金牌,学校赞助扩建劳模办公室为劳模办公室群,为了体现 OI 的特色,办公室群被设计成了树形(n 个点 n − 1 条边的无向连通图), ...

  7. Codeforces 633F 树的直径/树形DP

    题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...

  8. bzoj千题计划134:bzoj3124: [Sdoi2013]直径

    http://www.lydsy.com/JudgeOnline/problem.php?id=3124 第一问: dfs1.dfs2 dfs2中记录dis[i]表示点i距离最长链左端点的距离 第二问 ...

  9. 2018.11.05 bzoj3124: [Sdoi2013]直径(树形dp)

    传送门 一道sbsbsb树形dpdpdp 第一问直接求树的直径. 考虑第二问问的边肯定在同一条直径上均是连续的. 因此我们将直径记下来. 然后对于直径上的每一个点,dpdpdp出以这个点为根的子树中不 ...

随机推荐

  1. Forget Guava: 5 Google Libraries Java Developers Should Know

    Forget Guava: 5 Google Libraries Java Developers Should Know Published on 2016 7 13 Somenath PandaFo ...

  2. CodeForces 70

    题目 A题 #include<bits/stdc++.h> using namespace std; int n,b,sum; int main(){ scanf("%d&quo ...

  3. 3537. 【NOIP2013提高组day2】华容道(搜索 + 剪枝)

    Problem 给出一个类似华容道的图.\(q\)次询问,每次给你起始点,终止点,空格位置,让你求最少步数 \(n,m\le 30, q\le 500\). Soultion 一道智障搜索题. 弱智想 ...

  4. 分布式监控系统开发【day38】:主机存活检测程序解析(七)

    一.目录结构 二.入口 1.文件MonitorServer.py import os import sys if __name__ == "__main__": os.enviro ...

  5. Java String相关

    一.String类的常用方法 1. int indexOf(String s) 字符串查找 2. int lastIndexOf(String str) 3. char charAt(int inde ...

  6. SCI,EI,ISTP

    SCI:   Science Citation Index EI:     The Engineering Index ISTP:  Index to Scientific & Technic ...

  7. CF611D lcp+dp

    本篇博客只是留个辣鸡的自己标记一下,误入的同学请出门左转博客 https://blog.csdn.net/loy_184548/article/details/50865777 代码神马的也是复制啊 ...

  8. Best Practice API

    # 建议直接使用的第三方类 Common Lang =>StringUtils =>Validate Guava =>Cache =>Ordering JDK7(LTS JDK ...

  9. webpack构建Vue工程

    先开始webpack基本构建   创建一个工程目录 vue-structure mkdir vue-structure && cd vue-structure   安装webpack ...

  10. window开发环境常用操作

    1. 启动redis命令 redis-server redis.windows.conf 如果法正常启用,出现如下问题 windows下第一次通过以下命令启动redis (*:此处整理转载自:http ...