zoukankan      html  css  js  c++  java
  • CF600E:Lomsat gelral(线段树合并)

    Description

    一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和。

    Input

    第一行一个$n$。第二行$n$个数字是$c[i]$。后面$n-1$行给出树边。

    Output

    一行答案。

    Sample Input1

    4
    1 2 3 4
    1 2
    2 3
    2 4

    Sample Output1

    10 9 3 4

    Sample Input2

    15
    1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
    1 2
    1 3
    1 4
    1 14
    1 15
    2 5
    2 6
    2 7
    3 8
    3 9
    3 10
    4 11
    4 12
    4 13

    Sample Output2

    6 5 4 3 2 3 3 1 1 3 2 2 1 2 3

    Solution

    线段树合并模板题,但我竟然读错题了QAQ……
    情况特殊所以$Merge$的时候要加上$l$,$r$。
    没啥好说的看代码就能懂……

    Code

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #define N (100009)
     5 #define LL long long
     6 using namespace std;
     7 
     8 struct Sgt{int ls,rs,max; LL val;}Segt[N*50];
     9 struct Edge{int to,next;}edge[N<<1];
    10 int n,x,u,v,sgt_num,c[N],Root[N];
    11 int head[N],num_edge;
    12 
    13 void add(int u,int v)
    14 {
    15     edge[++num_edge].to=v;
    16     edge[num_edge].next=head[u];
    17     head[u]=num_edge;
    18 }
    19 
    20 void Pushup(int now)
    21 {
    22     int ls=Segt[now].ls, rs=Segt[now].rs;
    23     if (Segt[ls].max==Segt[rs].max)
    24     {
    25         Segt[now].max=Segt[ls].max;
    26         Segt[now].val=Segt[ls].val+Segt[rs].val;
    27     }
    28     else if (Segt[ls].max>Segt[rs].max)
    29     {
    30         Segt[now].max=Segt[ls].max;
    31         Segt[now].val=Segt[ls].val;
    32     }
    33     else
    34     {
    35         Segt[now].max=Segt[rs].max;
    36         Segt[now].val=Segt[rs].val;
    37     }
    38 }
    39 
    40 void Update(int &now,int l,int r,int v)
    41 {
    42     if (!now) now=++sgt_num;
    43     if (l==r)
    44     {
    45         Segt[now].max=1;
    46         Segt[now].val=v;
    47         return;
    48     }
    49     int mid=(l+r)>>1;
    50     if (v<=mid) Update(Segt[now].ls,l,mid,v);
    51     else Update(Segt[now].rs,mid+1,r,v);
    52     Pushup(now);
    53 }
    54 
    55 int Merge(int x,int y,int l,int r)
    56 {
    57     if (!x || !y) {return x|y;}
    58     int tmp=++sgt_num;
    59     if (l==r)
    60     {
    61         Segt[tmp].max=Segt[x].max+Segt[y].max;
    62         Segt[tmp].val=l; return tmp;
    63     }
    64     int mid=(l+r)>>1;
    65     Segt[tmp].ls=Merge(Segt[x].ls,Segt[y].ls,l,mid);
    66     Segt[tmp].rs=Merge(Segt[x].rs,Segt[y].rs,mid+1,r);
    67     Pushup(tmp); return tmp;
    68 }
    69 
    70 void DFS(int x,int fa)
    71 {
    72     for (int i=head[x]; i; i=edge[i].next)
    73         if (edge[i].to!=fa)
    74         {
    75             DFS(edge[i].to,x);
    76             Root[x]=Merge(Root[x],Root[edge[i].to],1,n);
    77         }
    78 }
    79 
    80 int main()
    81 {
    82     scanf("%d",&n);
    83     for (int i=1; i<=n; ++i)
    84     {
    85         scanf("%d",&c[i]);
    86         Update(Root[i],1,n,c[i]);
    87     }
    88     for (int i=1; i<=n-1; ++i)
    89     {
    90         scanf("%d%d",&u,&v);
    91         add(u,v); add(v,u);
    92     }
    93     DFS(1,0);
    94     for (int i=1; i<=n; ++i)
    95         printf("%lld ",Segt[Root[i]].val);
    96 }
  • 相关阅读:
    Hadoop week review
    [Q&A] VS 连接 SQLServer 时未找到或无法访问服务器
    [System] CentOS虚拟机系统克隆后的网络配置
    [Tool] csdn客户端开发(非官方版)
    [MySQL] Win7 下修改 MySQL 5.5 默认编码格式
    [Q&A] MySQL Error 1050(42S01): Table already exist
    [Matlab] libsvmmat 安装
    [SL] Silverlight + WCF Demo项目
    [Q&A] 在证书存储区中找不到清单签名证书
    谷歌浏览器当手机浏览器
  • 原文地址:https://www.cnblogs.com/refun/p/10057599.html
Copyright © 2011-2022 走看看