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


PROBLEM SETTER: JANE ALAM JAN
 

题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点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的更多相关文章

  1. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  2. Farthest Nodes in a Tree ---LightOj1094(树的直径)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...

  3. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  4. Farthest Nodes in a Tree (求树的直径)

    题目链接,密码:hpu Description Given a tree (a connected graph with no cycles), you have to find the farthe ...

  5. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

    1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...

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

  7. LightOJ 1094 - Farthest Nodes in a Tree(树的直径)

    http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...

  8. LightOJ1257 Farthest Nodes in a Tree (II)(树的点分治)

    题目给一棵树,边带有权值,求每一点到其他点路径上的最大权和. 树上任意两点的路径都可以看成是经过某棵子树根的路径,即路径权=两个点到根路径权的和,于是果断树分治. 对于每次分治的子树,计算其所有结点到 ...

  9. lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...

随机推荐

  1. delphi中的inpubox,如何能控制它的位置? 10

    https://zhidao.baidu.com/question/153270855.html delphi中的inpubox,如何能控制它的位置? 10 RT ! 前辈!最好你就把那代码都拿出来吧 ...

  2. phpmyadmin和网页上面的乱码问题

    前段时间做了个留言板,但是总是出现乱码,而且出现了无法插入的情况:发现这么个问题:在phpmyadmin里面默认的编码是瑞士的一个编码:我就郁闷了为什么这么一个通用软件的编码放着UTF-8或者是GBK ...

  3. C#实体类生成Create Table SQL

    using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...

  4. 利用python+tkinter做一个简单的智能电视遥控器

    要通过python实现遥控器功能分两步: 第一步:开发图形化界面,以暴风TV的遥控器按钮为例 第二步:使PC端给电视发送相应指令(此步骤需要打开电视的adb开关) 现在就开始第一步操作实现遥控器功能, ...

  5. linux下安装nginx(nginx(nginx-1.8.0.tar.gz),openssl(openssl-fips-2.0.9.tar.gz) ,zlib(zlib-1.2.11.tar.gz),pcre(pcre-8.39.tar.gz))

    :要按顺序安装: 1:先检查是否安装 gcc ,没有先安装:通过yum install gcc-c++完成安 2:openssl : tar -zxf  openssl-fips-2.0.9.tar. ...

  6. 执行 bower -v 时出现内部错误

    安装nodejs ,我的位置是D:\node.js_install.全局模块安装默认放在C:\Users\Administrator\AppData\Roaming\npm\node_modules里 ...

  7. [Python3] 011 字符串:给你们看看我的内置方法 第三弹

    目录 少废话,上例子 1. encode(encoding='utf-8', errors='strict') 2. expandtabs([tabsize=8]) 借此机会简单地说一说 print( ...

  8. Java-集合第六篇操作集合的工具类Collections

    1.Java提供了一个操作Set.List.Map等集合的工具类:Collections. 工具类中提供的方法主要针对Set.List.Map的排序.查询.修改等操作,以及将集合对象设置为不可变.对集 ...

  9. Java集合:Collection、List、Set、Map、泛型

    1.集合的理解和好处 2.集合的框架体系图 ★ 3.Collection接口的特点和使用 ★ 4.List和Set接口的特点和使用★ 5.List接口的实现类学习★ 6.Set接口的实现类学习★ 7. ...

  10. TP5.1+Vue前后端分离实践

    配置: 主域名 www.demo.xyz 二级子域名 api.demo.xyz 列表项目其中api.demo.xyz加入了版本控制,使用的是URL路由传入方式 在route.php路由文件中配置,如下 ...