Cow Marathon
poj1985:http://poj.org/problem?id=1985
题意:就是树的直径。
题解:直接DFS即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e5+;
struct Node{
int v;
int w;
};
vector<Node>Q[N];
int n,m,k,u,v;
int maxn,start;
void DFS(int u,int f,int len){
if(len>maxn){
maxn=len;
start=u;
}
for(int i=;i<Q[u].size();i++){
if(f!=Q[u][i].v){
DFS(Q[u][i].v,u,len+Q[u][i].w);
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++)
Q[i].clear();
for(int i=;i<=m;i++){
scanf("%d %d %d",&u,&v,&k);
getchar();getchar();
Node temp;temp.v=v;temp.w=k;
Q[u].push_back(temp);
temp.v=u;temp.w=k;
Q[v].push_back(temp);
}
start=,maxn=;
DFS(,,);
maxn=;
DFS(start,,);
printf("%d\n",maxn); }
}
Cow Marathon的更多相关文章
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- 树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松
3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽 ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ 1985 Cow Marathon (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- POJ P1985 Cow Marathon 题解
这道题是我们考试的第一题,非常水,就是一个树的直径的板子.详见上一篇博客. #include<iostream> #include<cstdio> #include<cs ...
随机推荐
- 局域网内使用linux的ntp服务
假设我们的饿局域网无法连接外网,但又需要同步时间,怎么办? 1. 已局域网内的一台机器作为基础,适用date修改其他机器的时间,date -s ...,很不方便,这里不介绍. 2. 适用ntp服务,自 ...
- expect批量分发公钥
sshkey.exp #!/usr/bin/expect# 由于是多台服务器,需要在shell脚本中循环调用该脚本 if { $argc != 2 } { send_user "usage: ...
- HDU2054JAVA
(包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: ...
- linux中的网络通信指令 分类: 学习笔记 linux ubuntu 2015-07-06 16:02 134人阅读 评论(0) 收藏
1.write write命令通信是一对一的通信,即两个人之间的通信,如上图. 效果图 用法:write <用户名> 2.wall wall指令可将信息发送给每位同意接收公众信息的终端机用 ...
- ListView simpleAdapter的基本使用
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行.HashMap的每个键 值数据映射到布局文件中对应id的组件上.因为系统没有对 ...
- grep参数说明及常用用法
grep参数说明及常用用法 趁着午休的时间把自己经常使用的一些grep命令整理一下. 方便以后查看. 后续会逐步把awk/sed/find等常用的命令理一理. 增强下记忆. 也算是对得起自己了. ^^ ...
- GitHub详细教程
GitHub详细教程 Table of Contents 1 Git详细教程 1.1 Git简介 1.1.1 Git是何方神圣? 1.1.2 重要的术语 1.1.3 索引 1.2 Git安装 1.3 ...
- python 面向对象简单理解
面向对象: 是一种程序设计范型 作用: 提高软件的重用性和灵活性,扩展性 世界万物一切皆为对象,对象即是指由特定状态,特征,行为的实体 知识点一: 代码的重用 举个栗子 比如小月月有了一个女朋友1 ...
- The hacker's sanbox游戏
第一关:使用/usr/hashcat程序,对passwd中root的密码进行解密,得到gravity98 执行su,输入密码gravity98. 第二关:获取提供的工具,wget http://are ...
- 从source folder 下将其所有子文件夹的*.* 文件拷贝到 target folder (不拷贝文件夹名仅拷贝文件)
因本人较懒,一直认为电脑能做的就让电脑来做,所以写下这个批处理的小脚本方便工作. 场景:碰到要拷贝一个文件夹(source folder)下的多个子文件夹(sub-folder)的文件到指定文件夹下( ...