Two

思路:

  树形DP求直径;

  答案是边权总和*2-直径;

  dp[i][1]::以i为根的子树中最长的路径;

  dp[i][0]::以i为根的子树中次长的路径;

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005 int n,s,E[maxn<<],V[maxn<<],W[maxn<<],cnt;
int head[maxn],dp[maxn][],sum,ans=; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void dfs(int now,int fa)
{
bool res=true;
int pos=,gs=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
res=false,dfs(V[i],now);
if(W[i]+dp[V[i]][]>gs) gs=W[i]+dp[V[i]][],pos=V[i];
}
if(res) return ;
dp[now][]=gs,gs=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa||V[i]==pos) continue;
if(W[i]+dp[V[i]][]>gs) gs=W[i]+dp[V[i]][];
}
dp[now][]=gs,ans=max(ans,dp[now][]+dp[now][]);
} int main()
{
in(n),in(s);int u,v,w;
for(int i=;i<n;i++)
{
in(u),in(v),in(w),sum+=w*;
E[++cnt]=head[u],V[cnt]=v,W[cnt]=w,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,W[cnt]=w,head[v]=cnt;
}
dfs(s,);
cout<<sum-ans;
return ;
}

AC日记——Two poj 1849的更多相关文章

  1. AC日记——Tree poj 3237

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9233   Accepted: 2431 Description ...

  2. AC日记——Dividing poj 1014

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 69575   Accepted: 18138 Descri ...

  3. AC日记——Crane poj 2991

    POJ - 2991 思路: 向量旋转: 代码: #include <cmath> #include <cstdio> #include <cstring> #in ...

  4. AC日记——pigs poj 1149

    POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...

  5. AC日记——Dining poj 3281

    [POJ-3281] 思路: 把牛拆点: s向食物连边,流量1: 饮料向t连边,流量1: 食物向牛1连边,流量1: 牛2向饮料连边,流量1: 最大流: 来,上代码: #include <cstd ...

  6. AC日记——Oulipo poj 3461

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37958   Accepted: 15282 Description The ...

  7. POJ 1849 Two(遍历树)

    POJ 1849 Two(遍历树) http://poj.org/problem?id=1849 题意: 有一颗n个结点的带权的无向树, 在s结点放两个机器人, 这两个机器人会把树的每条边都走一遍, ...

  8. poj 1849 Two

    /*poj 1849 two 思考一下会发现 就是求直径 直径上的中点就是两个人分开的地方(不再有交集)*/ #include<cstdio> #define maxn 100010 us ...

  9. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

随机推荐

  1. string函数Contains()实例

    public bool Contains(string value)如果值参数出现在此字符串内,或者值为空字符串(“”),则为true; 否则为false using System; class Ex ...

  2. Kotlin中功能操作与集合(KAD 11)

    作者:Antonio Leiva 时间:Feb 2, 2017 原文链接:https://antonioleiva.com/functional-operations-collections-kotl ...

  3. 测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低)

    测试基础面试题 + SQL 面试题(选择题有部分答案,难度:低) 答案: .A .C .C .A .A .D

  4. SQL面试题:之一(难度:中等)

    SQL面试题:之一(难度:中等)

  5. APP测试工程师面试题:之一

    第六题主要流程:编写计划 → 测试用例 → 评审用例 → 执行用例 → 写BUG →测修复情况 → 上线

  6. [译]15-spring 自动装配

    前面的章节我们已经学习了如何使用bean元素在xml配置文件中声明一个bean.也学习了如何使用bean的子元素contructor-arg 和property进行bean的依赖项的注入. 之前bea ...

  7. NVIDIA/DIGITS:Building DIGITS

    在 Prerequisites中的 sudo apt-get update命令发生错误: W: GPG 错误:http://developer.download.nvidia.com/compute/ ...

  8. 原始套接字--traceroute

    traceroute, 也就是 trace route,跟踪路由.这个程序最早是Van Jacobson实现的.源码在网上可以找到,不过我还没有去找.是IP路由过程中对数据包TTL(Time to L ...

  9. 团队项目-第七次scrum 会议

    时间:11.3 时长:30分钟 地点:F楼1039教室 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习cocos creator样例 修复员工招聘时bug 陈鑫 完成fire()与UI的对接 ...

  10. 【python基础】--常用数据结构

    list tuple dict set四种常用数据结构 list list 有序的集合,可以随时添加.删除其中元素值; 支持list嵌套模式, >>> p = ['a','b']&g ...