题面:https://www.lydsy.com/JudgeOnline/problem.php?id=5457

题解:

线段树合并,对于每个节点维护sum(以该节点为根的子树中最大的种类和)和kind(以该节点为根的子树中种类和最大的种类)即可。

代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=(4e5)+;
int N,M,U,V,num_edge=,edge_head[maxn],root[maxn];
int num_treenode=;
struct Edge{int to,nx;}edge[maxn<<];
inline void Add_edge(int from,int to){
edge[++num_edge].nx=edge_head[from];
edge[num_edge].to=to;
edge_head[from]=num_edge;
return;
}
struct Tree{int lc,rc,l,r,sum,kd;}tr[maxn*];
inline void Pushup(int x){
int lc=tr[x].lc,rc=tr[x].rc;
if(tr[lc].sum>=tr[rc].sum){
tr[x].sum=tr[lc].sum;
tr[x].kd=tr[lc].kd;
}
else {
tr[x].sum=tr[rc].sum;
tr[x].kd=tr[rc].kd;
}
return;
}
inline void Build(int x,int l,int r,int q,int s){
tr[x].l=l;tr[x].r=r;int mid=(l+r)>>;
if(l==r&&l==q){
tr[x].kd=q;
tr[x].sum=s;
return;
}
if(q<=mid)Build(tr[x].lc=++num_treenode,l,mid,q,s);
else Build(tr[x].rc=++num_treenode,mid+,r,q,s);
Pushup(x);
return;
}
struct A_{int kd,sum;}A[maxn];
inline int Merge(int u,int v){
if(!u)return v;
if(!v)return u;
int l=tr[u].l,r=tr[u].r;
if(l==r){
tr[u].sum+=tr[v].sum;
return u;
}
tr[u].lc=Merge(tr[u].lc,tr[v].lc);
tr[u].rc=Merge(tr[u].rc,tr[v].rc);
Pushup(u);
return u;
}
inline void Dfs(int x,int fa){
for(int i=edge_head[x];i;i=edge[i].nx){
int y=edge[i].to;
if(y!=fa){
Dfs(y,x);
Merge(root[x],root[y]);
}
}
return;
}
int main(){
scanf("%d%d",&N,&M);
for(int i=;i<N;i++){
scanf("%d%d",&U,&V);
Add_edge(U,V);Add_edge(V,U);
}
for(int i=;i<=N;i++){
scanf("%d%d",&A[i].kd,&A[i].sum);
Build(root[i]=++num_treenode,,M,A[i].kd,A[i].sum);
}
Dfs(,);
for(int i=;i<=N;i++)printf("%d %d\n",tr[root[i]].kd,tr[root[i]].sum);
return ;
}

By:AlenaNuna

线段树合并 || BZOJ 5457: 城市的更多相关文章

  1. bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并)

    bzoj5417/luoguP4770 [NOI2018]你的名字(后缀自动机+线段树合并) bzoj Luogu 给出一个字符串 $ S $ 及 $ q $ 次询问,每次询问一个字符串 $ T $ ...

  2. BZOJ:5457: 城市(线段树合并)(尚待优化)

    5457: 城市 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 18  Solved: 12[Submit][Status][Discuss] Des ...

  3. BZOJ #5457: 城市 [线段树合并]

    线段树合并的板子题,每次从下到上合并就完事了 // by Isaunoya #include <bits/stdc++.h> using namespace std; #define re ...

  4. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  5. BZOJ.4399.魔法少女LJJ(线段树合并)

    BZOJ 注意\(c\leq7\)→_→ 然后就是裸的权值线段树+线段树合并了. 对于取\(\max/\min\)操作可以直接区间修改清空超出范围的值,然后更新到对应位置上就行了(比如对\(v\)取\ ...

  6. BZOJ.5461.[PKUWC2018]Minimax(DP 线段树合并)

    BZOJ LOJ 令\(f[i][j]\)表示以\(i\)为根的子树,权值\(j\)作为根节点的概率. 设\(i\)的两棵子树分别为\(x,y\),记\(p_a\)表示\(f[x][a]\),\(p_ ...

  7. BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)

    BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...

  8. BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)

    BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...

  9. BZOJ.5417.[NOI2018]你的名字(后缀自动机 线段树合并)

    LOJ 洛谷 BZOJ 考虑\(l=1,r=|S|\)的情况: 对\(S\)串建SAM,\(T\)在上面匹配,可以得到每个位置\(i\)的后缀的最长匹配长度\(mx[i]\). 因为要去重,对\(T\ ...

随机推荐

  1. Quartz小记(一):Elastic-Job - 分布式定时任务框架

    Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范部分.该项目基于成熟的开源产品Quartz和Zooke ...

  2. html学习笔记之2——多媒体

    一:插件 插件可以通过 <object> 标签或者 <embed> 标签添加在页面中. <object width="400" height=&quo ...

  3. Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test java.lang.IllegalStateException

    不能找到对应的带有@SpringBootConfiguration 的类,你需要将它放在包的最顶层.

  4. assert BOOST_ASSERT的坑

    下面这行代码 BOOST_ASSERT(SUCCEEDED(m_pd3dDevice->CreateBuffer(&frame_ptr->m_const_buffers[i].m_ ...

  5. [转]Docker中的镜像

    引言 这篇文章中我们主要来探讨下Docker镜像,它是用来启动容器的构建基石,本文的所用到的Dcoker版本是17.1,API版本是1.33,Go的版本是1.9.2,OS是基于Arch Linux的M ...

  6. zsh+on-my-zsh配置教程指南(程序员必备)

    本文以CentOS 7/Mac 为例,介绍zsh的配置使用教程. 准备 查看当前环境shell echo $SHELL <!-- more --> 查看系统自带哪些shell cat /e ...

  7. 小米Pro 安装苹果系统

    参考 http://www.miui.com/thread-11363672-1-1.html http://www.miui.com/thread-7601066-1-1.html https:// ...

  8. Windows Server 2008 IIS安装FTP及端口配置

    添加角色IIS,选择上FTP服务 打开IIS,右击网站,添加FTP站点 允许访问的指定用户,必须是Windows系统真实存在的用户,为了安全起见,此用户只赋予user组即可,不能赋予远程桌面权限 如果 ...

  9. Fluent动网格【13】:网格光顺总结及实例

    光顺(Smoothing)方法是最基本的网格节点更新方法.Fluent提供了三种光顺方法: Spring弹簧光顺 Diffusion扩散光顺 Linearly Elastic Solid光顺 三种方法 ...

  10. Unable to find IPv4-only network bridge for LXD.

    https://github.com/conjure-up/conjure-up/issues/1440It seems like the the installation is complainin ...