• 黄学长报天气


    题目描述

    明天就是情人节了,黄学长正盘算着把哪个妹子。不幸的是,明天全市暴雨。但他发动了一场超级天气预报,这场预报超级厉害,能够知道每条道路的天气情况。
    现在已知福州城中有n座黄学长的房产,其中用n−1条道路连接,每条道路有个天气良好值v。如果v越低,则天气越差。现在定义两点间路径的天气情况值d(x,y)为x,y两点间路径上最小的v值;再定义某个点到其他所有点的d值的总和为房子的价值,即∑1≤i≤nd(x,i)。如果某个点的d值总和越大,说明他所处的房子位置越好。
    黄学长拥有超能力,明天可以睡醒在任意一个房子里,因为他有可能去把任意一个妹子,所以他希望醒来的房子的价值(该座房子的d值总和)最大。他当然是不屑做这种题的,于是这个任务就要交给你了!

    输入

    第一行输入一个整数n,表示该组数据中房产的个数。
    接下来n−1行,每行三个整数x,y,v,表示编号为x的房子和编号为y的房子之间存在一条天气良好值为v的道路,每座房产的编号从1−n。

    输出

    输出一行,为最大权值d。

    样例输入

    4
    1 2 2
    2 4 1
    2 3 1
    

    样例输出

    4

    提示

    对于60%的数据,有1≤n≤103
    对于100%的数据,有1≤n≤5×105,1≤v≤103,1≤x,y≤n。

    题解:

    按边权从大到小,枚举边权的贡献,对于每个块更新一下ans数组的值即可;ans[i]表示在i所在的块中某一结点到其他结点d总和的最大值。

    AC代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int maxn=5e5+50;
     5 struct node
     6 {
     7     int x,y,w;
     8 }stu[maxn];
     9 bool cmp(node p,node q)
    10 {
    11     return p.w>q.w;
    12 }
    13 int n,pre[maxn],sz[maxn],ans[maxn];
    14 int fin(int x)
    15 {
    16     int boss=x;
    17     while(boss!=pre[boss]) boss=pre[boss];
    18     while(x!=pre[x]){
    19         int cnt=pre[x];
    20         pre[x]=boss;
    21         x=cnt;
    22     }
    23 }
    24 int main()
    25 {
    26     scanf("%d",&n);
    27     for(int i=1;i<n;i++){
    28         scanf("%d %d %d",&stu[i].x,&stu[i].y,&stu[i].w);
    29     }
    30     sort(stu+1,stu+n+1,cmp);
    31     for(int i=1;i<=n;i++){
    32         sz[i]=1;pre[i]=i;
    33     }
    34     for(int i=1;i<n;i++){
    35         int x=fin(stu[i].x);
    36         int y=fin(stu[i].y);
    37         int val=max(ans[x]+sz[y]*stu[i].w,ans[y]+sz[x]*stu[i].w);
    38         pre[x]=y;
    39         sz[y]+=sz[x];
    40         ans[y]=val;
    41     }
    42     printf("%d
    ",ans[fin(n)]);
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    三台机子配置免密码登录
    cookie,session,token之间的联系与区别
    服务端技术栈
    ConcurrentHashMap
    Integer比较
    meidi
    MySQL字符串中数字排序的问题
    表格td、th强制换行
    mysql 清空表 Truncate及delete区别
    html锚点
  • 原文地址:https://www.cnblogs.com/lglh/p/11668910.html
Copyright © 2020-2023  润新知