POJ 1985 Cow Marathon(树的直径)
来源:互联网 发布:js隐藏标签 编辑:程序博客网 时间:2024/05/22 13:20
Description
给出一棵无向树,求树的直径
Input
第一行为两个整数n和m分别表示点数和边数,之后m行每行表示树的一条边
Output
输出树的直径长度
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
Sample Output
52
Solution
树的直径裸题
Code
#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;#define maxn 111111struct node{ int to,c,next;}edge[2*maxn];int n,m,head[maxn],tot,dis[maxn],vis[maxn];void init(){ memset(head,-1,sizeof(head)); tot=0;}void add(int u,int v,int c){ edge[tot].to=v; edge[tot].c=c; edge[tot].next=head[u]; head[u]=tot++;}void dfs(int u){ for(int i=head[u];~i;i=edge[i].next) { int v=edge[i].to,c=edge[i].c; if(!vis[v]) { vis[v]=1; dis[v]=dis[u]+c; dfs(v); } }}int main(){ int u,v,c,s;char op[3]; while(~scanf("%d%d",&n,&m)) { init(); while(m--) { scanf("%d%d%d%s",&u,&v,&c,op); add(u,v,c),add(v,u,c); } memset(vis,0,sizeof(vis)); dis[1]=0,vis[1]=1; dfs(1); for(int i=2,temp=-1;i<=n;i++) if(dis[i]>temp)temp=dis[i],s=i; memset(vis,0,sizeof(vis)); dis[s]=0,vis[s]=1; dfs(s); int ans=-1; for(int i=1;i<=n;i++) ans=max(ans,dis[i]); printf("%d\n",ans); } return 0;}
0 0
- POJ 1985 Cow Marathon 树的直径
- POJ 1985 Cow Marathon 树的直径
- 【树的直径】 POJ 1985 Cow Marathon
- poj 1985 Cow Marathon 【树的直径】
- POJ 1985 Cow Marathon(树的直径)
- poj 1985 Cow Marathon 【树的直径】
- poj-1985-Cow Marathon【树的直径】
- poj 1985 Cow Marathon 树的直径
- Poj 1985 Cow Marathon ( 树的直径
- [POJ 1985][树的直径]Cow Marathon
- poj 1985 Cow Marathon(树直径)
- [POJ 1985] Cow Marathon (树的直径)
- POJ 1985 Cow Marathon(求树的直径)
- POJ 1985 Cow Marathon(树的直径)
- poj--1985--Cow Marathon(树的直径)
- POJ 1985 Cow Marathon(树的直径)
- POJ 1985 Cow Marathon(树的直径&BFS+BFS)
- 【POJ】1985 - Cow Marathon(树的直径)
- 关于Windows7 64的注册表读写问题
- uvaoj-1225:数数字
- spring(AOP)多个切面
- Leetcode之first missing positive
- XML VS SQLite
- POJ 1985 Cow Marathon(树的直径)
- JUnit Test WebService
- 可以输入的下拉框,可以筛选内容,可输入筛选下拉框(原创)
- Leetcode之Sort List
- HDU-1010-Tempter of the Bone( DFS + 深度优先搜索 )
- 在java中的基础点
- ubuntu 搭建(编译)生产力版的LNMP环境
- RHCE 学习笔记(20) ACL
- 下载并安装libevent开发库详细步骤