这道题以前好像在哪遇到过。

注意树的每一条边都是桥,所以,桥两端的点要到达对方是必须通过这条边的。于是,可以把边由小到大排序,利用并查集,这样,每加一条边就连通了一部分,而随着权值的增大,必定是桥两端到达对方经过的最大的边。于是总的权为左边集合数*右边集合数*桥的权值,就可以求出最大值和了。求最小值和相同。两者相减即为结果。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define LL unsigned __int64
using namespace std; const int N=150100; int pre[N];
LL cnt[N];
struct Edge{
int u,v;
LL c;
}edge[N];
bool cmp1(Edge a,Edge b){
if(a.c>b.c) return true;
return false;
}
bool cmp2(Edge a,Edge b){
if(a.c<b.c) return true;
return false;
} int n; int findr(int u){
int r,t=u;
while(pre[t]!=-1){
t=pre[t];
}
r=t;
while(u!=r){
t=pre[u];
pre[u]=r;
u=t;
}
return r;
} int main(){
int t=0;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n-1;i++){
scanf("%d%d%I64u",&edge[i].u,&edge[i].v,&edge[i].c);
}
for(int i=1;i<=n;i++){
pre[i]=-1,cnt[i]=1;
}
LL ans_max=0,ans_min=0;
int u,v;
sort(edge,edge+n-1,cmp2);
for(int i=0;i<n-1;i++){
u=findr(edge[i].u),v=findr(edge[i].v);
ans_max+=(cnt[u]*cnt[v]*edge[i].c);
if(cnt[u]>cnt[v]){
pre[v]=u;
cnt[u]+=cnt[v];
}
else {
pre[u]=v;
cnt[v]+=cnt[u];
}
}
for(int i=1;i<=n;i++){
pre[i]=-1,cnt[i]=1;
}
for(int i=n-2;i>=0;i--){
u=findr(edge[i].u),v=findr(edge[i].v);
ans_min+=(cnt[u]*cnt[v]*edge[i].c);
if(cnt[u]>cnt[v]){
pre[v]=u;
cnt[u]+=cnt[v];
}
else {
pre[u]=v;
cnt[v]+=cnt[u];
}
}
printf("Case #%d: %I64u\n",++t,ans_max-ans_min);
}
return 0;
}

  

HDU 5176的更多相关文章

  1. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  3. hdu 5176(并查集)

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  4. hdu 5176 The Experience of Love

    The Experience of Love  Accepts: 11  Submissions: 108  Time Limit: 4000/2000 MS (Java/Others)  Memor ...

  5. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. EOJ 1641/UVa The SetStack Computer

    Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...

  2. 学习XOR

    //f(x;W,c,w,b)=w*max{0, W*x+c}+b #include <iostream>#include <vector>#include <algori ...

  3. 正确的缩写document。querySelector

    北京的夕阳,伴随淡淡的霾殇.从写字楼望去,光线是那么昏黄.没有孤雁,也没有霞光,遥想当年,还是 jQuery 独霸一方.那时的我们,写程序都习惯了使用 $,至少在对美元符号的喜爱上,与 PHP 达成了 ...

  4. Django day03 orm介绍

    一:orm介绍 就是对象关系映射,python当中的类能够对应到数据库当中的某一个表(一个类对应一个表),一个类的对象对应着数据库中的一条记录,类当中的某个字段对应着数据库当中表的字段,字段拥有不同的 ...

  5. 通过JS制作一个简易数码时钟

    设计思路: 数码时钟即通过图片数字来显示当前时间,需要显示的图片的URL根据时间变化而变化. a.获取当前时间Date()并将当前时间信息转换为一个6位的字符串; b.根据时间字符串每个位置对应的数字 ...

  6. 从flink-example分析flink组件(1)WordCount batch实战及源码分析

    上一章<windows下flink示例程序的执行> 简单介绍了一下flink在windows下如何通过flink-webui运行已经打包完成的示例程序(jar),那么我们为什么要使用fli ...

  7. Win7的虚拟Wi-Fi

    前几天无意中发现,Win7的硬件驱动里有个叫Microsoft Virtual WiFi Miniport Adapter的东东,从网上查了一下,可以用来组建临时网络,共享Internet.一块无线网 ...

  8. android 提纲挈领

    之后的android学习将侧重三方面: 1.基础内容例如xml属性.sharedpreference.数据库必须能够熟记于心. 2.开源library熟练应用,能够了解如何更好地使用各种开源libra ...

  9. Spring Boot (20) 拦截器

    动态资源和静态资源 拦截器可以算是aop的一种实现,专门拦截对动态资源的后台请求,也就是拦截对控制层的请求,主要用于判断用户是否有权限请求后台.拦截器不会拦截静态资源,如spring boot默认静态 ...

  10. 青橙 M4 解锁BootLoader 并刷入recovery ROOT

    首先下载工具链接:https://pan.baidu.com/s/1o9xzTEi密码:7s7a 备用连接:https://pan.baidu.com/s/1bq47TMn 本篇教程教你如何傻瓜式解锁 ...