1. // 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大
    // i 到 j的最大权值为 i到j所经历的树边容量的最小值
    // 第一感觉是树上的dp
    // 后面发现不可以
    // 看了题解说是并查集
    // 然后发现这不就是在最小生成树那个模板上做其它操作吗、、
    // 的确是好题
    #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <cmath>
  5. #include <string.h>
  6. using namespace std;
  7. #define maxn 200010
  8. #define LL long long
  9. struct Eg{
  10. int a,b,w;
  11. bool operator<(const Eg &t)const{
  12. return w>t.w;
  13. }
  14. }E[maxn<<];
  15. int f[maxn];
  16. LL cnt[maxn],sum[maxn];
  17. int Find(int x){
  18. if(x!=f[x])
  19. f[x]=Find(f[x]);
  20. return f[x];
  21. }
  22. int main(){
  23. int n;
  24. int i;
  25. while(scanf("%d",&n)!=EOF){
  26. for(i=;i<n-;i++)
  27. scanf("%d %d %d",&E[i].a,&E[i].b,&E[i].w);
  28. sort(E,E+n-);
  29. for(i=;i<=n;i++){
  30. f[i]=i;
  31. cnt[i]=;
  32. sum[i]=;
  33. }
  34. LL ans=;
  35. int u,v;
  36. LL su,sv;
  37. for(i=;i<n-;i++){
  38. u=Find(E[i].a);
  39. v=Find(E[i].b);
  40. su=cnt[v]*E[i].w+sum[u];
  41. sv=cnt[u]*E[i].w+sum[v];
  42. if(su>sv){
  43. f[v]=u;
  44. cnt[u]+=cnt[v];
  45. sum[u]=su;
  46. }else {
  47. f[u]=v;
  48. cnt[v]+=cnt[u];
  49. sum[v]=sv;
  50. }
  51. ans=max(ans,max(su,sv));
  52. }
  53. printf("%lld\n",ans);
  54. }
  55. return ;
  56. }

zoj 3659 Conquer a New Region的更多相关文章

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

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

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

  4. HDOJ 4424 Conquer a New Region

    并检查集合 侧降序,每增加一个侧面应该推断,其中基本建设方..... Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Me ...

  5. ZOJ3659 Conquer a New Region 并查集

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  6. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  7. zoj 3659

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659 #include<cstdio> #inclu ...

  8. hdu4424 Conquer a New Region 并查集/类似最小生成树

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

  9. HDU 4424 Conquer a New Region

    http://acm.hdu.edu.cn/showproblem.php?pid=4424 [题目大意] 给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的.每条边有个权值,从一点到另 ...

随机推荐

  1. CSS属性--过渡(transtion)

    首先介绍一下transition的属性取值: transition-property : 主要设置对象中的参与过渡的属性,包括(border-color,background-color,color) ...

  2. 【WCF--初入江湖】13 实战

    13 实战 在线升级 using System; using System.Collections.Generic; using System.ComponentModel; using System ...

  3. CString 转换成 char *

    最近用到CString类,转换成 char * 类型,下面介绍用法: 一.CString 和 LPSTR 转换: CString转换成LPSTR: 方法一:CString server; LPSTR ...

  4. android C/C++ source files 全局宏定义 .

    \system\core\include\arch\linux-arm AndroidConfig.h * ============================================== ...

  5. CentOS下使用cmake编译安装mysql

    一.下载安装所必需的依赖包 1.因为高版本mysql都用cmake安装,所以下载cmake wget http://www.cmake.org/files/v3.0/cmake-3.0.1.tar.g ...

  6. ubantu安装jdk来配置hadoop

    1.将jdk-7u5-linux-x64.tar.gz拷贝到/usr/lib/jdk/目录下面,这里如果没有jdk文件夹,则创建该文件夹,命令: sudo mkdir jdk //创建文件夹jdk s ...

  7. JsRender系列demo(2)多模板-template

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. 使用 C# 对文件进行压缩和解压

    C#中对文件压缩和可以使用两个类: GZipStream 类 此实例分为几个模块,分别为: 压缩函数: /// <summary> /// 压缩文件 /// </summary> ...

  9. docker: "build" requires 1 argument. See 'docker build --help'.

    http://bbs.csdn.net/topics/391040030 docker build  --tag="ouruser/sinatra:v3" -<Dockerf ...

  10. Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

    在线程中出现这种错误的原因是多次启动start() 解决方法: 将start()改成 run()