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. [luogu P2184] 贪婪大陆 [树状数组][线段树]

    题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前方是变异了的超级蚂蚁. 小 ...

  2. GetStdHandle 函数--获取标准设备的句柄

    #GetStdHandle函数 来源:https://msdn.microsoft.com/en-us/library/ms683231.aspx 作用 该函数用于取得指定的标准设备的句柄(标准输入, ...

  3. swift3.0 coreData的使用-日记本demo

    效果 需求分析 基于官方MasterDetail模板,官方写了很多复杂的coredata逻辑,在此基础上快速开发简单的日记本程序. - 主要功能:增.删.改.查 - 界面用默认的界面,将detail页 ...

  4. Cocoapods最新安装教程

    一.CocoaPods简介 每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如 Java 语言的 Maven,nodejs 的 npm.随着 iOS 开发者的增多,业界也出现了为 iOS 程序提 ...

  5. java迭代器浅析

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

  6. 使用gitLab 或 github 关联本地仓库

    要先在git里面注册自己的邮箱 然后: git commit -m 是为本次提交命名 刷新gitLab 发现更新了

  7. 【整理】01. localhost_access_log 记录post请求参数

    环境:apache-tomcat-7.0.57 利用Filter过去request请求参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  8. year:2017 month:7 day:20

    2017-07-20 JavaScript(Dom) 1:获取节点对象 document.getElementById("html元素的id") document.getEleme ...

  9. Python应用场景

    Web应用开发 Python经常被用于Web开发.比如,通过mod_wsgi模块,Apache可以运行用Python编写的Web程序.Python定义了WSGI标准应用接口来协调Http服务器与基于P ...

  10. Jenkins+ANT+Jmeter 接口测试的实践(转载)

    转载地址:https://testerhome.com/topics/5262 1.前言 最近感觉大家都在讲Jenkins+jmeter+ant或maven的使用,但没有说到具体怎么投入到项目使用,只 ...