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. javascript实现自动切换焦点功能学习

    当用户在表单中填写完当前字段后,能否自动将焦点跳转到下一个字段以方便用户输入? 为了增强易用性,加快数据输入的速度,可以在前一个文本框中的字符达到一定的设置的字符长度后(比如电话号码,身份证号等),用 ...

  2. [GitHub] - Unity Timer

    https://github.com/akbiggs/UnityTimer#unity-timer Run actions after a delay in Unity3D. This library ...

  3. REST接口设计规范

    URI格式规范 URI(Uniform Resource Identifiers) 统一资源标示符 URL(Uniform Resource Locator) 统一资源定位符 URI的格式定义如下: ...

  4. Java使用泛型的困顿

    原文有点儿胡说的意味,删了,有空再次更新这篇博文~

  5. [LeetCode] 56. Merge Intervals(vector sort)

    /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...

  6. 隐马尔可夫模型HMM

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

  7. hdu 1207 汉诺塔II (DP+递推)

    汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. C++——内存使用

    内存分配方式: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建.在执行函数时,函数内局部变量的存储单 ...

  9. C++——拷贝构造函数说明

    一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: [c-sharp] view plaincopy 1 int a = 100; 2 int b = a; 而类对 ...

  10. 写一个JavaScript“返回顶部”功能

    在web页面中,如果页面较高,为了方便用户快速地返回顶部,都会添加一个返回顶部按钮. 效果演示可以查看本页.如果页面有滚动高度,右下角就会有一个含有“返回顶部”字样的黑色背景半透明的小条条.点击这里“ ...