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 (Java/Others)
Total Submission(s): 221 Accepted Submission(s): 91
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.
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<=109
1 2 1
2 3 2
5
1 2 2
2 3 5
2 4 7
3 5 4
Case #2: 17
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;
转一发官方题解:http://bestcoder.hdu.edu.cn/
题意:给一棵树,求任意{两点路径上的最大边权值-最小边权值}的总和。
解法:sigma(maxVal[i]−minVal[i])=sigma(maxVal)−sigma(minVal) ;所以我们分别求所有两点路径上的最大值的和,还有最小值的和。再相减就可以了。求最大值的和的方法用带权并查集,把边按权值从小到大排序,一条边一条边的算,当我们算第i 条边的时候权值为wi ,两点是ui,vi ,前面加入的边权值一定是小于等于当前wi 的,假设与ui 连通的点有a 个,与vi 连通的点有b 个,那么在a 个中选一个,在b 个中选一个,这两个点的路径上最大值一定是wi ,一共有a∗b 个选法,爱情经验值为a∗b∗wi 。
求最小值的和的方法类似。
槽点:
一:这题做数据的时候突然想到的把数据范围设在 unsigned long long 范围内,要爆 long long,这样选手在wa了之后可能心态不好找不到这个槽点,当是锻炼大家的心态和出现wa时的找错能力了,把这放在pretest..很良心的。
二,并查集的时候,用是递归的需要扩栈,一般上10w 的递归都需要,所以看见有几个FST在栈溢出的,好桑心。
12957565 | 2015-02-16 11:18:47 | Accepted | 5176 | 842MS | 6820K | 2033 B | G++ | czy |
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 150005
#define M 10005
//#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define ull unsigned long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int f[N];
ull cou[N];
ull suma,sumi;
int cnt; typedef struct
{
int a;
int b;
ull c;
}PP; PP p[N]; bool cmp(PP x,PP y)
{
return x.c<y.c;
} int find(int x)
{
int fa;
if(x!=f[x])
{
fa=find(f[x]);
f[x]=fa;
}
return f[x];
} void merge(int x,int y)
{
int a,b;
a=find(x);
b=find(y);
if(a==b) return;
f[b]=a;
cou[a]=cou[a]+cou[b];
} void ini()
{
suma=sumi=;
int i;
for(i=;i<=n;i++){
f[i]=i;
cou[i]=;
}
for(i=;i<n;i++){
scanf("%d%d%I64u",&p[i].a,&p[i].b,&p[i].c);
}
sort(p+,p+n,cmp);
} void solve()
{
int i;
int aa,bb;
for(i=;i<n;i++){
aa=find(p[i].a);
bb=find(p[i].b);
suma+=cou[aa]*cou[bb]*p[i].c;
merge(p[i].a,p[i].b);
}
for(i=;i<=n;i++){
f[i]=i;
cou[i]=;
}
for(i=n-;i>=;i--){
aa=find(p[i].a);
bb=find(p[i].b);
sumi+=cou[aa]*cou[bb]*p[i].c;
merge(p[i].a,p[i].b);
}
} void out()
{
printf("Case #%d: %I64u\n",cnt,suma-sumi);
cnt++;
} int main()
{
cnt=;
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
return ;
}
Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]的更多相关文章
- 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 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3038 How Many Answers Are Wrong(带权并查集)
传送门 Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, ...
- hdu 5441 (2015长春网络赛E题 带权并查集 )
n个结点,m条边,权值是 从u到v所花的时间 ,每次询问会给一个时间,权值比 询问值小的边就可以走 从u到v 和从v到u算不同的两次 输出有多少种不同的走法(大概是这个意思吧)先把边的权值 从小到大排 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集
C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...
- HDU 3038 How Many Answers Are Wrong(带权并查集,真的很难想到是个并查集!!!)
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU - 3038 How Many Answers Are Wrong (带权并查集)
题意:n个数,m次询问,每次问区间a到b之间的和为s,问有几次冲突 思路:带权并查集的应用.[a, b]和为s,所以a-1与b就能够确定一次关系.通过计算与根的距离能够推断出询问的正确性 #inclu ...
- hdu 3038 How Many Answers Are Wrong【带权并查集】
带权并查集,设f[x]为x的父亲,s[x]为sum[x]-sum[fx],路径压缩的时候记得改s #include<iostream> #include<cstdio> usi ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
随机推荐
- UVA 1661 Equation (后缀表达式,表达式树,模拟,实现)
题意:给出一个后缀表达式f(x),最多出现一次x,解方程f(x) = 0. 读取的时候用一个栈保存之前的结点,可以得到一颗二叉树,标记出现'X'的路径,先把没有出现'X'的子树算完,由于读取建树的时候 ...
- JS中鼠标左右键以及中键的事件
在三维场景中有时候需要判断鼠标的事件,除了使用的click事件,只有鼠标左键有效,而右键无效.而对于onmousedown.onmouseup的时候鼠标的事件左键/右键有效.详细请看w3c上的资料. ...
- C++_STL基础案例
C++ C++三种容器:list.vector和deque的区别:https://blog.csdn.net/gogokongyin/article/details/51178378 一.容器 小常识 ...
- C++系统学习之三:向量
标准库类型vector 定义:vector表示对象的集合,其中所有对象的类型都相同. 访问方式:索引 头文件:<vector> 本质:类模板 NOTE: 模板本身不是类或函数,相反可以将模 ...
- 牛客网NOIP赛前集训营-普及组(第二场)和 牛客网NOIP赛前集训营-提高组(第二场)解题报告
目录 牛客网NOIP赛前集训营-普及组(第二场) A 你好诶加币 B 最后一次 C 选择颜色 D 合法括号序列 牛客网NOIP赛前集训营-提高组(第二场) A 方差 B 分糖果 C 集合划分 牛客网N ...
- mysqldump指令说明
3种形式mysqldump [OPTIONS] database [tables]mysqldump [OPTIONS] -B | --databases [OPTIONS] DB1 [DB2 DB3 ...
- RN安卓原生模块
https://facebook.github.io/react-native/docs/native-modules-android.html RN实际就是依附在原生平台上,把各种各样的RN组件展示 ...
- Web安全XSS、CSRF和SQL注入
SQL注入 SQL注入是以用户的输入作为sql语句的一部分,如后端接收到用户的请求数据后,不经过数据转义,就把数据拼接到SQL中执行,容易导致SQL的语义被篡改,即受到攻击了. 解决办法是对接收的数据 ...
- Juqyer:$.ajax()方法详解
Jquery中的ajax方法参数总是记不住,这里记录一下. 最常用的属性是:url.data 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为S ...
- 元组-tuple
Python内置的元组数据类型:tuple tuple和list非常类似,但tuple一旦初始化就不能修改,因此它没有append(),insert()这样的方法 当定义一个tuple时,tuple的 ...