这道题以前好像在哪遇到过。

注意树的每一条边都是桥,所以,桥两端的点要到达对方是必须通过这条边的。于是,可以把边由小到大排序,利用并查集,这样,每加一条边就连通了一部分,而随着权值的增大,必定是桥两端到达对方经过的最大的边。于是总的权为左边集合数*右边集合数*桥的权值,就可以求出最大值和了。求最小值和相同。两者相减即为结果。

#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的更多相关文章

  1. 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 ...

  2. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  3. hdu 5176(并查集)

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  4. hdu 5176 The Experience of Love

    The Experience of Love  Accepts: 11  Submissions: 108  Time Limit: 4000/2000 MS (Java/Others)  Memor ...

  5. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. LA4788

    贪心 这个贪心不太懂啊 dfs返回子树需要的最小值,然后按需要减消耗排序,然后贪心选取即可. #include<bits/stdc++.h> using namespace std; ty ...

  2. Candies(差分约束系统)

    http://poj.org/problem?id=3159 思路:用O(V+ElogV)的Dijkstra算法求1到n的最短路.即用优先队列优化Dijkstra算法. #include <st ...

  3. go-swagger的简单使用

    一.下载go-swagger go-swagger 官方下载 根据不同个的操作系统选择对应的 二.添加环境变量 2.1 window swagger_windows_amd64.exe 将swagge ...

  4. Oracle占用内存过高解决办法

    1.cmd sqlplus system账户登录 2.show parameter sga; --显示内存分配情况 3.alter system set sga_max_size=200m scope ...

  5. myeclipse2014 破解步骤

    1.打开破解文件夹Myeclipse 2014 patch,运行run.bat文件 2.在破解界面中,usercode随便输入, systemid 在右边的SystemId按钮处,点击. 将自动生成一 ...

  6. node 连接MySQL及其分装, 连接池连接

    const mysql = require('mysql') const config = require('./../../config/config.default') var connectio ...

  7. 解决:惠普HP LaserJet Pro M126a MFP 驱动 安装失败,及其它同类打印机失败问题

    注意:如果在 Windows XP 系统下安装出错,请先安装WindowsXP KB971276-v3补丁后再安装装驱动. 下载地址:http://www.dyjqd.com/soft/KB97127 ...

  8. java Web(3)

    Servlet 是运行在Web服务器或应用服务器上的Java程序 在Web上创建动态内容的有效而强大的解决方案 由容器来管理生命周期与Web服务器交互 由Sun规范了其功能 Servlet部署: 一个 ...

  9. tomcat映射java目录 sever.xml

    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />        --> & ...

  10. PAT_A1154#Vertex Coloring

    Source: PAT A 1154 Vertex Coloring (25 分) Description: A proper vertex coloring is a labeling of the ...