Conquer a New Region


Time Limit: 5 Seconds      Memory Limit: 32768 KB

The wheel of the history rolling forward, our king conquered a new region in a distant continent.

There are N towns (numbered from 1 to N) in this region connected by several roads. It's confirmed that there is exact one route between any two towns. Traffic is important while controlled colonies are far away from the local country. We define the capacity C(i, j) of a road indicating it is allowed to transport at most C(i, j) goods between town i and town j if there is a road between them. And for a route between i and j, we define a value S(i, j) indicating the maximum traffic capacity between i and j which is equal to the minimum capacity of the roads on the route.

Our king wants to select a center town to restore his war-resources in which the total traffic capacities from the center to the other N - 1 towns is maximized. Now, you, the best programmer in the kingdom, should help our king to select this center.

Input

There are multiple test cases.

The first line of each case contains an integer N. (1 ≤ N ≤ 200,000)

The next N - 1 lines each contains three integers a, b, c indicating there is a road between town a and town b whose capacity is c. (1 ≤ a, b ≤ N, 1 ≤ c ≤ 100,000)

Output

For each test case, output an integer indicating the total traffic capacity of the chosen center town.

Sample Input

4
1 2 2
2 4 1
2 3 1
4
1 2 1
2 4 1
2 3 1

Sample Output

4
3

Contest: The 2012 ACM-ICPC Asia Changchun Regional Contest

题意:给你一棵树,s[i,j]表示从i点到j点的路径权值的最小值,f[i]表示i点到树上除i外所有的点j的s[i,j]。求最大的f[i]是多少。

