树的直径

给定n个点 n-1条边 和每条边的val

输出直径的大小和 直径上的点的序号

input:
8
1 2 2
1 3 1
1 5 10
2 4 3
4 6 4
3 7 5
7 8 2 output:
19
6 4 2 1 5

法1.DFS

思路:跑两遍dfs 第一次求出一个端点c1,第二次求出第二个端点c2

第一次:任意一个点跑一遍dfs 记录最远的点为c1 ,二次:这个时候dist[c1]=0 从c1开始跑dfs

缺点:不能处理存在val为负数的情况

#include <cstdio>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#define ep emplace_back #define lld long long
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);
#define vec vector
const int N = 2e5+9;
const int INF = 0x7FFFFFFF; //2147483647 const int inf1 = 0x3f3f3f3f; //1061109567
const int inf2 = 0x7f7f7f7f; //2139062143 memset赋值用 using namespace std; int c1,c2;//直径的两个端点
int c;//处理c1,c2用
int n;
int head[N],idx=0;
int dist[N];
int prenodes[N];
struct node{
int to,val,next;
};
node e[N];
void add(int u,int v,int val){
e[idx] = {v,val,head[u]};
head[u] = idx++;
}
void bd(){
cin>>n;
memset(head,-1,sizeof head);
for(int i=1 ; i<=n-1 ; ++i){
int u,v,val;
cin>>u>>v>>val;
add(u,v,val);
add(v,u,val);
}
}
void dfs(int u,int fa,int tag){
//tag==1才记录路径
for(int i=head[u] ; i!=-1; i=e[i].next){
int v = e[i].to;
int val = e[i].val;
if(v == fa) continue;
dist[v] = dist[u] + val;
if(tag==1)
prenodes[v] = u;
if(dist[v] > dist[c])
c=v;
dfs(v,u,tag);
}
} int main(){
ios;
bd();
dfs(1,0,0);
c1=c;
dist[c]=0;
dfs(c,0,1);
//记录第一个端点c1 = c 此时从c1开始 赋值dist[c1]=0;
c2=c;
cout<<dist[c]<<"\n";
for(int i=c2 ; i!=0 ;i=prenodes[i]) cout<<i<<" ";
return 0;
}

法2:树形的dp



#include <cstdio>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#define ep emplace_back #define lld long long
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);
#define vec vector
const int N = 2e5+9;
const int INF = 0x7FFFFFFF; //2147483647 const int inf1 = 0x3f3f3f3f; //1061109567
const int inf2 = 0x7f7f7f7f; //2139062143 memset赋值用 using namespace std; int n, d = 0;
int dp[N];
vector<int> E[N]; void dfs(int u, int fa) {
for (int v : E[u]) {
if (v == fa) continue;
dfs(v, u);
d = max(d, dp[u] + dp[v] + 1);
dp[u] = max(dp[u], dp[v] + 1);
}
} int main() {
scanf("%d", &n);
for (int i = 1; i <= n-1; i++) {
int u, v;
scanf("%d %d", &u, &v);
E[u].push_back(v);
E[v].push_back(u);
}
dfs(1, 0);
printf("%d\n", d);
return 0;
}

【模板】树的直径(dfs & dp)的更多相关文章

  1. 图论--树的直径--DFS+树形DP模板

    #include <iostream> #include <cstring> using namespace std; //maxv:源点能到的最远点,maxdis:最远点对应 ...

  2. 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][St ...

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

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

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

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

  5. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...

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

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

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

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

  8. HDU4514 湫湫系列故事——设计风景线 ——树的直径/树形dp+判环

    中文题面,给出一个图,问能不能成环,如果可以就输出YES.否则输出该树的直径. 这里的判环我们用路径压缩的并查集就能很快的判断出来,可以在输入的同时进行判断.这题重点就是求树的直径. 树直径的性质可以 ...

  9. 历届试题 大臣的旅费-(树的直径+dfs)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  10. 【lightoj-1094】树的直径(DFS)

    链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w, ...

随机推荐

  1. C# DateTime日期字段转中文文字

    public static String ToChineseYearAndMonth(this DateTime dt) { string[] chineseNumbers = { "零&q ...

  2. 开源云同步的markdown写作软件——Yosoro

    文章目录 前言 简便的项目管理 舒服的写作体验 支持one driver 存在缺点 前言 Yosoro是一款支持在Win.Linux.macOS上使用的写作软件.它的界面设计以及交互上表达出的极简主义 ...

  3. PPP协议简介

    转载出处:https://blog.csdn.net/csucxcc/article/details/1684416 PPP(Point-to-Point Protocol)协议是在SLIP的基础上发 ...

  4. 降维(三)LLE与其他降维技术

    LLE 局部线性嵌入,Locally Linear Embedding(LLE)是另一个功能强大的非线性降维(nonlinear dimensional reduction,NLDR)技术.它是一个流 ...

  5. 海思SDK 学习 :000-海思HI35xx平台软件开发快速入门之背景知识

    背景 参考自:<HiMPP V3.0 媒体处理软件开发参考.pdf> 由于在音视频处理领域,海思芯片占有全球市场的很大份额.当我们选择使用海思芯片开发时,程序开发模型主要是围绕HIMPP( ...

  6. xpath-猪八戒网服务商名称爬取

    import requests from lxml import etree url = 'https://changsha.zbj.com/xcxkfzbjzbj/f.html?fr=zbj.sy. ...

  7. 很好用的SSH工具FinalShell

    上图片:1.远程连接Linux 2.Linux:CentOS 3.虚拟机:

  8. Mysql实现主从复制(一主双从)

    一.环境介绍 LNMP(centos7,mysql5.6) vmware workstation pro配置了3个虚拟机,均安装了LNMP环境: master:  192.168.0.105 slav ...

  9. C# LINQ之IEqualityComparer<>接口应用

    在C#语言中,对集合的条件查询.分组统计等操作使用LINQ非常方便,LINQ的语法格式与SQL非常相似和便捷,而LINQ扩展方法配合Lambda更为简洁,如All.Any.Count.Max等Enum ...

  10. Aug. 2024 杭二训练游记

    \(\text{前言}\) 我在 \(\text{Aug. 6th}\) 到 \(\text{Aug. 25th}\) 在杭州某知名中学集训,但是我亲爱的母亲却在一开始告诉我是 \(\text{Aug ...