UVa 10308 Roads in the North:树上的最长路

10308 - Roads in the North

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1249

思路:由于是一棵树,我们只要随便指定一个树根就开始dfs。所有路径都可以看成以父节点为中转点的“子树甲+子树乙”的形式(只有一个子树的情况也是一样的),遍历时向上返回一个最大的路径长即可。

完整代码:

/*0.016s*/

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;  

char s[30];
int ans;
vector<pair<int, int> > node[maxn]; /// first: 与node[i]连结的村庄号  second: 距离  

int dfs(int to, int from)
{
    int lto, lans = 0, lmax = 0;
    for (int i = 0; i < node[to].size(); ++i)
    {
        lto = node[to][i].first;
        if (lto != from)
        {
            lans = dfs(lto, to) + node[to][i].second;///递归子树,返回子树中的最长路
            ans = max(ans, lans + lmax);///当前得到的子树最长路和前面得到的另一棵子树的最长路之和
            lmax = max(lmax, lans);
        }
    }
    return lmax;
}  

int main()
{
    int u, v, l, i;
    bool ok = true;
    while (ok)
    {
        for (i = 0; i < maxn; ++i) node[i].clear();
        while (true)
        {
            if (gets(s) == 0)
            {
                ok = false;
                break;
            }
            if (s[0])
            {
                sscanf(s, "%d%d%d", &u, &v, &l);
                node[u].push_back(make_pair(v, l));
                node[v].push_back(make_pair(u, l));
            }
            else break;
        }
        ans = 0;
        dfs(1, 0);
        printf("%d\n", ans);
    }
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索递归
, int
, node
, 路径
, 一个
最长
uva ms in commerce、the silk roads、the two roads、the toll roads、the silk roads 微盘,以便于您获取更多的相关知识。

时间: 2024-09-17 21:34:12

UVa 10308 Roads in the North:树上的最长路的相关文章

算法题:UVA 437 The Tower of Babylon (dp + DAG最长序列)

The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians

uva 103 - Stacking Boxes DAG最长路

    图很小,所以随便乱搞就可以了,记忆化搜索 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <

算法题:10308

Problem I Roads in the North Input: standard input Output: standard output Time Limit: 2 seconds Memory Limit: 32 MB Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are built in

UVa 437 The Tower of Babylon:DP&amp;amp;DAG

437 - The Tower of Babylon Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=378 思路: 对于一个(x,y,z)砖头,它可以有3中姿势放置: (前两个为地面,后一个为高) x, y, z x, z, y y, z, x 把每种姿势

uva 10382 - Watering Grass

点击打开链接uva 10382 题目意思:     有一块草坪,长为l,宽为w  ,在它的中心线上装有n个点状的喷水装置 , 效果是让以它为中心半径为ri的圆被润湿  , 选择尽量少的喷水装置把整个草坪全部润湿. 解题思路:   1:贪心,区间覆盖 2:这道题就是一个区间覆盖的变形,只是没有那么的直观而已.刚开始区间求错了,直接求出最左边和最右边的左边以此来作为区间端点,后来WA了近20次才发现考虑错了,嗨,只怪自己思维不够严谨.真正的区间求法是,把这个圆和这个矩形两边的交点的横坐标求出来就是区

最短路专题【完结】

第一题 hdu 1317 XYZZY 点击打开hdu 1317 思路: 1 题目的图是一个有向图,并且可能存在环.第一个点的能量值为100,边的权值利用能量大小,例如2点为-60,如果1->2那么value[1][2] = -602 题目明确指出如果是要win的话,那么必须是经过的每条边都要大于0.那么我们只要把那些经过松弛操作后的点大于0的入队即可,小于等于0的点肯定不会出现在最终的路径上.3 如果存在正环的话,那么就有能量值无限大,那么这个时候只要判断这个点能否到达n4 判断是否是有环还是五

看懂这些故事,你做人就很成功了

第一课一个男人在他妻子洗完澡后准备进浴室洗澡.这时,门铃响了.妻子迅速用浴巾裹住自己冲到门口.当她打开门时,邻居鲍勃站在那儿.在她开口前,鲍勃说,"你如果把浴巾拿掉,我给你800美元."想了一会儿,这个女人拿掉浴巾赤裸地站在鲍勃面前.几秒钟后,鲍勃递给她800美元然后离开了.女人重新裹好浴巾回到屋里.当她踏进浴室时,丈夫问她,"是谁呀?""是邻居鲍勃."她回答."哦,"丈夫说,"他有没有提到还欠我800美元?&quo

Fedora 内核是由什么构成的?

Fedora 的目标是包含尽可能多的上游代码,这样使得 bug 修复和 API 更新更加容易,同时也会有更多的人审查代码,在理想情况下,Fedora 能够直接获取 kernel.org 的内核,然后发送给所有用户. 现实情况是,使用 vanilla 内核并不能完全满足 Fedora,然而 Vanilla 内核可能并不支持一些 Fedora 用户希望拥有的功能.用户接收的 [Fedora 内核] 是在 vanilla 内核之上打了很多补丁的内核.这些补丁被认为"不在树上out of tree&qu

国内搜索引擎介绍---百度

中介交易 SEO诊断 淘宝客 云主机 技术大厅 百度公司(Baidu.com,Inc) 于1999年底成立于美国硅谷.2000年1月,百度公司在中国成立了她的全资子公司百度网络技术(北京)有限公司,随后于同年10月成立了深圳分公司,2001年6月又在上海成立了上海办事处. 百度是国内最大的商业化全文搜索引擎,占国内80%的市场份额.其功能完备,搜索精度高,除数据库的规模及部分特殊搜索功能外,其他方面可与当前的搜索引擎业界领军人物Google相媲美,在中文搜索支持方面有些地方甚至超过了Google