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. MySQL性能优化总结(转)https://yq.aliyun.com/articles/24249

    摘要: 一.MySQL的主要适用场景 1.Web网站系统 2.日志记录系统 3.数据仓库系统 4.嵌入式系统 二.MySQL架构图:   三.MySQL存储引擎概述 1)MyISAM存储引擎 MyIS ...

  2. Func<T>与Action<T>委托泛型介绍

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

  3. 【python】类中__slots__使用

    __slots__作用:限制类的属性,只给实例绑定任何属性和方法 如果我们想要限制class的属性怎么办?比如,只允许对Student实例添加name和age属性. 为了达到限制的目的,Python允 ...

  4. Hadoop学习10--常用命令记录帖

    1.列出所有正在执行的任务 hadoop job -list 2.kill任务 hadoop job -kill 任务名

  5. xcode 打静态库.a文件

    原文地址:http://blog.csdn.net/pjk1129/article/details/7255163 核心命令:lipo -info 地址.查看支持的类型,如armv7 lipo -cr ...

  6. 使用asmcmdcp命令把datafile从文件系统移动(move)到asm磁盘组中 针对11gR2

    使用asmcmd cp命令 把datafile从文件系统移动(move)到asm磁盘组中--针对11gR2 参考原文:How to Move a Datafile from Filesystem to ...

  7. Lua 5.1 参考手册

    Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 云风 译 www.codingno ...

  8. noip2007解题报告

    T1.统计数字 给出n个数,统计每个数字出现的个数. n小,快排解决. T2.字符串的展开 给出一个字符串,其中形如 d-h,4-9之类的就展开,(前面比后面小的保留,相等也是),三个参数,P1表示大 ...

  9. 纸上谈兵:队列(queue)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 队列(queue)是一个简单而常见的数据结构.队列也是有序的元素集合.队列最大的特 ...

  10. SQLite数据库文件格式

    数据库命名约定 sqlite3_open()API用到数据库的文件名,可以是相对当前工作目录的相对路径名,也可以是从系统根文件树开始的完整路径名.任何被本地文件系统接受的正规文件名都是好的. 如果文件 ...