zoj 3659
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = ;
int p[maxn],num[maxn];
long long int sum[maxn];
struct Edge{
int u,v,c;
bool operator <(const Edge & rh) const{
return c > rh.c;
}
};
vector<Edge> e;
int n; int find(int x){
return x == p[x] ? x : p[x] = find(p[x]);
}
void init(){
e.clear();
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
p[i] = i;
num[i] = ;
}
}
int main()
{
//if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} while(cin>>n){
init();
for(int i=;i<n;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
e.push_back((Edge){u,v,c});
}
sort(e.begin(),e.end());
for(int i=;i<n-;i++){
int a = find(e[i].u), b = find(e[i].v);
long long int suma,sumb;
suma = sum[a] + (long long)num[b]*e[i].c ;
sumb = sum[b] + (long long)num[a]*e[i].c ;
if(suma >= sumb) {p[b] = a; sum[a] = suma; num[a] = num[a] + num[b];}
else {p[a] = b; sum[b] = sumb; num[b] = num[a] + num[b];}
//printf("%d %d %d %d\n",a,b,suma,sumb);
}
printf("%lld\n",sum[find()]);
}
}
//wa的原因就在于long long没使用正确;
//本题巧妙的用并查集,归纳递推;
zoj 3659的更多相关文章
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- zoj 3659 Conquer a New Region
// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- zoj 3659 并检查集合
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...
- zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)
题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
随机推荐
- 1 加到 100 的 时间复杂度 C#.
//1 加到 100 的 时间复杂度: ; ; ; i <= n; i++){ sum += i; } T() = ; //Initialize 'n'. T() = ; //Initializ ...
- c# 双问号运算
model.id??0 ??运算:如果运算符左边的值为NULL侧返回右边的值,否则返回左边的值
- java中怎么进行字符串替换?
String str = "test.doc"; String newStr = str.replaceAll("doc","html");
- PDF在线预览
1.所需插件jquery.media.js或者pdfobject.js 代码: <html> <head> <style type="text/css" ...
- 解决js浮点数计算bug
1.加 function add(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 动态树LCT小结
最开始看动态树不知道找了多少资料,总感觉不能完全理解.但其实理解了就是那么一回事...动态树在某种意思上来说跟树链剖分很相似,都是为了解决序列问题,树链剖分由于树的形态是不变的,所以可以通过预处理节点 ...
- sql语句复制表
1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable; ...
- import uno 错误
安装aeroolib 模块后,提示没有 uno 相关段一些模块, 原因是这些模块是 openoffice 中段,需要先安装 openoffice. 1:清除所有 libreoffice 软件, su ...
- Mvc Controller 单元测试 Mock User.Identity.Name
被测试的Action 包含 User.Identity.Name 代码,在写测试代码需要Mock ControllerContext对象 代码如下: var mock = new Mock<Co ...