HDU 5176
这道题以前好像在哪遇到过。
注意树的每一条边都是桥,所以,桥两端的点要到达对方是必须通过这条边的。于是,可以把边由小到大排序,利用并查集,这样,每加一条边就连通了一部分,而随着权值的增大,必定是桥两端到达对方经过的最大的边。于是总的权为左边集合数*右边集合数*桥的权值,就可以求出最大值和了。求最小值和相同。两者相减即为结果。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define LL unsigned __int64
using namespace std; const int N=150100; int pre[N];
LL cnt[N];
struct Edge{
int u,v;
LL c;
}edge[N];
bool cmp1(Edge a,Edge b){
if(a.c>b.c) return true;
return false;
}
bool cmp2(Edge a,Edge b){
if(a.c<b.c) return true;
return false;
} int n; int findr(int u){
int r,t=u;
while(pre[t]!=-1){
t=pre[t];
}
r=t;
while(u!=r){
t=pre[u];
pre[u]=r;
u=t;
}
return r;
} int main(){
int t=0;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n-1;i++){
scanf("%d%d%I64u",&edge[i].u,&edge[i].v,&edge[i].c);
}
for(int i=1;i<=n;i++){
pre[i]=-1,cnt[i]=1;
}
LL ans_max=0,ans_min=0;
int u,v;
sort(edge,edge+n-1,cmp2);
for(int i=0;i<n-1;i++){
u=findr(edge[i].u),v=findr(edge[i].v);
ans_max+=(cnt[u]*cnt[v]*edge[i].c);
if(cnt[u]>cnt[v]){
pre[v]=u;
cnt[u]+=cnt[v];
}
else {
pre[u]=v;
cnt[v]+=cnt[u];
}
}
for(int i=1;i<=n;i++){
pre[i]=-1,cnt[i]=1;
}
for(int i=n-2;i>=0;i--){
u=findr(edge[i].u),v=findr(edge[i].v);
ans_min+=(cnt[u]*cnt[v]*edge[i].c);
if(cnt[u]>cnt[v]){
pre[v]=u;
cnt[u]+=cnt[v];
}
else {
pre[u]=v;
cnt[v]+=cnt[u];
}
}
printf("Case #%d: %I64u\n",++t,ans_max-ans_min);
}
return 0;
}
HDU 5176的更多相关文章
- 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 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- hdu 5176(并查集)
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- hdu 5176 The Experience of Love
The Experience of Love Accepts: 11 Submissions: 108 Time Limit: 4000/2000 MS (Java/Others) Memor ...
- HDU 1081 To The Max【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- 线性回归(最小二乘法、批量梯度下降法、随机梯度下降法、局部加权线性回归) C++
We turn next to the task of finding a weight vector w which minimizes the chosen function E(w). Beca ...
- bzoj1593 [Usaco2008 Feb]Hotel 旅馆(线段树)
1593: [Usaco2008 Feb]Hotel 旅馆 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 758 Solved: 419[Submit ...
- Python 34(进程了解)
一:僵尸进程与孤儿进程 测试程序: 基本概念: 一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中. ...
- git上
## 建立本地版本库 ## 本地版本库与远程关联 ## 修改文件并提交 ## 创建分支,修改文件合并至master 1. git的由来 linux系统是很多开发者贡献代码不断完善的,linux的创始人 ...
- HDU 4901 DP
我觉得这个DP挺难的...然而这只是lydrainbowcat学长幻灯片上的第一题-- 明天考试要GG. 题意: 给你一个序列,让你选出两个集合S和T.保证S里的数都在T里的数的左边.求一共有多少个集 ...
- CSS3之 transform和animation区别
CSS3 有3种和动画相关的属性:transform, transition, animation.其中 transform 描述了元素静态样式.而transition 和 animation 却都能 ...
- bootstrap的栅格系统和响应式工具
关于bootstrap的响应式布局,昨天看了杨老师的视频教学https://www.bilibili.com/video/av18357039豁然开朗,在这里记录一下 一:meta标签的引用 < ...
- vue-pdf的使用方法及解决在线打印预览乱码
最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...
- 《Linux程序设计》笔记(一)入门
1. 头文件 使用-I标志来包含头文件. gcc -I/usr/openwin/include fred.c 2. 库文件 通过给出 完整的库文件路径名 或 用-l标志 来告诉编译器要搜索的库文件. ...
- 三维重建面试13X:一些算法试题-今日头条AI-Lab
被人牵着鼻子走,到了地方还墨明棋妙地吃一顿砖头.今日头条AI-Lab,其实我一直发现,最擅长的还是点云图像处理,且只是点云处理. 一.C++题目 New 与Malloc的区别: ...