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. 谈一款MOBA游戏《码神联盟》的服务端架构设计与实现

    一.前言 <码神联盟>是一款为技术人做的开源情怀游戏,每一种编程语言都是一位英雄.客户端和服务端均使用C#开发,客户端使用Unity3D引擎,数据库使用MySQL.这个MOBA类游戏是笔者 ...

  2. hdu_1506:Largest Rectangle in a Histogram 【单调栈】

    题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long lon ...

  3. 如何在Shell中快速切换目录

    1.回到上一次进入的路经cd -2.回到Homecd ~3.自动补齐实例,cd /usr/src/redhat,可以用cd /u[TAB]s[TAB]r[TAB]4.!$ 表示上一个命令的最后一个参数 ...

  4. arcgis for javascript 自定义infowindow

    arcgis 自己的infowindow 太难看了,放在系统中与系统中的风格格格不入,在参考了网上的一些资料后,整理编写了适合自己系统的infowindow,与大家分享. 1.自定义展示效果 2.In ...

  5. 64位系统下8G内存仅使用到4G问题的解决方法

    笔记本:联想E46G 当前bios版本:25CN32WW 内存:DDR3 133 4G × 2 问题:bios信息显示8G,win7和ubuntu 在64位下使用情况仅4G 准备工作1:bios版本和 ...

  6. 走进STM32世界之Hex程序烧写

    多数51单片机(STC系列单片机)的初学者都知道,在51单片机初上电时,可以通过PC机上位机软件将程序引导至bootloader,从而将新程序的hex文件下载至单片机中,完成程序的升级或是更新.在32 ...

  7. for’ loop initial declarations are only allowed in C99 mode

    今天做南邮编程在线的编程题,编程首先计算Fibonacci数列1,1,2,3,5,8,13,21,......的前n项(n不超过40)存入一维整型数组f中,再按%12d的格式输出每项的值,每6项换一行 ...

  8. NYOJ--1276--机器设备(河南省第九届省赛,简单的bfs)

    机器设备 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Alpha 公司设计出一种节能的机器设备.它的内部结构是由 N 个齿轮组成.整个机器设备有 一个驱动齿轮,当 ...

  9. C#语言入门详解(002)

    c# 所編寫的不同應用程序 Console.WriteLine("Hello World!"); ///console textBoxShowHellow.Text = " ...

  10. SqQueue(环状队列(顺序表结构))

    template<typename ElemType> class SqQueue { protected: int count; int front,rear; int maxSize; ...