解题思路

  \(dsu\) \(on\) \(tree\)的模板题。暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\)

代码


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set> using namespace std;
const int MAXN = 100005;
typedef long long LL; inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f?x:-x;
} int n,c[MAXN],head[MAXN],cnt,to[MAXN<<1],nxt[MAXN<<1],num[MAXN],sum[MAXN];
int siz[MAXN],son[MAXN],fa[MAXN],Num;
LL ans[MAXN],Max[MAXN],Ans; inline void add(int bg,int ed){
to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
} inline void update(int x){
sum[num[c[x]]]--;Max[num[c[x]]]-=c[x];
num[c[x]]++;sum[num[c[x]]]++;Max[num[c[x]]]+=c[x];
if(Num<=num[c[x]]) Num=num[c[x]];Ans=Max[Num];
} inline void Clear(int x){
sum[num[c[x]]]--;Max[num[c[x]]]-=c[x];
num[c[x]]--;sum[num[c[x]]]++;Max[num[c[x]]]+=c[x];
if(Num==num[c[x]]+1 && !sum[num[c[x]]+1]) Num=num[c[x]];
Ans=Max[Num];
} void bfs(int x){
update(x);
for(int i=head[x];i;i=nxt[i])
if(to[i]!=fa[x]) bfs(to[i]);
} void clear(int x){
Clear(x);
for(int i=head[x];i;i=nxt[i])
if(to[i]!=fa[x]) clear(to[i]);
} void dfs1(int x,int f){
fa[x]=f;siz[x]=1;int maxson=-1,u;
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(u==f) continue;
dfs1(u,x);siz[x]+=siz[u];
if(siz[u]>maxson) maxson=siz[u],son[x]=u;
}
} void dfs2(int x){
for(int i=head[x];i;i=nxt[i]){
int u=to[i];if(u==fa[x] || u==son[x]) continue;
dfs2(u);clear(u);
}
if(son[x]) dfs2(son[x]);
for(int i=head[x];i;i=nxt[i]){
int u=to[i];if(u==fa[x] || u==son[x]) continue;
bfs(u);
}update(x);
ans[x]=Ans;
} int main(){
n=rd();int x,y;
for(int i=1;i<=n;i++) c[i]=rd();
for(int i=1;i<n;i++){
x=rd(),y=rd();
add(x,y);add(y,x);
}dfs1(1,0);dfs2(1);
for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
return 0;
}

CF 600E. Lomsat gelral(dsu on tree)的更多相关文章

  1. Codeforces.600E.Lomsat gelral(dsu on tree)

    题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...

  2. Codeforces 600E. Lomsat gelral(Dsu on tree学习)

    题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...

  3. 【CodeForces】600 E. Lomsat gelral (dsu on tree)

    [题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...

  4. cf600E. Lomsat gelral(dsu on tree)

    题意 题目链接 给出一个树,求出每个节点的子树中出现次数最多的颜色的编号和 Sol dsu on tree的裸题. 一会儿好好总结总结qwq #include<bits/stdc++.h> ...

  5. codeforces600E. Lomsat gelral(dsu on tree)

    dsu on tree先分轻重儿子先处理轻边,再处理重儿子再加上轻儿子的答案 #include<iostream> #include<cstdio> #include<q ...

  6. 【Codeforces】600E. Lomsat gelral

    Codeforces 600E. Lomsat gelral 学习了一下dsu on tree 所以为啥是dsu而不是dfs on tree??? 这道题先把这棵树轻重链剖分了,然后先处理轻儿子,处理 ...

  7. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

  8. Codeforces 600E Lomsat gelral(dsu on tree)

    dsu on tree板子题.这个trick保证均摊O(nlogn)的复杂度,要求资瓷O(1)将一个元素插入集合,清空集合时每个元素O(1)删除.(当然log的话就变成log^2了) 具体的,每次先遍 ...

  9. Codeforces 600E - Lomsat gelral 「$Dsu \ on \ tree$模板」

    With $Dsu \ on \ tree$ we can answer queries of this type: How many vertices in the subtree of verte ...

随机推荐

  1. python--MySql(外键约束、多表查询(*****))

    两张表之间的关系: 一对一(两张表可以合并成一张表) 一对一用的比较少,多对一对外键设置unique约束可以实现一对一 一对多(例如:每一个班主任会对应多个学生 , 而每个学生只能对应一个班主任) 多 ...

  2. Cisco基础(二):三层交换vlan间通信、多交换机vlan间通信、三层交换配置路由、RIP动态路由配置、三层交换配置RIP动态路由

    一.三层交换vlan间通信 目标: VLAN实现了广播域的隔离,同时也将VLAN间的通信隔离了.三层交换技术使得VLAN间可以通信. 通过三层交换实现VLAN间通信 方案: 为了解决了传统路由器低速. ...

  3. Hive 窗口函数

    举例: row_number() over(partition by clue_id order by state_updated desc) 业务举例: select distinct a.clue ...

  4. HTML-参考手册: HTML 音频/视频

    ylbtech-HTML-参考手册: HTML 音频/视频 1.返回顶部 1. HTML 音频/视频 DOM 参考手册 HTML5 DOM 为 <audio> 和 <video> ...

  5. JS-动态加载

    var s = document.createElement('script'); s.setAttribute('src', ''); s.setAttribute('type', 'text/ja ...

  6. 52、saleforce 导入csv文件

    Load Data Using the Custom Object Import Wizard 1. 2. 3. 4. 5. 6.然后就导入成功了

  7. grub命令详解

    在grub下引导linux:其步骤如下;a 进入grub的命令模式.b 先熟悉一下grub 的一些命令 grub>help c 熟悉一下cat命令d root指令来指定/boot所在的分区e k ...

  8. Linux中TLS

    TLS(Thread Local Storage) 线程局部存储. 在Linux操作系统中,TLS保存成GDT中描述的一个段. 1: /* 2: * This creates a new proces ...

  9. css篇-页面布局-三栏布局

    页面布局 题目:假设高度已知,请写出三栏布局,其中左栏.右栏宽度各为300px,中间自适应. 1)浮动 2)绝对定位 3)Flexbox 4)表格布局 5)网格布局(CSS3的Grid布局) 代码: ...

  10. 开发效率优化之Git分布式版本控制系统(一)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680本篇文章将先从Git分布式版本控制系统来阐述开发效率优化 一,企业 ...