POJ2631 Roads in the North
题面:http://poj.org/problem?id=2631
本题是求树的直径裸题。Code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<ctime>
using namespace std;
const int MAXN=10005;
struct Node{int v,Next,w;
}Edge[MAXN*2];
int head[MAXN],maxDis,Cnt,maxInd;
void Push(int u,int v,int w){++Cnt;Edge[Cnt].v=v; Edge[Cnt].w=w;Edge[Cnt].Next=head[u];head[u]=Cnt;
}
void dfs(int now,int fa,int dis){if(dis>maxDis){maxDis=dis;maxInd=now;}for(int i=head[now];i;i=Edge[i].Next){if(Edge[i].v!=fa){dfs(Edge[i].v,now,Edge[i].w+dis);}}
}
int main(){int u,v,w;while(~scanf("%d%d%d",&u,&v,&w)){Push(u,v,w);Push(v,u,w);}dfs(1,0,0);dfs(maxInd,0,0);printf("%d\n",maxDis);return 0;
}
转载于:https://www.cnblogs.com/ukcxrtjr/p/11243509.html
POJ2631 Roads in the North相关推荐
- 【POJ - 2631】Roads in the North (树的直径,模板)
题干: Building and maintaining roads among communities in the far North is an expensive business. With ...
- 【POJ - 2631 】Roads in the North(树的直径)
题干: Building and maintaining roads among communities in the far North is an expensive business. With ...
- C++剑指offer:[POJ]2631 Roads in the North - 用树形DP的方式求出一棵树的直径
前言 此题是道很简单的题(做法不单一,不仅只有树形DP的方法) 做完了这道题才发现此题原来是一道求树的直径的题,也就是求树上两个节点的最大距离. 题目 问题 N(2692): [POJ2631]北极地 ...
- AOAPC I: Beginning Algorithm Contests 题解
AOAPC I: Beginning Algorithm Contests 题解 AOAPC I: Beginning Algorithm Contests (Rujia Liu) - Virtual ...
- 树的直径/重心 学习笔记
树的直径 POJ2631 Roads in the North 题意:裸的直径 题解:套模板,但是开始的时候,我的代码在第一次dfs循环的时候,没有考虑到,路长全为0的情况,而用来记录最远点的maxp ...
- UVa Online Judge 工具網站
UVa Online Judge 工具網站 转自http://www.csie.ntnu.edu.tw/~u91029/uva.html Lucky貓的ACM園地,Lucky貓的 ACM 中譯題目 M ...
- HOJ题目分类//放这儿没事刷刷学算法!嘻嘻!
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 提取了下刘汝佳推荐的题号...
今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started 10055 - Hashmat the Brave Warrior ...
- USACO07DEC道路建设Building Roads(prim算法+堆优化与Kruskal+路径压缩对比)
目录 primprimprim算法 KruskalKruskalKruskal算法 P2872 [USACO07DEC]道路建设Building Roads 4 1 1 1 3 1 2 3 4 3 1 ...
最新文章
- python 3.9 新特性 简介
- Ubuntu安装pycharm并且激活
- Metapath2vec:Scalable Representation Learning for Heterogeneous Networks(结构化深度网络特征表示)
- IEDevToolBar - 一个分析网页的有用的工具
- python_day9 回调函数
- java跨境电商ERP源码
- 将数字转换为中文大写(缩写)
- python中snip_Snip滚动截屏_腾讯Snip For Mac官方下载-华军软件园
- HCIA H12-211题库解析
- 计算机按键不灵怎么回事,电脑键盘失灵,教您电脑键盘失灵怎么办
- 它是最给力的数据分析体系,却被90%的新人忽略!
- VMware虚拟机NAT模式下连不上网
- 随机产生a-z一个字母
- 计算机博士 英语词汇,2017考博英语词汇
- 【C语言】你会用吗?
- C# 实现打印机功能
- 从零开始学习Python在e站上下载蕾姆的本子
- Laravel后端接口使用mews/captcha验证码注册+登录流程讲解
- 喜讯:我司通过ISO9001质量管理体系认证!
- 差分电荷密度怎么画_MS电荷密度在Origin中作图方法