HDU 5176 The Experience of Love 带权并查集

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】

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;

【题意】

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

【分析】

直白地考虑,枚举任意两点是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[150010];

bool op(node a,node b)
{
    if (a.c==b.c) return a.a<b.a;
    else return a.c<b.c;
}

int father[150010],rank[150010]; 

void clean_father(int n)
{
    for (int i=1;i<=n;i++)
    {
        father[i]=i;
        rank[i]=1;
    }
} 

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=0;
    while(scanf("%d",&n)==1)
    {
        for (int i=1;i<n;i++)
            scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c);

        sort(&a[1],&a[n],op);
        clean_father(n);
        unsigned long long ans1=0;
        for (int i=1;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=0;
        for (int i=n-1;i>=1;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 0;
}

时间: 2024-12-20 07:03:35

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

HDU 5176 The Experience of Love (带权并查集 + 贪心)

The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 275    Accepted Submission(s): 111 Problem Description A girl named Gorwin and a boy named Vivin is a couple. They arrived

hdu 1829-A Bug&#39;s LIfe(简单带权并查集)

题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有,按题目要求输出.这题有点坑的地方在于输出上多了一行空行,不PE都没注意到. 思路: 用一个数组gender[i] 记录当前节点 i 与根节点的关系,parent[i]数组记录当前节点的父节点. 因为是带权并查集,在Find_Parent 时更新当前节点与根节点的关系,且路径压缩至根节点下, 所以不用

hdu 1558 Segment set【基础带权并查集+计算几何】

Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3599    Accepted Submission(s): 1346 Problem Description A segment and all segments which are connected with it compose a segment set

hdu 2818 Building Block【基础带权并查集】

Building Block Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3365    Accepted Submission(s): 1009 Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbe

hdu 3635 Dragon Balls 【基础带权并查集】

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3461    Accepted Submission(s): 1348 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

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

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr

Valentine&#39;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 (Java/Others)Total Submission(s): 221    Accepted Submission(s): 91 Problem Description A girl named Gorwin and a boy named Vivin is a couple. They arriv

HDU 3047 Zjnu Stadium 带权并查集

题目来源:HDU 3047 Zjnu Stadium 题意:给你一些人 然后每次输入a b c 表示b在距离a的右边c处 求有多少个矛盾的情况 思路:用sum[a] 代表a点距离根的距离 每次合并时如果根一样 判断sum数组是否符合情况 根不一样 合并两棵树 这里就是带权并查集的精髓 sum[y] = sum[a]-sum[b]+x 这里y的没有合并前b的根 #include <cstdio> #include <cstring> using namespace std; cons

HDU - 3038 How Many Answers Are Wrong (带权并查集)

题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用,[a, b]和为s,所以a-1与b就可以确定一次关系,通过计算与根的距离可以判断出询问的正确性 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 200010; int f[MAXN],a