zoj 3659 Conquer a New Region
- // 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大
// i 到 j的最大权值为 i到j所经历的树边容量的最小值
// 第一感觉是树上的dp
// 后面发现不可以
// 看了题解说是并查集
// 然后发现这不就是在最小生成树那个模板上做其它操作吗、、
// 的确是好题
#include <iostream>- #include <algorithm>
- #include <stdio.h>
- #include <cmath>
- #include <string.h>
- using namespace std;
- #define maxn 200010
- #define LL long long
- struct Eg{
- int a,b,w;
- bool operator<(const Eg &t)const{
- return w>t.w;
- }
- }E[maxn<<];
- int f[maxn];
- LL cnt[maxn],sum[maxn];
- int Find(int x){
- if(x!=f[x])
- f[x]=Find(f[x]);
- return f[x];
- }
- int main(){
- int n;
- int i;
- while(scanf("%d",&n)!=EOF){
- for(i=;i<n-;i++)
- scanf("%d %d %d",&E[i].a,&E[i].b,&E[i].w);
- sort(E,E+n-);
- for(i=;i<=n;i++){
- f[i]=i;
- cnt[i]=;
- sum[i]=;
- }
- LL ans=;
- int u,v;
- LL su,sv;
- for(i=;i<n-;i++){
- u=Find(E[i].a);
- v=Find(E[i].b);
- su=cnt[v]*E[i].w+sum[u];
- sv=cnt[u]*E[i].w+sum[v];
- if(su>sv){
- f[v]=u;
- cnt[u]+=cnt[v];
- sum[u]=su;
- }else {
- f[u]=v;
- cnt[v]+=cnt[u];
- sum[v]=sv;
- }
- ans=max(ans,max(su,sv));
- }
- printf("%lld\n",ans);
- }
- return ;
- }
zoj 3659 Conquer a New Region的更多相关文章
- 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 ...
- 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 roll ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- HDOJ 4424 Conquer a New Region
并检查集合 侧降序,每增加一个侧面应该推断,其中基本建设方..... Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Me ...
- ZOJ3659 Conquer a New Region 并查集
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- zoj 3659
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659 #include<cstdio> #inclu ...
- hdu4424 Conquer a New Region 并查集/类似最小生成树
The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...
- HDU 4424 Conquer a New Region
http://acm.hdu.edu.cn/showproblem.php?pid=4424 [题目大意] 给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...
随机推荐
- CSS属性--过渡(transtion)
首先介绍一下transition的属性取值: transition-property : 主要设置对象中的参与过渡的属性,包括(border-color,background-color,color) ...
- 【WCF--初入江湖】13 实战
13 实战 在线升级 using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- CString 转换成 char *
最近用到CString类,转换成 char * 类型,下面介绍用法: 一.CString 和 LPSTR 转换: CString转换成LPSTR: 方法一:CString server; LPSTR ...
- android C/C++ source files 全局宏定义 .
\system\core\include\arch\linux-arm AndroidConfig.h * ============================================== ...
- CentOS下使用cmake编译安装mysql
一.下载安装所必需的依赖包 1.因为高版本mysql都用cmake安装,所以下载cmake wget http://www.cmake.org/files/v3.0/cmake-3.0.1.tar.g ...
- ubantu安装jdk来配置hadoop
1.将jdk-7u5-linux-x64.tar.gz拷贝到/usr/lib/jdk/目录下面,这里如果没有jdk文件夹,则创建该文件夹,命令: sudo mkdir jdk //创建文件夹jdk s ...
- JsRender系列demo(2)多模板-template
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 使用 C# 对文件进行压缩和解压
C#中对文件压缩和可以使用两个类: GZipStream 类 此实例分为几个模块,分别为: 压缩函数: /// <summary> /// 压缩文件 /// </summary> ...
- docker: "build" requires 1 argument. See 'docker build --help'.
http://bbs.csdn.net/topics/391040030 docker build --tag="ouruser/sinatra:v3" -<Dockerf ...
- Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
在线程中出现这种错误的原因是多次启动start() 解决方法: 将start()改成 run()