思路1:先把所有边按从大到小排序。一条边一条边地加入树中,每次合并两个集合(其实就是两个并查集,也就是两棵树),f[i]表示以i为根的这棵子树每个节点都能获得比原来多f[i]的值。不断合并两个集合,然后用类似并查集压缩路径的方法压缩f[i]。最后就能得到所有f[i]的值,再从中选一个最大的,即答案。

 /*
* Author: Joshua
* Created Time: 2014年10月05日 星期日 10时01分55秒
* File Name: e.cpp
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 200001
typedef long long LL;
int n;
struct edge
{
int u,v,w;
bool operator < (const edge& p) const
{
return w>p.w;
}
void input()
{
scanf("%d%d%d",&u,&v,&w);
}
} e[maxn]; int fa[maxn],size[maxn];
bool vis[maxn];
LL f[maxn];
void init()
{
for (int i=;i<n;++i)
e[i].input();
sort(e+,e+n);
for (int i=;i<=n;++i)
{
fa[i]=i;
size[i]=;
}
memset(f,,(n+)<<);
memset(vis,,n+);
} int gf(int x,int t)
{
if (vis[x]) return f[x];
if (t) vis[x]=true;
if (fa[x]==x) return x;
int temp,tf=fa[x];
temp=gf(tf,t);
if (!t)
{
if (temp!=tf) f[x]+=f[tf];
}
else f[x]+=f[tf];
return fa[x]=temp;
} void solve()
{
int u,v,fu,fv;
LL ans=,w;
for (int i=;i<n;++i)
{
u=e[i].u; v=e[i].v; w=e[i].w;
fu=gf(u,);
fv=gf(v,);
f[fu]+=size[fv]*w;
f[fv]+=(size[fu]*w-f[fu]);
size[fu]+=size[fv];
fa[fv]=fu;
}
for (int i=;i<=n;++i)
{
if (!vis[i]) u=gf(i,);
ans=max(ans,f[i]);
}
cout<<ans<<endl;
} int main()
{
while (scanf("%d",&n)!=EOF)
{
init();
solve();
}
return ;
}

思路2:类似于思路1,但既然我们只要求一个最大的,那么其他的值求出来就很浪费了。因此我们将思路1中的f[i]改为以i为根的并查集中,当前获得的最大值的那个点的值。因为在不断合并中,这个并查集中的每个点获得的数值都是一样的,所以当前最大的那个点肯定在最后也是最大的。因此我们做的就是每次合并两个集合,选出一个最大的。然后不断合并即可。最后的答案肯定能在最后一次合并中找出。

 /*
* Author: Joshua
* Created Time: 2014年10月05日 星期日 20时01分10秒
* File Name: e.cpp
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 200001
typedef long long LL;
int n;
struct edge
{
int u,v,w;
bool operator < (const edge& p) const
{
return w>p.w;
}
void input()
{
scanf("%d%d%d",&u,&v,&w);
}
} e[maxn]; int fa[maxn],size[maxn];
LL f[maxn];
void init()
{
for (int i=;i<n;++i)
e[i].input();
sort(e+,e+n);
for (int i=;i<=n;++i)
{
fa[i]=i;
size[i]=;
}
memset(f,,(n+)<<);
} int gf(int x)
{
int &t=fa[x];
return t= ( t==x ? x:gf(t));
} void solve()
{
int u,v,fu,fv;
LL ans=,w,tu,tv;
for (int i=;i<n;++i)
{
u=e[i].u; v=e[i].v; w=e[i].w;
fu=gf(u);
fv=gf(v);
tu=f[fu]+size[fv]*w;
tv=f[fv]+size[fu]*w;
if (tu>tv)
{
fa[fv]=fu;
size[fu]+=size[fv];
f[fu]=tu;
}
else
{
fa[fu]=fv;
size[fv]+=size[fu];
f[fv]=tv;
}
}
ans=max(tu,tv);
cout<<ans<<endl;
} int main()
{
while (scanf("%d",&n)!=EOF)
{
init();
solve();
}
return ;
}

zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest的更多相关文章

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

  2. HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online)

    HDU 4291 A Short problem(2012 ACM/ICPC Asia Regional Chengdu Online) 题目链接http://acm.hdu.edu.cn/showp ...

  3. zoj 3659 Conquer a New Region

    // 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...

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

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

  5. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

  6. 2016 ACM ICPC Asia Region - Tehran

    2016 ACM ICPC Asia Region - Tehran A - Tax 题目描述:算税. solution 模拟. B - Key Maker 题目描述:给出\(n\)个序列,给定一个序 ...

  7. HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )

    Sum of divisors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)

    Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, an ...

  9. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

随机推荐

  1. Golang 基于libpcap/winpcap的底层网络编程——gopacket安装

    Go简介 Go是一种编译型语言,它结合了解释型语言的游刃有余,动态类型语言的开发效率,以及静态类型的安全性. 语法类似C/C++,但是又带有一点python的味道 其中个人认为最出色的特点就是他的包管 ...

  2. iOS开发中frame与bounds的区别

    闲话不多说,先上两张图,大伙们就已经明白了: 显示出来的效果是这样子滴:  总结: 要理清这两者的区别,最主要的要理解一下几个概念:frame可以理解为可视的范围,而bounds可以理解为可视范围内的 ...

  3. (转)SQL Server基础之存储过程(清晰使用)

    阅读目录 一:存储过程概述 二:存储过程分类 三:创建存储过程 1.创建无参存储过程 2.修改存储过程 3.删除存储过程 4.重命名存储过程 5.创建带参数的存储过程   简单来说,存储过程就是一条或 ...

  4. 【HTML】table元素

    1.最简单的table <table> <tr> <th></th> </tr> <tr> <td></td& ...

  5. Hibernate的事务处理机制和flush方法的用法

    关于在使用hibernate在提交事务时常遇到的异常: an assertion failure occured (this may indicate a bug in Hibernate, but ...

  6. java迭代器浅析

    简介 迭代器是遍历容器的一种常用方法,它屏蔽了容器的实现细节,无需暴露数据结构内部,就可以对容器进行遍历,迭代器本身也是一种设计模式,迭代是一种特殊的遍历方式 Iterator 在java中,迭代器接 ...

  7. 实例甜点 Unreal Engine 4迷你教程(1)之如何用C++将纹理绘制在UserWidget的Image小部件上

    完成本迷你教程之前,请前往完成以下迷你教程: 无前置教程待完成. 本教程适合的人群: 初学者,具有开发经验两周: 本示例的目的:为了在代码中实现UMG中的这个功能: 说明:这是一些列迷你教程的首篇,所 ...

  8. 【bzoj1458】士兵占领

    Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放 ...

  9. 灵玖软件Nlpir Parser语义智能内容过滤

    Internet是全球信息共享的基础设施,是一种开放和面向 所有用户的技术.它一方面要保证信息方便.快捷的共享;另一方面要防止垃圾信息的传播.网络内容分析是一种管理信 息传播的重要手段.它是网络信息安 ...

  10. Linux 系统下安装 rz/sz 命令及使用说明

    Linux 系统下安装 rz/sz 命令及使用说明 rz/sz命令,实现将本地的文件上传到服务器或者从服务器上下载文件到本地,但是很多Linux系统初始并没有这两个命令,以下为安装和使用的具体步骤: ...