lightoj1094 - Farthest Nodes in a Tree
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.
Output
For each case, print the case number and the maximum distance.
Sample Input |
Output for Sample Input |
2 4 0 1 20 1 2 30 2 3 50 5 0 2 20 2 1 10 0 3 29 0 4 50 |
Case 1: 100 Case 2: 80 |
Notes
Dataset is huge, use faster i/o methods.
题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点index,然后找离index最远的点就是最大距离?
假定0为根节点找离0最远的点就是,最深的点index,然后找离index最远的点就是树上最远的距离
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define N 30008 struct node
{
int v, w, next;
}e[N*]; int n, cnt, maxx;
int Index; // 写成index过不了lightoj=·=||
int head[N], dist[N]; void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void dfs(int u, int w)
{
dist[u] = w;
if(w > maxx)
{
maxx = dist[u];
Index = u;
}
for(int i = head[u]; i != -; i = e[i].next)
{
if(dist[e[i].v] == -)
{
dfs(e[i].v, dist[u]+e[i].w);
}
}
}
int main()
{
int t, u, v, w, k = ; scanf("%d", &t); while(t--)
{
cnt = maxx = ;
memset(head, -, sizeof(head)); scanf("%d", &n);
n--;
while(n--)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
memset(dist, -, sizeof(dist));
dfs(, );
memset(dist, -, sizeof(dist));
dfs(Index, );
printf("Case %d: %d\n", k++, maxx);
}
return ;
}
bfs
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define N 30008 struct node
{
int v, w, next;
} e[N*]; int n, cnt, maxx;
int Index;
int head[N], dist[N], vis[N]; void addedge(int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void bfs(int u)
{
memset(vis, , sizeof(vis));
queue<int> Q;
Q.push(u);
vis[u] = ;
dist[u] = ; while(Q.size())
{
u = Q.front();
Q.pop(); for(int i = head[u]; i != -; i = e[i].next)
{
int v = e[i].v;
if(!vis[v])
{
vis[v] = ;
dist[v] = dist[u]+e[i].w;
if(dist[v] > maxx)
{
maxx = dist[v];
Index = v; }
Q.push(v);
}
}
}
} int main()
{
int t, u, v, w, k = ;
scanf("%d", &t); while(t--)
{
maxx = cnt = ;
memset(head, -,sizeof(head)); scanf("%d", &n);
n--; while(n--)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
memset(dist, , sizeof(dist));
bfs();
bfs(Index);
printf("Case %d: %d\n", k++, maxx);
}
return ;
}
lightoj1094 - Farthest Nodes in a Tree的更多相关文章
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Farthest Nodes in a Tree (求树的直径)
题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- E - Farthest Nodes in a Tree
Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. Th ...
- LightOJ 1094 - Farthest Nodes in a Tree(树的直径)
http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...
- LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)
题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
随机推荐
- Iview 启动报错 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
解决 fs.write(fd, buf, 0, buf.length, 0, function(err, written, buffer) {}); 替换为 fs.write(fd, buf, 0, ...
- 【python+selenium自动化】基于Autolt实现上传
在UI自动化过程中,总会遇到文件上传的操作,一般的,标签为input,可以直接使用sendkeys 如果他仅仅是一个button,那则无法直接sendkeys,则需要用到autoIT这个工具 基于Au ...
- 浅谈矩阵加速——以时间复杂度为O(log n)的算法实现裴波那契数列第n项及前n之和使用矩阵加速法的优化求法
首先请连矩阵乘法乘法都还没有了解的同学简单看一下这篇博客: https://blog.csdn.net/weixin_44049566/article/details/88945949 首先直接暴力求 ...
- Angular5 错误: ngModel cannot be used to register form controls with a parent formGroup directive
在创建一个表单时,出现了这样的错误: 原因是,在最外层的form中使用了 formGroup 指令,但在下面的某个input 元素中,使用了ngModel 指令,但没有加入formControl 指令 ...
- python+selenium下拉列表option对象操作方法二
options = driver.find_elements_by_tag_name('option') #获取所有的option子元素 o ...
- 解决浏览器打开网页后提示“dns_probe_possible”的方法
使用浏览器浏览网页时偶尔会遇到无法上网且浏览器提示:DNS_PROBE_POSSIBLE 一般有三种情况会导致这样的故障: 1.网络协议出现故障,也就是常说的 DNS 设置问题 2.浏览器中设置问题, ...
- Vue.js 源码学习笔记
最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ...
- Billboard 题解 hdu2795
Billboard 题解 hdu2795 题意 有个广告牌,上面需要依次贴广告,广告的高度均为1,但是宽度不同,每次都想贴在最靠左上的位置,按照顺序进行广告的话,输出每个广告位于广告牌的高度. 解题思 ...
- Vert.x学习第一天
昨天看了下异步,然后就开始了Vert.x相关知识的学习. Vert.x是当下非常流行的一套全异步框架,其优势在于轻量级.高效.非常适合作为移动端后台或是企业应用. 当然对于第一天接触这个框架的人(没错 ...
- 手写ORM持久层框架(转)
工程结构: 本文测试的数据库为: 其中student的表结构为: 表数据: 配置文件 DB2.properties driver=com.mysql.jdbc.Driver url=jdbc\:mys ...