链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/C

求树的直径,这里只不过给每条边增加一个边长属性,变成了求树上两点距离最远为多少

树的直径,在今天比赛前我是没有接触过的, 队友说在学长的博客上看是用了两个bfs就可以了,可我看的题解是用了两个dfs

应该意思是一样的吧!!! 都学习一下

题解的代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std; #define N 30005 struct node
{
int v, next, w;
}edge[N<<]; int Head[N], dis[N];
int cnt, Max, Index; void Add(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = Head[u];
Head[u] = cnt++;
} void dfs(int u, int w)
{
dis[u] = w; if(dis[u] > Max)
{
Max = dis[u];
Index = u;
} for(int i=Head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].v; if(dis[v] == -)
dfs(v, dis[u]+edge[i].w);
}
} int main()
{
int t, n, iCase=; scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(Head, -, sizeof(Head));
cnt = Max = ; for(int i=; i<n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
Add(u, v, w);
Add(v, u, w);
} memset(dis, -, sizeof(dis));
dfs(, ); memset(dis, -, sizeof(dis));
dfs(Index, );
printf("Case %d: %d\n", iCase++, Max);
}
return ;
}

(树的直径)LightOJ -- 1094的更多相关文章

  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. light oj 1094 Farthest Nodes in a Tree(树的直径模板)

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

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

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

  4. LightOJ 1094 - Farthest Nodes in a Tree

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

  5. 【lightoj-1094】树的直径(DFS)

    链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w, ...

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

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

  7. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  8. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  9. VIJOS1476旅游规划[树形DP 树的直径]

    描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...

随机推荐

  1. 在Linux Bash通过上下键快速查找历史命令

    在centos 7中 ~/.bashrc 或者Mac中的 ~/.bash_profile 中添加,然后source一下以下内容: if [[ $- == *i* ]] then bind '" ...

  2. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  3. 迷你MVVM框架 avalonjs 学习教程18、一步步做一个todoMVC

    大凡出名的MVC,MVVM框架都有todo例子,我们也搞一下看看avalon是否这么便宜. 我们先从react的todo例子中扒一下HTML与CSS用用. <!doctype html> ...

  4. 迷你MVVM框架 avalonjs 学习教程13、模板引用

    稍为复杂一点的网站都是多个前端工程师合作而成,因此分工是必需的.简单一点的分工就是一个人负责一个频道,某个页面是由一个人全部做的:但如果涉及到一个页面非常复杂,需要多个人同时动工呢?于是到模板的出场时 ...

  5. JAVA动态性之一一反射机制reflection

    package com.bjsxt.reflection.test.bean; public class User { private int id; private int age; private ...

  6. django中怎么使用mysql数据库的事务

    Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起失败. Django中对于数据库的事务,默认每执行 ...

  7. byte,short,int,long数据之间的倍数关系

    基本数据类型 byte   =  -128和127------------------------------------------------------------2的8次方,1个字节 shor ...

  8. in文件注意事项及详细解释

    lammps做分子动力学模拟时,需要一个输入文件(input script),也就是in文件,以及关于体系的原子坐标之类的信息文件(data file)和势文件(potential file).lam ...

  9. linux分区之gpt(大于2T的分区)

    1.文件系统限制: ext3块尺寸 最大文件尺寸 最大文件系统尺寸1KiB  16GiB  2TiB2KiB  256GiB  8TiB4KiB  2TiB  16TiB8KiB  16TiB  32 ...

  10. cdoj第13th校赛初赛L - Lovely princess

    http://acm.uestc.edu.cn/#/contest/show/54 L - Lovely princess Time Limit: 3000/1000MS (Java/Others) ...