POJ 1985 Cow Marathon
此题求的是树的最长链, 要注意此题不能用dis[maxn][maxn]来记录a, b两点的距离,这样会超空间, 也就是超内存,要用另一种储存方式储存。此题思想很简单, 随便选一个点,搜此点能到的最远距离, 并记录最远距离的点, 然后再从最远距离的点搜这点能到的最远距离, 这个dis就是答案。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;int m, n, ans, flag, num, head[100005];
bool vis[100005];struct eage
{int to;int next;int eg;
}e[100005];void add(int a, int b, int eg)
{e[num].to = b;e[num].eg = eg;e[num].next = head[a];head[a] = num++;e[num].to = a;e[num].eg = eg;e[num].next = head[b];head[b] = num++;
}void bfs(int a)
{int dis = 0, he, to;queue<int> q;q.push(a);q.push(dis);while(!q.empty()){he = q.front();vis[he] = 1;q.pop();dis = q.front();q.pop();to = head[he];while(vis[e[to].to] == 1&&to != -1){to = e[to].next;}while(to != -1&&vis[e[to].to] == 0){q.push(e[to].to);q.push(dis + e[to].eg);if(ans < (dis + e[to].eg))ans = dis + e[to].eg, flag = e[to].to;to = e[to].next;while(vis[e[to].to] == 1&&to != -1){to = e[to].next;}}}
}int main(int argc, char *argv[])
{int i, j, k, l;ans = 0;num = 0;mem(e);mem(vis);memset(head, -1, sizeof(head));scanf("%d%d",&m, &n);for(i = 0;i < n;i++){scanf("%d%d%d%*c%*c%*c", &j, &k, &l);add(j, k, l);}bfs(1);mem(vis);bfs(flag);printf("%d\n", ans);return 0;
}
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
给出一棵树 ,和边的权值 求权值最长的一条直径 两次bfs求 第一次以任意点开始 BFS求出第一个端点 第二次以第一次得到的端点 BFS求出第二个端点 #include <cstdio> ...
- POJ 3660 Cow Contest [Floyd]
POJ - 3660 Cow Contest http://poj.org/problem?id=3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1. ...
- poj1985 Cow Marathon(树的直径#入门)
poj1985 Cow Marathon(树的直径) Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 9110 Accepted: ...
- POJ 3660 Cow Contest 传递闭包+Floyd
原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- [POJ 3270]Cow Sorting
Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...
- poj 3045 Cow Acrobats (贪心!!不是二分,)
农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 <= W_i <= 10,000)和力量( ...
- POJ 3263-Tallest Cow
Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow ...
- POJ 3660 Cow Contest
Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...
最新文章
- 呼之欲出的量子计算机和漫长的最后一公里
- gometalinter代码检查
- 请说明一下http和https的区别
- 其它综合-CentOS7 忘记root密码
- 关于提高网页加载速度个人学习以及经验总结
- 学java要背的单词_学习JAVA必背的8类常用单词
- 【新书速递】你想知道的通信“灵魂三问”都在这里……(福利再现)
- Python函数合集:68个内置函数请收好!
- springsecurity 认证之密码模式
- CentOS下双网卡绑定-bond0
- Mysql常用内置函数
- 数学分析思维导图整理(四)单变量积分学
- 后端日志【22】:时间过的很快,坚持的第7.3个月过去了,我有什么变化?
- 油猴脚本对web项目的影响
- 个人博客建站中jekyll/hexo/Hugo各自的优势与区别
- 看电脑头痛计算机专业,看电脑头疼怎么回事
- Win10 点击任务栏固定的文件夹资源管理器就重启
- 使用hexo基于cactus仙人掌主题最全美化客制教程
- 一个压长定式衍生出的一个角部攻防
- 概率论笔记—一维随机变量及其分布