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. Python之路:爬虫之urllib库的基本使用和高级使用

    关于爬虫自己一直在看,所以时间太慢,这才第二更,有等不及的小伙伴可以慢慢的品尝了,在看下面的之前,建议先把上一章看一下.以下是关于python的Urllib的基础和高级用法. 1.如何扒下一个网站,用 ...

  2. golang vs java

    http://programmers.stackexchange.com/questions/83780/how-fast-can-go-go In terms of language design, ...

  3. 把perl脚本编译成exe

    来源:http://www.veryhuo.com/a/view/38338.html 今天想把 perl 脚本编译成 exe 以便脱离 perl 解释器独立运行.都可以生成PERL的PE文件,在PE ...

  4. jsp 页面取值

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. u盘烧写后实际容量变小了

    百度了一下 : http://jingyan.baidu.com/article/d45ad148f383ea69552b808a.html 百度下载 USBoot 打开软件 列表中选择你的U盘,点击 ...

  6. strictmode

    最新的Android平台中(Android 2.3起),新增加了一个新的类,叫StrictMode(android.os.StrictMode).这个类可以用来帮助开发者改进他们编写的应用,并且提供了 ...

  7. spring 入门级程序示例

    public interface Action { public String execute(String str); } 接口 public class UpperAction implement ...

  8. 12c meet sysdba meet ORA-01017: invalid username/password; logon denied

    checklist: 1.12c: threaded_execution=true Prevents OS Login As Sysdba 2. The following database para ...

  9. css 重新学习系列(2)

    摘自: http://www.cnblogs.com/liuzhaoyang/articles/3289456.html Position定位:relative | absolute 定位一直是WEB ...

  10. Hibernate 系列教程12-继承-Join策略

    Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...