hdu 5176(并查集)
The Experience of Love
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 645 Accepted Submission(s): 216
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.
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
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.
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;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef unsigned long long LL;
const int N = ;
struct Edge
{
LL u,v;
LL w;
} edge[N];
LL father[N];
LL cnt[N];
LL MAX,MIN;
LL _find(LL x)
{
if(x!=father[x]){
father[x] = _find(father[x]);
}
return father[x];
}
void init(int n)
{
for(int i=; i<=n; i++)
{
father[i] = i;
cnt[i] = ;
}
}
void Union(LL a,LL b,LL w,int flag)
{
LL x = _find(a);
LL y = _find(b);
if(flag)
{
MAX+=cnt[x]*cnt[y]*w;
}
else
{
MIN+=cnt[x]*cnt[y]*w;
}
father[x] = y;
cnt[y]+=cnt[x];
}
int cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int main()
{
int n;
int t = ;
while(scanf("%d",&n)!=EOF)
{
init(n);
for(int i=; i<n; i++)
{
scanf("%I64u%I64u%I64u",&edge[i].u,&edge[i].v,&edge[i].w);
}
MAX = ,MIN = ;
sort(edge+,edge+n,cmp);
for(int i=; i<n; i++)
{
Union(edge[i].u,edge[i].v,edge[i].w,);
}
init(n);
for(int i=n-; i>=; i--)
{
Union(edge[i].u,edge[i].v,edge[i].w,);
}
printf("Case #%d: ",t++);
printf("%I64u\n",MAX-MIN);
}
return ;
}
hdu 5176(并查集)的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
随机推荐
- ssh问题_1
昨天配置了虚拟机的ssh,可以相互连接,但是今天早上就不行了,遇到如下错误 [slave1@hadoop ~]$ scp .ssh/id_rsa.pub master@hadoop.master:~/ ...
- 甲级1002 A+B for Polynomials (25)
题目描述: This time, you are supposed to find A+B where A and B are two polynomials. Input Each input fi ...
- vue 介绍的拓展
计算属性 <div id="example"> <p>Original message: "{{ message }}"</p&g ...
- ELK6.2.4集群
ELK6.2.4集群安装使用 https://www.cnblogs.com/frankdeng/p/9139035.html 一 简介 Elasticsearch是一个高度可扩展的开源全文搜索和分析 ...
- DataBase -- Operator
TOP子句用于规定要返回的记录的数目. SELECT TOP number | percent column_name FROM table_name LIKE操作符用于在WHERE子句中搜索列的指定 ...
- [UVA1402]Robotic Sort;[SP2059]CERC07S - Robotic Sort([洛谷P3165][CQOI2014]排序机械臂;[洛谷P4402][Cerc2007]robotic sort 机械排序)
题目大意:一串数字,使用如下方式排序: 先找到最小的数的位置$P_1$,将区间$[1,P_1]$反转,再找到第二小的数的位置$P_2$,将区间$[2,P_2]$反转,知道排序完成.输出每次操作的$P_ ...
- [洛谷P3690]【模板】Link Cut Tree (动态树)
题目大意:给定$n$个点以及每个点的权值,要你处理接下来的$m$个操作.操作有$4$种.操作从$0到3编号.点从1到n编号. $0,x,y$:代表询问从$x$到$y$的路径上的点的权值的$xor$和. ...
- Android 异步通信:图文详解Handler机制工作原理
前言 在Android开发的多线程应用场景中,Handler机制十分常用 今天,我将图文详解 Handler机制 的工作原理,希望你们会喜欢 目录 1. 定义 一套 Android 消息传递机制 2. ...
- MFC 相关类、函数
timeSetEvent()函数 CRectTracker类的使用 SetLocalTime设置本地时间 AdjustTokenPrivileges启用权限
- CCmdUI
原文链接地址:http://blog.csdn.net/luicha/article/details/6771185 CCmdUI是一个只被使用于ON_UPDATECOMMAND_UI消息的响应函数中 ...