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 并检查集合的更多相关文章

  1. HDU 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

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

  3. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  4. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  5. zoj 3659

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659 #include<cstdio> #inclu ...

  6. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  7. URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

    意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...

  8. CodeForces 277A Learning Languages (并检查集合)

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...

  9. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. 怎么样putty打开图形化管理工具,在终端上

    有时需要在putty这种图形终端开放的图形化管理工具将出现以下错误: [root@node2 ~]# Traceback (most recent call last): File "/us ...

  2. 全文检索引擎Solr 指南

    全文检索引擎Solr系列:第一篇:http://t.cn/RP004gl.第二篇:http://t.cn/RPHDjk7 .第三篇:http://t.cn/RPuJt3T

  3. UVA How Big Is It?

    题目例如以下: How Big Is It?  Ian's going to California, and he has to pack his things, including hiscolle ...

  4. Code::Blocks项目配置基础

    File 菜单 New :新建( Empty file/file . class . project . build target ) . Recent projects/files :近期打开的项目 ...

  5. IntelliJ IDEA15开发时设置中java complier 的问题

    Error:java: Compilation failed: internal java compiler error set中java complier 设置的问题  ,项目中有人用jdk1.6 ...

  6. 错误: 无法找到或可以不被加载到主类 Main

    于eclipse导入Javaproject,执行错误:错误: 无法找到或可以不被加载到主类 Main! 百思不得其解,该解决方案是非常在线,但不是正确的方式,最后,例如,由下列溶液: 打开debug ...

  7. Idea开发环境

    Idea开发环境中搭建Maven 1.配置Maven的环境变量 a.首先我们去maven官网下载Maven程序,解压到安装目录,如图所示: b.配置M2_HOME的环境变量,然后将该变量添加到Path ...

  8. 部署Redis主-从

    Redis主-从部署实践 0. 前言 这篇文章简要介绍Redis的主从部署,实现了一主二从,使用两个哨兵监控,以实现简单的HA,其中从库作为备机. 1. 部署 这里有三台服务器,其中239主机上的Re ...

  9. Ini文件帮助类

    .ini文件是什么 .ini 文件是Initialization File的缩写,就是初始化文件.在Windows系统中,其是配置文件所采用的存储格式(主要是system.ini,win.ini,sy ...

  10. POJ3467(预处理)

    Cross Counting Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1331   Accepted: 375 De ...