HDU 5176 The Experience of Love 带权并查集
The Experience of Love
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
【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 带权并查集的更多相关文章
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
- Travel(HDU 5441 2015长春区域赛 带权并查集)
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- 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 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- 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 ...
- 【带权并查集】HDU 3047 Zjnu Stadium
http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
随机推荐
- FUSE
FUSE is particularly useful for writing [ vritual ] file system. Unlike traditional filesystem that ...
- CAFFE中训练与使用阶段网络设计的不同
神经网络中,我们通过最小化神经网络来训练网络,所以在训练时最后一层是损失函数层(LOSS), 在测试时我们通过准确率来评价该网络的优劣,因此最后一层是准确率层(ACCURACY). 但是当我们真正要使 ...
- 两台linux利用heartbeat+drbd 完美实现双机热备
一直想做基于linux的双机热备,一直没有时间和机会.一直以为只要做双机热备的实验就必须两台机器外接一个存储.甚至一个月以前在学习keepalived的时候还在琢磨keepalvied去掉哪些条件可以 ...
- Hibernate写入Oracle Date类型处理
Hibernate写入Oracle数据库时,数据库设计字段为Date类型时,只能保存年月日,不能保存时分秒,如果要保存时分秒,需修改Hibernate.cfg.xml文件 <property n ...
- 熟练使用NTFS的文件链接技术
硬链接和软链接介绍: 硬连接指向的是i节点(iNode),而软连接指向的是路径(Path) ,又称符号链接.硬链接可理解为对i节点的引用,最初的文件名与所有的硬链接地位是对等的,比如为文件a建立了硬链 ...
- OPenGL中的缓冲区对象
引自:http://blog.csdn.net/mzyang272/article/details/7655464 在许多OpenGL操作中,我们都向OpenGL发送一大块数据,例如向它传递需要处理的 ...
- 转载:数位DP模板
// pos = 当前处理的位置(一般从高位到低位) 2 // pre = 上一个位的数字(更高的那一位) 3 // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, 4 ...
- Jquery 控制元素 上 下 移动
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- How to write a probeContentType() and Usage?
Files.probeContentType() is an instance of FileTypeDetector class's abstract method String FileTypeD ...
- RACSignal的Subscription深入
ReactiveCocoa是一个FRP的思想在Objective-C中的实现框架,目前在美团的项目中被广泛使用.对于ReactiveCocoa的基本用法,网上有很多相关的资料,本文不再讨论.RACSi ...