http://acm.hust.edu.cn/vjudge/contest/121398#problem/H

不是特别理解,今天第一次碰到这种问题。给个链接看大神的解释吧

http://www.cnblogs.com/qq2424260747/p/4740347.html

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm> using namespace std;
typedef long long LL; const int maxn=;
const int INF=0x3f3f3f3f;
const int mod=;
int head[maxn], dist[maxn];
int maxs, k, Index; struct node
{
int u, v, next, s;
}edge[maxn<<]; void Add(int u, int v, int s)
{
edge[k].u = u;
edge[k].v = v;
edge[k].s = s;
edge[k].next = head[u];
head[u] = k ++;
} void DFS(int u, int s)
{
dist[u] = s; if(dist[u]>maxs)
{
maxs = dist[u];
Index = u;
} for(int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].v;
if(dist[v] == -)
DFS(v, edge[i].s+dist[u]);
}
} int main()
{
int T, n, u, v, s, cnt=; scanf("%d", &T); while(T --)
{
scanf("%d", &n); memset(head, -, sizeof(head));
k = ; for(int i=; i<n; i++)
{
scanf("%d %d %d", &u, &v, &s);
Add(u, v, s);
Add(v, u, s);
} maxs = ; memset(dist, -, sizeof(dist));
DFS(, ); memset(dist, -, sizeof(dist));
DFS(Index, ); printf("Case %d: %d\n", cnt++, maxs);
}
return ;
}

LightOJ 1094 - Farthest Nodes in a Tree(树的直径)的更多相关文章

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

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

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

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

  3. LightOJ 1094 - Farthest Nodes in a Tree

    http://lightoj.com/volume_showproblem.php?problem=1094 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

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

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

  5. LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)

    Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...

  6. lightoj1094 - Farthest Nodes in a Tree

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

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

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

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

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

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

随机推荐

  1. Hadoop的I/O操作

    HDFS的数据完整性 检验数据是否损坏最常见的措施是:在数据第一次引入系统时计算校验和并在数据通过一个不可靠通道进行传输时再次计算校验和,这样就能发现数据是否被损坏.HDFS会对写入的所有数据计算校验 ...

  2. iOS 定位于地理反编码

    - (void)viewDidLoad { [self startLocation]; } //开始定位 -(void)startLocation{ self.locationManager = [[ ...

  3. SpringMVC处理静态资源

    若将DispatcheServlet请求映射设置为/,则SpringMvc将捕获WEB容器的所有请求,包括静态资源的请求,SpringMvc会将它们当成一个普通的请求处理,那么将会出现因找不到对应的处 ...

  4. 报错:HTTP Status 404 - There is no Action mapped for namespace [/] and action name [product-save] associated with context path [/20161101-struts2-2].

    运行:index.jsp---->input.jsp----->details.jsp,但是在input.jsp到details.jsp的时候报错误. 异常如下: 严重: Could no ...

  5. flex loaderInfo为null在creationComplete事件中

    原文: http://yunzhongxia.iteye.com/blog/1152670   Flex4中application变为FlexGlobals.topLevelApplication,很 ...

  6. sql 数据量高并发的数据库优化(转)

    Mysql 大数据量高并发的数据库优化 一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实 ...

  7. elasticsearch知识点

    1.分析:数据转化的过程. 两个转化过程-----传入文档中的数据转化程倒排序索引 -----查询文本转化成可被搜索的词 2.分析器:承担分析(数据转化)的工作 组成:一个分词器(tokenizer) ...

  8. MySQL中四舍五入的实现

    MySQL四舍五入的实现   文章主要描述的是MySQL四舍五入的实际应用, 以及在其实际操作中的值得我们大家注意的事项与其实际应用代码的描述,以下就是文章的主要内容的详细描述,望大家在浏览之后会对其 ...

  9. LeetCode Implement pow(x, n).

    这个题目我也没有思路,同学们可以查看这个http://www.cnblogs.com/NickyYe/p/4442867.html 下面是我改进后的代码 第一种方法: class Solution { ...

  10. python 向上取整ceil 向下取整floor 四舍五入round

    #encoding:utf-8 import math #向上取整 http://www.manongjc.com/article/1335.html print "math.ceil--- ...