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. VSX-2 搭建项目

    由于是公司的项目,也不可能直接拿过来写博客,所以准备搭建一个自己的VSX项目. 项目需求这里就不写了,大体可参考曾经的一篇文章,这个VSX项目就是用来简化插件式开发. 本文开始正式记录做这个VSX项目 ...

  2. runtime如何通过selector找到对应的IMP地址?(分别考虑类方法和实例方法)

    每一个类对象中都一个对象方法列表(对象方法缓存) 类方法列表是存放在类对象中isa指针指向的元类对象中(类方法缓存) 方法列表中每个方法结构体中记录着方法的名称,方法实现,以及参数类型,其实selec ...

  3. USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】

    nocows解题报告------------------------------------------------------------------------------------------ ...

  4. Node应用进程管理器pm2的使用

    本文转载自:豆瓣-PM2介绍 更多内容见github上的pm2说明文档pm2 pm2 是一个带有负载均衡功能的Node应用的进程管理器. 当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程 ...

  5. kafak基本操作

    创建topic bin/kafka-topics.sh --create --zookeeper 192.168.1.81:2181 --replication-factor 3 -partition ...

  6. 想了一天的题目QAQ 毛线数列的最值

    #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #i ...

  7. Nginx简单的配置详情

    大致了解Nginx后,直接从配置文件入手: [shell] #定义Nginx运行的用户和用户组 user nginx; #nginx进程数,建议设置为等于CPU总核心数. worker_process ...

  8. struts框架搭建

    struts是开源框架.使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间.如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的 ...

  9. Java分布式数据导出实践

    伴随业务发展日益剧增,对数据的要求越来越多也越来越高. 用户在浏览器发起导出请求--web服务器接收请求--请求后台获取数据--数据统计后生成excel或其他图标--响应给客户端 整个过程至少5步,才 ...

  10. shit vue-cli & path bug & baseUrl bug

    vue-cli path bug https://cli.vuejs.org/zh/guide/#cli baseUrl bug baseUrl: "././" , https:/ ...