The Experience of Love

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 645    Accepted Submission(s): 216

Problem Description
A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting of N cities and only N−1
edges (just like a tree), every edge has a value means the distance of
two cities. They select two cities to live,Gorwin living in a city and
Vivin living in another. First date, Gorwin go to visit Vivin, she would
write down the longest edge on this path(maxValue).Second date, Vivin
go to Gorwin, he would write down the shortest edge on this
path(minValue),then calculate the result of maxValue subtracts minValue
as the experience of love, and then reselect two cities to live and
calculate new experience of love, repeat again and again.

Please help them to calculate the sum of all experience of love after they have selected all cases.

 
Input
There will be about 5 cases in the input file.
For each test case the first line is a integer N, Then follows n−1 lines, each line contains three integers a, b, and c, indicating there is a edge connects city a and city b with distance c.

[Technical Specification]
1<N<=150000,1<=a,b<=n,1<=c<=109

 
Output
For
each case,the output should occupies exactly one line. The output
format is Case #x: answer, here x is the data number, answer is the sum
of experience of love.
 
Sample Input
3
1 2 1
2 3 2
5
1 2 2
2 3 5
2 4 7
3 5 4
 
Sample Output
Case #1: 1
Case #2: 17

Hint

huge input,fast IO method is recommended.

In the first sample:
The maxValue is 1 and minValue is 1 when they select city 1 and city 2, the experience of love is 0.
The maxValue is 2 and minValue is 2 when they select city 2 and city 3, the experience of love is 0.
The maxValue is 2 and minValue is 1 when they select city 1 and city 3, the experience of love is 1.
so the sum of all experience is 1;

 
 
题意:给出一棵树n个结点n-1条边,找到所有的两个点之间的最大值和最小值,求 sum(MAX-MIN).
题解:sum(MAX-MIN) = sum(MAX)-sum(MIN)很巧妙的思路,并查集将n-1条边添加进去,按照边权从小到大排序,如果现在找到了点 a ,b 那么a的所有子节点和 b的所有子节点中的最大权边必定为 edge[a][b] 所以我们根据乘法原理可以得出当前的所有最大权为 edge[a][b]  的点,他们对最大值结果的贡献为 cnt[a]*cnt[b]*edge[a][b].最小值的话从大到小选就行了。最后结果会爆long long ,所以要开 unsigned long long ,输入输出用 %I64u
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef unsigned long long LL;
const int N = ;
struct Edge
{
LL u,v;
LL w;
} edge[N];
LL father[N];
LL cnt[N];
LL MAX,MIN;
LL _find(LL x)
{
if(x!=father[x]){
father[x] = _find(father[x]);
}
return father[x];
}
void init(int n)
{
for(int i=; i<=n; i++)
{
father[i] = i;
cnt[i] = ;
}
}
void Union(LL a,LL b,LL w,int flag)
{
LL x = _find(a);
LL y = _find(b);
if(flag)
{
MAX+=cnt[x]*cnt[y]*w;
}
else
{
MIN+=cnt[x]*cnt[y]*w;
}
father[x] = y;
cnt[y]+=cnt[x];
}
int cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int main()
{
int n;
int t = ;
while(scanf("%d",&n)!=EOF)
{
init(n);
for(int i=; i<n; i++)
{
scanf("%I64u%I64u%I64u",&edge[i].u,&edge[i].v,&edge[i].w);
}
MAX = ,MIN = ;
sort(edge+,edge+n,cmp);
for(int i=; i<n; i++)
{
Union(edge[i].u,edge[i].v,edge[i].w,);
}
init(n);
for(int i=n-; i>=; i--)
{
Union(edge[i].u,edge[i].v,edge[i].w,);
}
printf("Case #%d: ",t++);
printf("%I64u\n",MAX-MIN);
}
return ;
}

hdu 5176(并查集)的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. VS2015中常用快捷键的修改推荐

    首先,在VS2015中修改快捷键的步骤如下: 工具--选项--环境--键盘. 如图: 然后就是修改快捷键:(以下是个人的使用习惯,仅供参考) 1.编辑.设置选定内容的格式.设置为ctrl+E,Ctrl ...

  2. Redis使用手册

    简介 Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型. Key-Value数据库. Redis面向互联网的方案提供了三种形式: 1.主从 主机进行写操作, ...

  3. 隐马尔可夫模型HMM

    隐马尔可夫模型HMM的探究 1 HMM基本概念1.1 定义1.2 观测序列生成过程1.3 HMM的三个问题2 概率计算算法2.1 直接计算算法2.2 前向算法forward algorithm2.3 ...

  4. 【COGS 461】[网络流24题] 餐巾 最小费用最大流

    既然是最小费用最大流我们就用最大流来限制其一定能把每天跑满,那么把每个表示天的点向T连流量为其所需餐巾,费用为0的边,然后又与每天的餐巾对于买是无限制的因此从S向每个表示天的点连流量为INF,费用为一 ...

  5. 整理一些JavaScript时间处理扩展函数

    在JavaScript中,时间处理是经常需要用到的.最近想要慢慢建立自己的代码库,整理了几个之前用到的js处理时间的函数,发出来跟大家分享一下,以后的使用中会不断增加和修改代码库. 把字符串转换为日期 ...

  6. Install Rancher server

    1.pre-requirement: sudo nmtui # sudo hostnamectl set-hostname <hostname> $ sudo hostnamectl se ...

  7. javascript中Date使用总结(转)

    //全局函数 Date //Date 类的静态方法 Date.parse Date.UTC //Date 对象的建立方法 new Date() new Date(毫秒数) new Date(标准时间格 ...

  8. es6+最佳入门实践(9)

    9.Iterator和for...of 9.1.Iterator是什么? Iterator又叫做迭代器,它是一种接口,为各种不同的数据结构提供统一的访问机制.这里说的接口可以形象的理解为USB接口,有 ...

  9. Java类的声明和访问介绍

    1.类的声明 类本身的声明:对类的声明来说,主要包括类的访问权限声明和非访问修饰符的使用.对于一个普通的Java类(POJO)来说,主要的访问权限修饰符只有两个public和默认权限,内部类可以有pr ...

  10. 51Nod 1212无向图最小生成树

    prim #include<stdio.h> #include<string.h> #define inf 0x3f3f3f3f ][]; ],lowc[]; ],int n) ...