贪心就离根最大距离

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 500007; struct Edge{
int nxt, pre, w;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v, int w){
e[++cntEdge] = (Edge){ head[u], v, w}, head[u] = cntEdge;
} long long ans;
int dis[N];
inline void DFS(int u, int fa){
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue;
DFS(v, u);
dis[u] = Max(dis[u], dis[v] + e[i].w);
}
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v == fa) continue;
ans += dis[u] - dis[v] - e[i].w;
}
} int main(){
int n, rt;
io >> n >> rt;
R(i,2,n){
int u, v, w;
io >> u >> v >> w;
add(u, v, w);
add(v, u, w);
} DFS(rt, 0); printf("%lld", ans); return 0;
}

最新文章

  1. 【Java并发编程实战】-----&ldquo;J.U.C&rdquo;:CLH队列锁
  2. Unity3D安卓打包参数配置与兼容性的关系分析
  3. Swing How to make dialogues
  4. android 类ios actionsheet效果
  5. SparkSQL使用之JDBC代码访问Thrift JDBC Server
  6. linux 捕获信号处理中遇到的死锁
  7. D&amp;F学数据结构系列——B树(B-树和B+树)介绍
  8. Google Chrome浏览器调试功能介绍
  9. HITAG 1/2/S
  10. Oracle表操作 (未完待续)
  11. Adress
  12. 实例介绍Cocos2d-x物理引擎:使用关节
  13. [网络分析]WEBQQ3.0协议分析---good good study
  14. uva - The Lottery(容斥,好题)
  15. Delphi反射
  16. nginx tcp proxy 连接保持设置
  17. CGI编程学习----查询2000W开房数据
  18. java创建线程的三种方法
  19. python高级编程1
  20. BZOJ 4242: 水壶(Kruskal重构树 + Bfs)

热门文章

  1. 题解 CF1095F 【Make It Connected】
  2. [OCWA 模拟赛ADay1] 钢铁侠的逃离
  3. python+selenium 自动化测试——显式等待详解
  4. 聊聊 C# 方法重载的底层玩法
  5. 2 Elment Ui 日期选择器 格式化问题
  6. Java 将HTML转为XML
  7. BUUCTF-小明的保险箱
  8. JavaScript与Node.js一起打造一款聊天App
  9. OpenSSF安全计划:SBOM将驱动软件供应链安全
  10. python小题目练习(五)