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 ...
随机推荐
- django中间件(获取请求ip)
def simple_middleware(get_response): # 此处编写的代码仅在Django第一次配置和初始化的时候执行一次. print('1----django启动了') def ...
- Beetle简单构建TCP服务
使用Beetle构建TCP服务应用是件非常简单的事情,它并不需要你去关注Socket细节,如果你想用Socket编写高性能的TCP服务,那你要关注的东西非常多,异步数据处理,大量连接下的线程管理和连接 ...
- 你还没有真正理解的innodb_flush_log_at_trx_commit
关于innodb_flush_log_at_trx_commit的描述,看了mysql手册中的解释,感觉都不够清晰明了,下面试图以最简单直白的方式解释一下innodb_flush_log_at_trx ...
- 【ABAP系列】SAP ABAP 关于ALV布局保存选项的讲解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 关于ALV布局保存 ...
- fastdfs集群安装过程_学习笔记
最终效果 初始化为6个节点 在/usr/local/software 目录下上传需要用到tar包,分别在各个节点上传 使用scp 将本地software目录复制到其他节点上 73.74为 tracke ...
- Developer Express 第三方控件使用系列方法
本人目前从事的开发工作主要是以C#语言进行的相关C/S的开发,在工作中也要求使用Developer Express第三方控件所以这一系列的控件使用说明都将以C#语言进行代码说明.平时工作中会慢慢的收集 ...
- java _static 关键字
• 在类中,用static声明的成员变量为静态成员变量 ,或者叫做: 类属性,类变量. • 它为该类的公用变量,属于类,被该类的所有实例共享,在类被载入时被显式初始化, • 对于该类的所有对象来说,s ...
- [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (5/5)
- 在eclipse里搜索maven项目需要的dependency
eclipse直接就可以通过下载同步仓库索引,直接关键字查询需要的dependency. 前提是你已经在你的eclipse上配好了maven正确的环境. 1. 设置在开启eclipse时下载同步仓库索 ...
- nginx报错[error] CreateFile() "D:\Java-windows\nginx-1.16.0/logs/nginx.pid" failed (2: The system cannot find the file specified)
无论是nginx -s stop还是nginx -s reload命令,都会出现这个错误. 解决方法:使用命令创建/logs/nginx.pid文件,命令如下所示: nginx -c conf/ngi ...