zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest
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的更多相关文章
- 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 ...
- 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 ...
- zoj 3659 Conquer a New Region
// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...
- 2016 ACM ICPC Asia Region - Tehran
2016 ACM ICPC Asia Region - Tehran A - Tax 题目描述:算税. solution 模拟. B - Key Maker 题目描述:给出\(n\)个序列,给定一个序 ...
- 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) ...
- 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 ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
随机推荐
- [信息安全] 4.一次性密码 && 身份认证三要素
[信息安全]系列博客:http://www.cnblogs.com/linianhui/category/985957.html 在信息安全领域,一般把Cryptography称为密码,而把Passw ...
- Unity编辑器重写Inspector面板,面板中编辑的数据不触发场景发生变化的问题。
今天开始协助主程一起制作新框架.主程让我写关于新版UI框架注册UI预制体用的快捷编辑器. 现学现写,总算完成了. 可以直接把选中的预制体添加到UIController的数组中,期间涉及到改变大小.所以 ...
- UWP中使用Composition API实现吸顶(1)
前几天需要在UWP中实现吸顶,就在网上找了一些文章: 吸顶大法 -- UWP中的工具栏吸顶的实现方式之一 在UWP中页面滑动导航栏置顶 发现前人的实现方式大多是控制ListViewBase的Heade ...
- TCP/IP协议之IP层
TCP/IP协议的结构参见下图.有应用层,运输层,网络层,链路层. 但是如果更细化的话,其实还有几层没在这上面体现出来. 1 表示层:数据格式化,代码转换,加密. 没有协议 2 会话层:解除或者建立与 ...
- linux c++如何学习
最近有人问我,linux c++工资这么高,怎么学习才能入门,只要有个项目经验能进入一个公司即可. 然后我就说了linux c++开发的整个流程,然后用项目作为目标进行学习,这其实是一种以目的为主导的 ...
- web正则表达式与示例
正则表达式应用——实例应用: 1.验证用户名和密码:("^[a-zA-Z]\w{5,15}$")正确格式:"[A-Z][a-z]_[0-9]"组成,并且第一个字 ...
- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理
在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有 ...
- [补档]暑假集训D8总结
%dalao 今天有两位大佬来讲课,meaty来讲了Catalan(本来说好的莫比乌斯反演呢),聪聪来讲Splay呢 至于听课笔记= =,没来得及记= = 不过好不想上树啊,上了树就下不来了 考试 仍 ...
- python--DenyHttp项目(2)--ACM监考客户端测试版(1阶段客户端总结)
客户端: 1.既然脚本是让别人用的,怎么说也得有个界面,(虽然很low) ''' DenyManager.py 调用客户端与客户端界面 ''' from DenyClient import * fro ...
- 案例:Oracle报错ASM磁盘组不存在或没有mount
案例:Oracle报错ASM磁盘组不存在或没有mount 环境:RHEL 6.5 + Oracle Standby RAC 11.2.0.4 我做Standby RAC实验时,在恢复控制文件时,报错无 ...