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. 知识积累:CGI,FastCGI,PHP-CGI与PHP-FPM

    CGICGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上.CGI可以用任何一种语 ...

  2. [转]MySQL去除查询结果重复值

    在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...

  3. Fatal error: Call to undefined function imagettftext()解决办法

    Fatal error: Call to undefined function imagettftext()解决办法   我的问题是php编译安装时指定了gd的目录,其实不用指定.就可以了 博客分类: ...

  4. 【xargs使用】查询包含某字符串的所有文件

    在initrd目录下,查找包含"Loading virtio.ko"的所有文件 cd initrd find . | xargs grep "Loading virtio ...

  5. 使用jquery的delay方式模拟sleep

    javascript中并没有原生sleep函数可供调用,一般来说为了实现sleep功能,大都是采用SetTimeout来模拟,以下片段采用jquery的delay方法来模拟,也算是提供了另外一个视角吧 ...

  6. Bellman算法

    Bellman算法 当图有负圈的时候可以用这个判断最短路! [时间复杂度]O(\(nm\)) &代码: #include <bits/stdc++.h> using namespa ...

  7. java基础要点

    Java语言是现在比较常用的编程语言,因为Java语言可以编写桌用程序,也可以编写web程序,还能编写嵌入式程序.这是由于相比脚本语言,Java 的运行速度要快.而对于底层语言,Java与平台无关,可 ...

  8. ASP.NET Page对象各事件执行顺序(转)

    很久没写 asp.net 的东西了,search 了一下 page 的事件执行顺序,找到如下的东西,仅仅做记录用 Page.PreInit 在页初始化开始时发生 Page.Init 当服务器控件初始化 ...

  9. JS-改变页面的颜色(三)

    需求:点击页面的按钮,改变页面的颜色 思路:一先画出最简单的页面,二想办法获取页面的body节点,三想办法修改body节点的背景颜色属性,四通过一个方法获取随机的颜色值           和第二个例 ...

  10. nodejs将PDF文件转换成txt文本,并利用python处理转换后的文本文件

    目前公司Web服务端的开发是用Nodejs,所以开发功能的话首先使用Nodejs,这也是为什么不直接用python转换的原因. 由于node对文本的处理(提取所需信息)的能力不强,类似于npm上的包: ...