题意:就是求一个树的直径,也就是求任意两点的最大距离。

做法:跑两遍DFS,详见代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<queue>
#include<vector>
#include<algorithm> using namespace std;
typedef long long LL;
#define N 100100
#define INF 0x3f3f3f3f int n, k, head[N], dist[N], Max, Index;
struct node
{
int v, len, next;
} maps[N<<2]; void add(int u, int v, int len)
{
maps[k].v=v;
maps[k].len=len;
maps[k].next=head[u];
head[u]=k++;
} void DFS(int u, int len)
{
dist[u]=len; if(dist[u]>Max)
{
Max=dist[u];
Index=u;
} for(int i=head[u]; i!=-1; i=maps[i].next)
{
int v=maps[i].v;
if(dist[v]==-1)
DFS(v, dist[u]+maps[i].len);
}
} int main()
{
int T, a, b, c, cas=1;
scanf("%d", &T); while(T--)
{
k=1;
memset(head, -1, sizeof(head));
scanf("%d", &n);
for(int i=0; i<n-1; i++)
{
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
} Max=Index=-1;
memset(dist, -1, sizeof(dist));
DFS(0, 0); memset(dist, -1, sizeof(dist));
DFS(Index, 0);
printf("Case %d: %d\n", cas++, Max);
}
return 0;
}

LightOJ 1094的更多相关文章

  1. LightOJ 1094 - Farthest Nodes in a Tree

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

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

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

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

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

  4. (树的直径)LightOJ -- 1094

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/C 求树的直径,这里只不过给每条边增加一个边长属性,变成 ...

  5. Lightoj 1094 【DFS】

    题意: 给你一颗n-1条边的树,求某两点的最长路 思路: 随便找个根,然后我们从根往下搜,对于每个结点会出现多个子节点或者一个子节点或者没有结点, 多个子节点的话,就以他为中间点,在子节点中找两条最长 ...

  6. 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链

    树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...

  7. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  8. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  9. LightOj 1298 - One Theorem, One Year(DP + 欧拉)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...

  10. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

随机推荐

  1. [ARC158D] Equation

    Problem Statement You are given a positive integer $n$, and a prime number $p$ at least $5$. Find a ...

  2. MySQL运维5-Mycat配置

    一.schema.xml 1.1 简介 schema.xml作为Mycat中最重要的配置文件之一,涵盖了Mycat的逻辑库.逻辑表.分片规则.分片节点即数据源的配置.主要包括一下三组标签 schema ...

  3. vue-admin-template快速开发框架,菜单栏不显示层级结构

    问题现象 路由 页面效果 解决办法 路由添加中配置 alwaysShow: true, // 树形层级显示 页面效果

  4. python tkinter 使用(三)

    python tkinter 使用(三) 本篇文章主要讲下tkinter下的filedialog的使用. 1: askopenfilename 首先使用tkinter中fiedialog来实现一个简单 ...

  5. 万界星空科技仓库管理wms系统

    ​ 企业在管理库存时,尤其是生产制造企业,使用传统方式比如纸笔.Excel 管理库存,由于工具和信息化存在局限,导致在管理库存时出现如下问题: 1.通过纸笔记录出入库申请,人为手动计算易出错,数据易丢 ...

  6. idea2020下载、安装、破解、配置

    idea2020下载.安装.破解.配置 idea2020下载 [推荐]官方下载地址:https://www.jetbrains.com/idea/download/other.html 进入后往下找, ...

  7. ElasticSearch之cat templates API

    命令样例如下: curl -X GET "https://localhost:9200/_cat/templates?v=true&pretty" --cacert $ES ...

  8. EvilBox : ONE - WriteUp

    EvilBox : ONE 信息收集 扫描网段内存活主机,得到目标 nmap 进一步收集有效信息,只开放了22和80 访问80的页面没有什么有效信息 接着扫一下目录, 没有敏感的文件 在robots中 ...

  9. java中根据公网IP获取地址

    package com.dashan.utils.iputils; import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3 ...

  10. Redis核心技术与实战

    最近在读一篇关于Redis的专栏,叫做<Redis核心技术与实战>,作者在Redis方面研究颇深,读后非常受益,特在此做记录. 一.Redis基础 1)知识图和问题画像图 Redis知识全 ...