【树的直径】 POJ 1985 Cow Marathon
给出一棵树 ,和边的权值
求权值最长的一条直径
两次bfs求
第一次以任意点开始 BFS求出第一个端点
第二次以第一次得到的端点 BFS求出第二个端点
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
const int MAXN = 200100;
const int INF = 999999;
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(int &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
struct Edge
{int to,next,val;
} edge[MAXN*2];
int head[MAXN],d[MAXN],tol;
bool vis[MAXN];
void init()
{tol=0;memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{edge[tol].to=v,edge[tol].val=w,edge[tol].next=head[u];head[u]=tol++;edge[tol].to=u,edge[tol].val=w,edge[tol].next=head[v];head[v]=tol++;
}
int bfs(int u)
{int point,big=0;memset(vis,false,sizeof(vis));queue<int>q;q.push(u);vis[u]=true;d[u]=0;while(!q.empty()){u=q.front();q.pop();for(int i=head[u]; ~i; i=edge[i].next){int v=edge[i].to;if(!vis[v]){d[v]=d[u]+edge[i].val;if(d[v]>big){big=d[v];point=v;}vis[v]=true;q.push(v);}}}return point;
}
int main()
{
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endifint t,a,b,c,n,m;while(scanf("%d%d",&n,&m)!=EOF){init();for(int i=0; i<m; i++){scanf("%d%d%d %*c",&a,&b,&c);addedge(a,b,c);}int u=bfs(1);int v=bfs(u);int len=d[v];cout<<len<<endl;}return 0;
}
转载于:https://www.cnblogs.com/kewowlo/p/4088333.html
【树的直径】 POJ 1985 Cow Marathon相关推荐
- poj 1985 Cow Marathon 【树的直径】
求树的直径 /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostre ...
- POJ 1985 Cow Marathon
此题求的是树的最长链, 要注意此题不能用dis[maxn][maxn]来记录a, b两点的距离,这样会超空间, 也就是超内存,要用另一种储存方式储存.此题思想很简单, 随便选一个点,搜此点能到的最远距 ...
- 0x63.图论 - 树的直径与最近公共祖先
目录 一.树的直径(Diameter) 1.树形DP求树的直径 2.两次BFS/DFS求树的直径 1.POJ 1985.Cow Marathon(DFS求树的直径模板题) 2.AcWing 350. ...
- poj1985 Cow Marathon(树的直径#入门)
poj1985 Cow Marathon(树的直径) Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 9110 Accepted: ...
- poj 1383 Labyrinth 树的直径
题意: 给一个迷宫,其中任意两个可以走的点之间只有一条路,求在这个迷宫里最长能走多远. 分析: 求树的直径,两次dfs即可. 代码: //poj 1383 //sep9 #include <io ...
- POJ 1849 Two(树的直径+思维)
题目链接:http://poj.org/problem?id=1849 题意是有n个点,n-1条边(树形图),在s点放两个机器人,问这两个机器人遍历完所有的点的最少花费(不用回到原点). ...
- 【POJ - 3310】Caterpillar(并查集判树+树的直径求树脊椎(bfs记录路径)+dfs判支链)
题干: An undirected graph is called a caterpillar if it is connected, has no cycles, and there is a pa ...
- 【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]北极地 ...
最新文章
- visual studio installer正在提取文件_并非危言耸听,赶紧检查自己网盘内的私密文件...
- 动态规划面试常考:最短路径和
- 过滤器跟拦截器的区别
- Linux环境下Redmine的安装(一)
- Spring 的创建流程
- Exchange 2007 安裝(-)
- java se 开发web程序_JDiy快速开发WEB之javaSE环境搭建-初级
- CCPC-EDG专场——E.Buy and Delete
- 首都师范大学计算机科学与技术考研分数线,2017考研:计算机科学与技术专业考研院校推荐之首都师范...
- SecureCRT 完美代替超级终端
- Netty傻瓜教程(四):bossGroup, workGroup?
- 快速实现安卓输入法!
- 语音识别(ASR)论文优选:性能测试Wav2Vec2.0 on the Edge: Performance Evaluation
- 在KubeSphere中部署微服务(阡陌)+ DevOps
- Web前端优化最佳实践
- 移植u-boot-1.3.4到GT2440(第二版2.0)
- Python知识体系图
- 堡垒机如何传输文件_mac 堡垒机传文件
- Python PEP—Python增强提案
- 北京理工网络教育计算机网络答案,北理工99级计算机网络终考试卷.doc
热门文章
- python质数列_现代化程序开发笔记(3)——多文件与模块
- 宝塔安装php遇错libicui18n.so.42: cannot open shared object file: No such file or directory
- 【mysql】安装 mysql 5.7 完成后,默认密码是啥?怎么查看默认密码?
- 英属哥伦比亚大学计算机科学,细致用心 终获英属哥伦比亚大学计算机科学专业offer...
- mysql日期格式化季度_mysql中常用日期比较与计算函数
- php mssql 新 id,MSSQL获取当前插入数据的id
- 并联串联混合的电压和电流_电流互感器一次绕组串联、并联,二次绕组串联、并联的相关问题...
- linux系统内核官网,五年26个版本:Linux系统内核全程回顾
- data的值 如何初始化vue_vue 创建一个基础实例【02】
- 还记得当年你是如何接触Python的吗?