zoj 3659 并检查集合
http://acm.zju.edu.cn/onlinejudge/showProblem.do?
problemId=4882
现在在牡丹江,明天regional直播比赛,我会在一个月内退休。求祝福
今天做的热身赛非常紧张称号,总是错的字,晚上写写代码练练手
脑子还是不好使。没想到能够并查集
思路:题目中的操作导致的一个结果是,一条边会成为一个集合的w,---- 假设能想到这里可能就能想到并查集吧
WA了由于假设father[x]==x并不能表示x中仅仅有一个数啊。
。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; #define ll long long
#define IN(s) freopen(s,"r",stdin)
const int MAXN = 200000+200;
struct edge{
int u,v,w;
bool operator < (const edge &c)const{
return w>c.w;
}
}e[MAXN*2]; int n,fa[MAXN],num[MAXN];
ll co[MAXN]; int Find(int x)
{
if(x != fa[x])fa[x]=Find(fa[x]);
return fa[x];
} void init()
{
for(int i=0;i<=n;i++)
fa[i]=i,num[i]=1,co[i]=0LL;
} void un(int x,int y,int w)
{
int xf=Find(x);
int yf=Find(y);
if(xf == yf)return;
/*if(xf == x && yf == y && num[xf]==1 && num[yf]==1)
{
num[xf]+=num[yf];
fa[yf]=xf;
co[xf]=(ll)w*num[yf]+co[xf];
return;
}*/
ll cosx=(ll)w*num[yf]+co[xf];
ll cosy=(ll)w*num[xf]+co[yf];
if(cosx > cosy)
{
fa[yf]=xf;
num[xf]+=num[yf];
co[xf]=cosx;
//co[y]+=w*num[x];
return;
}
fa[xf]=yf;
num[yf]+=num[xf];
co[yf]=cosy;
} ll solve()
{
init();
for(int i=1;i<n;i++)
{
un(e[i].u,e[i].v,e[i].w);
}
return co[Find(1)];
} int main()
{
//IN("zoj3659.txt");
while(~scanf("%d",&n))
{
for(int i=1;i<n;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
sort(e+1,e+n);
printf("%I64d\n",solve());
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
zoj 3659 并检查集合的更多相关文章
- HDU 1272 小希迷宫(并检查集合)
意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...
- 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(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- zoj 3659
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659 #include<cstdio> #inclu ...
- hdu1325 Is It A Tree?并检查集合
pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...
- URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)
意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...
- CodeForces 277A Learning Languages (并检查集合)
A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 站点搭建从零開始(四) server的配置
原文发表在我的个人站点,欢迎大家訪问~转载请保留本段,或注明原文:http://www.hainter.com/website-primer-4 域名解析设置好了,通常我们就能够訪问我们的网站了,上篇 ...
- Java 并发专题 :闭锁 CountDownLatch 之一家人一起吃个饭
最近一直整并发这块东西,顺便写点Java并发的例子,给大家做个分享,也强化下自己记忆. 每天起早贪黑的上班,父母每天也要上班,话说今天定了个饭店,一家人一起吃个饭,通知大家下班去饭店集合.假设:3个人 ...
- 设计模式——工厂模式(Factory)
要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 1.概念 工厂模式定 ...
- strategy pattern
- RH033读书笔记(5)-Lab 6 Exploring the Bash Shell
Lab 6 Exploring the Bash Shell Sequence 1: Directory and file organization 1. Log in as user student ...
- 採訪The Molasses Flood:BioShock Infinite 游戏之后又一大作
Xsolla有幸与Flame in the Flood游戏的开发人员之中的一个-----Forrest Dowling进行了採訪,Flame in the Flood这款非常棒的游戏在Kickstar ...
- HDU 1874 畅通公程续 (最短路 水)
Problem Description 某省自从实行了非常多年的畅通project计划后,最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择,而某些方案 ...
- JDBC调用存储过程
一. JDBC调用存储过程 (1)使用存储过程SQL编写的程序代码,等一段语句和控制流语句.在创建时被编译成机器代码和存储在数据库中的client转让. 存储过程具有以下优势: 1.所生成的机器代码被 ...
- ECharts SSH+JQueryAjax+Json+JSP在数据库中的数据来填充ECharts在
1导入包.设定SSH框架. 进口JQuery的JS包.<script src="JS/jquery-1.7.1.js"></script> 导入EChart ...
- java.io.NotSerializableException
结果发现序列不成功非静态内部类时的序列中,出现以下异常: java.io.NotSerializableException: com.tang.sharedpreferencesdemo.MainAc ...