解题思路

  \(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. CSP2019赛前小复习:

    虽然觉得复习也没有什么用,还不吃好睡好,保持好心情. SA: 坑就那几个. \(s[0]=s[n+1]=-1\). 和\(rank\)交换的\(tp\)数组的\(tp[n+1]=0\). 一般加上这两 ...

  2. UVa 11384 (推公式+递归)

    题目: 给你1到n,现在让你将每个数变成0,每一步操作可以选取任意数一起减去一个整数,减完了不能为负数!问你最少需要几步? 巨水的题,然而为什么要写博客呢?提醒自己要记得递归函数,不要傻傻的开数组硬比 ...

  3. eureka学习(二)

    eureka服务端创建好后,现在我们让eureka客户端(也就是服务提供者)注册到eureka上去. 首先加入依赖包: <!--将微服务provider注册到eureka--> <d ...

  4. 回调函数 和 promise对象,及封装API接口

    1.回调函数:https://blog.csdn.net/baidu_32262373/article/details/54969696 注意:回调函数不一定需要用到 return.如果浏览器支持Pr ...

  5. 33. 构建第一个job

    1.点击 New Item 2.Enter an item name 输入一个name,点击Freestyle project 3.我们可以输入一个描述,点击Advanced 4.勾选Use cust ...

  6. 前端学习笔记——HTML

    1.前端的三把利器 HTML:我们把它比喻成赤裸的一个人 CSS:华丽的衣服 JS/JavaScript:赋予这个人的行为,也就是动起来 2.什么是HTML? HTML 是用来描述网页的一种语言.ht ...

  7. python作业/练习/实战:生成双色球小程序

    作业要求: 每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中选择:蓝色球号码从1--16中选择 代码范例 import random all_red_ball = [str( ...

  8. JavaScript之 ------ 函数(一般函数、动态函数、匿名函数)

    JavaScript之 ------ 函数(一般函数.动态函数.匿名函数) 函数 一.一般函数 1.格式: function 函数名(形式参数...) { 执行语句: return 返回值: } 函数 ...

  9. jQuery选择器我犯的错误(原创)

    jQuery的选择器十分强大,但是在使用jQuery选择器的时候一定要十分小心,空格.冒号.引号到处都是坑,老手也不能避免,只能勤加练习,熟能生巧,掌握规律,为了练习,凡是到选择器的地方我都自己先敲, ...

  10. bootstrapTble 的一些小结

    前言: 1.bootstrapTable 官网  http://bootstrap-table.wenzhixin.net.cn/zh-cn/ ,http://bootstrap-table.wenz ...