The Experience of Love

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

【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<=10^9
 
【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】

【Sample Output】

Case #:
Case #:

【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;

【题意】

给出一张图(其实是一棵树),要求计算图上任意两点间路径上最长边的总和以及任意两点间路径上最短边的总和,求其差。

【分析】

直白地考虑,枚举任意两点是O(n^2)的复杂度,然后还要找到两点的路径,然后还要记下路径上最长/最短边,然后求和,这样的复杂度是完全不可能承受的。

考虑一个子图被一条长度为c的边分成两部分,c的两端点为a和b,且这两部分中的所有边长都小于c。

则a所连接的点的数量*b所连接的点的数量就是这个子图中,任意两点直接路径上最长边为c的点对的总数,同理只要递增/递减地枚举每一条边,可以根据这个方法算出以每一条边为路径最长边的点对的总数,乘上边长求和即可。

需要解决的就是如何快速得到每条边两端的点数,带权并查集可以解决这个问题。

根据这个想法,可以首先对所有边进行排序,用并查集维护点对集关系,用权保存下每个点集中点的总数,用于每次得到一条边两端连接的点的个数。

最长边总和与最短边总和的思路完全一样,改一下代码方向即可。

【注意】

这里还有个坑是注意数据范围,要开到unsigned long long

/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU5176
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef struct nod
{
int a,b,c;
} node; node a[]; bool op(node a,node b)
{
if (a.c==b.c) return a.a<b.a;
else return a.c<b.c;
} int father[],rank[]; void clean_father(int n)
{
for (int i=;i<=n;i++)
{
father[i]=i;
rank[i]=;
}
} int getfather(int x)
{
if (father[x]!=x) father[x]=getfather(father[x]);
return father[x];
} void link(int x,int y)
{
int xx=getfather(x),yy=getfather(y);
father[xx]=yy;
rank[yy]+=rank[xx]; } int main()
{
int n,t=;
while(scanf("%d",&n)==)
{
for (int i=;i<n;i++)
scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c); sort(&a[],&a[n],op);
clean_father(n);
unsigned long long ans1=;
for (int i=;i<n;i++)
{
int x=getfather(a[i].a),y=getfather(a[i].b);
ans1+=(unsigned long long)rank[x]*rank[y]*a[i].c;
link(x,y);
} clean_father(n);
unsigned long long ans2=;
for (int i=n-;i>=;i--)
{
int x=getfather(a[i].a),y=getfather(a[i].b);
ans2+=(unsigned long long)rank[x]*rank[y]*a[i].c;
link(x,y);
} t++;
printf("Case #%d: %llu\n",t,ans1-ans2);
} return ;
}

HDU 5176 The Experience of Love 带权并查集的更多相关文章

  1. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

  2. Travel(HDU 5441 2015长春区域赛 带权并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  3. 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 ...

  4. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  5. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  6. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. 【带权并查集】HDU 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...

  8. HDU 3047 带权并查集 入门题

    Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...

  9. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

随机推荐

  1. CCF考试真题题解

    CCF考试认证:题解参考博客http://blog.csdn.net/u014578266/article/details/45221841 问题描述 试题编号: - 试题名称: 图像旋转 时间限制: ...

  2. openwrt 中make的使用

    make 命令简单说明 make V=99 V=99表示输出详细的debug信息 make world 表示编译所有 make j=2 V=99 如是多核CPU,加j=2 选项理论上能加快编译速度 m ...

  3. openwrt默认不开启wifi

    Openwrt默认不开启wifi,要开启的话, 修改这个文件: openwrt/trunk/package/kernel/mac80211/files/lib/wifi/mac80211.sh. 滚到 ...

  4. linux top 命令---VIRT,RES,SHR,虚拟内存和物理内存(

    VIRT,RES,SHR,虚拟内存和物理内存(转) VIRT: 1.进程"需要的"虚拟内存大小,包括进程使用的库.代码.数据,以及malloc.new分配的堆空间和分配的栈空间等: ...

  5. Moss列表查询,删除条目,更新条目。

    基于Query语句的列表查询 function retrieveListItems(itemId) {    var siteUrl=_spPageContextInfo.webServerRelat ...

  6. flash跨域策略文件crossdomain.xml配置详解

    来源:http://www.2cto.com/Article/201108/100008.html 0x01 简介 flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了 ...

  7. php中的PHP_EOL换行符

      看手册时发现PHP_EOL这个变量,查了下资料,原来是相当于换行符 在unix系列用 \n 在windows系列用 \r\n 在mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代 ...

  8. Inno Setup入门(二)——修改安装过程中的图片

    修改安装过程中的图片 一般编译之后,安装过程中出现在左边图片是是下图这个样子的: 其实也可以修改它,只需要在setup段中作一点稍微的修改,加一行代码即可: [setup] AppName=Test ...

  9. wl18xx编译的时候出现WARNING: "simple_open" WARNING: "wl12xx_get_platform_data"

     ................................................................................................... ...

  10. mvc动态生成a标签,多个属性,多个querystring

    1*服务端 客户端 跳转的url 2*服务端 客户端 跳转的url 3*服务端 客户端  跳转的url  4*服务端 客户端 跳转的url