思路:将边从大到小排序,判断向哪边连,能使总和最大。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 200010
#define Maxm 200010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x7fffffff
#define LL __int64
#define Mod 1000000007
using namespace std;
struct Edge{
int u,v;
LL val;
int operator <(const Edge &temp) const
{
return val>temp.val;
}
}p[Maxn];
int Set[Maxn];
LL sum[Maxn],num[Maxn];
int Find(int x)
{
if(x!=Set[x])
Set[x]=Find(Set[x]);
return Set[x];
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
Set[i]=i,num[i]=;
memset(sum,,sizeof(sum));
for(i=;i<n;i++)
scanf("%d%d%I64d",&p[i].u,&p[i].v,&p[i].val);
sort(p+,p+n);
int a,b;
for(i=;i<n;i++){
a=Find(p[i].u),b=Find(p[i].v);
LL sum1,sum2;
sum1=sum[a]+num[b]*p[i].val;
sum2=sum[b]+num[a]*p[i].val;
if(sum1>sum2){
Set[b]=a;
num[a]+=num[b];
sum[a]+=num[b]*p[i].val;
}
else{
Set[a]=b;
num[b]+=num[a];
sum[b]+=num[a]*p[i].val;
}
}
printf("%I64d\n",sum[Find()]);
}
return ;
}

hdu 4424 并查集的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. oracle 创建同义词

    1.语法: 同义词 (SYNONMY) CREATE SYNONYM同义词名FOR 表名; CREATE SYNONYM同义词名FOR 表名@数据库链接名; Create synonym synony ...

  2. 理解MapReduce哲学

    Google工程师将MapReduce定义为一般的数据处理流程.一直以来不能完全理解MapReduce的真义,为什么MapReduce可以“一般”? 最近在研究Spark,抛开Spark核心的内存计算 ...

  3. CodeForces 173B Chamber of Secrets spfa

    Chamber of Secrets 题目连接: http://codeforces.com/problemset/problem/173/B Description "The Chambe ...

  4. bzoj 1041: [HAOI2008]圆上的整点 数学

    1041: [HAOI2008]圆上的整点 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  5. Django 1.6 CBVs

    Django 1.6 最佳实践: 怎样正确使用 CBVs (Class-based views) Class-based views是Django为解决建站过程中的常见的呈现模式而建立的. 在这节中, ...

  6. 浏览器禁用Cookie,基于Cookie的会话跟踪机制失效的解决的方法

    当浏览器禁用Cookies时.基于Cookie的会话跟踪机制就会失效.解决的方法是利用URL重写机制跟踪用户会话. 在使用URL重写机制的时候须要注意.为了保证会话跟踪的正确性,全部的链接和重定向语句 ...

  7. cocos2d-x中CCScale9Sprite的另一种实现

    cocos2d 2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观 ...

  8. 关于测试Windows电脑端口的命令 —— telnet用法

    telnet服务在win7默认是打开的,如果没有打开要在电脑中打开. 命令格式:telnet ip port 例如:telnet 127.0.0.1 80 或者 telnet www.XXX.com ...

  9. Android实现定时器的方法

    一.Handler 和 Thread package com.lstech.app; import android.app.Activity; import android.os.Bundle; im ...

  10. LeetCode10 Regular Expression Matching

    题意: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...