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 ...
随机推荐
- LeetCode OJ 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- struct函数
//今天老师叫用一个struct函数做一个函数指针循环 突然我都不知道struct函数怎么用了 害的我下去查了很多资料 第一个定义法: int fun1(){ struct student{ ...
- ./encrypt: error while loading shared libraries: libcrypto.so.10:
./encrypt: error while loading shared libraries: libcrypto.so.10:
- WinForm 子窗体在父窗体范围内移动,不能出父窗体 摘自于网络
详细解释:1, 主窗体Form1属性IsMdiContainer设为True,并添加ToolStrip控件, Toolstrip中添加一个按钮toolStripButton1. 2,添 ...
- vs2010在进行数据架构比较时报'text lines should not be null'错误
通过VS2010进行服务器数据库和本地数据库比较架构(都是sql server 2008 R2)时,弹出“text lines should be not null”错误,如下图: 解决方法:在Vis ...
- c语言库函数
#include <stdio.h> #define LENGTH 100 main() { FILE *fd; char str[LENGTH]; fd = fopen("he ...
- 11g init DB software and database
oadmin->administrator2.169set ORACLE_HOME=C:\app\oracle\product\11.2.0\dbhome_1set ORACLE_SID=csm ...
- 转 vi 技巧和诀窍:令人刮目相看的 10 个超酷命令
for ksh vi 编辑器的许多选项可以控制编辑会话的外观和感觉.使用 :set 命令修改 vi 中的会话设置.按 Escape 键进入命令模式之后,可以使用 :set all 命令显示选项和设置的 ...
- Interview with Oleg
Interview with Oleg time limit per test 1 second memory limit per test 256 megabytes input standard ...
- java 持有对象
1.泛型和类型安全的容器 ArrayList,可以自动扩充大小的数组,add插入对象,get访问对象,size查看对象数目. 1 /** 2 * 泛型和类型安全的容器 3 * 2016/5/6 4 * ...