codeforces600E. Lomsat gelral(dsu on tree)
dsu on tree
先分轻重儿子
先处理轻边,再处理重儿子
再加上轻儿子的答案
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#pragma GCC optimize(2)
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 1000010
#define For(i,a,b) for(long long i=a;i<=b;++i)
#define p(a) putchar(a)
#define g() getchar() using namespace std;
long long n,col[N],son[N],size[N],cnt[N],Max,Son;
long long sum,ans[N],x,y; struct node{
long long n;
node *next;
}*e[N]; void in(long long &x){
long long y=;char c=g();x=;
while(c<''||c>''){if(c=='-')y=-;c=g();}
while(c<=''&&c>=''){ x=(x<<)+(x<<)+c-'';c=g();}
x*=y;
}
void o(long long x){
if(x<){p('-');x=-x;}
if(x>)o(x/);
p(x%+'');
} void push(long long x,long long y){
node *p;
p=new node();
p->n=y;
if(e[x]==)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} void dfs(long long x,long long fa){//重儿子
size[x]=;
for(node *i=e[x];i;i=i->next){
if(i->n==fa) continue;
dfs(i->n,x);
size[x]+=size[i->n];
if(size[i->n]>size[son[x]])
son[x]=i->n;
}
} void add(long long x,long long fa,long long val){
cnt[col[x]]+=val;
if(cnt[col[x]]>Max){
Max=cnt[col[x]];
sum=col[x];
}
else
if(cnt[col[x]]==Max)
sum+=col[x];
for(node *i=e[x];i;i=i->next){
if(i->n==fa||i->n==Son) continue;
add(i->n,x,val);
}
} void dfs2(long long x,long long fa,long long opt){
for(node *i=e[x];i;i=i->next){
if(i->n==fa) continue;
if(i->n!=son[x]) dfs2(i->n,x,);
}
if(son[x]){
dfs2(son[x],x,);
Son=son[x];
}
add(x,fa,);
Son=;
ans[x]=sum;
if(!opt){
add(x,fa,-);
sum=;
Max=;
}
} int main(){
in(n);
For(i,,n)
in(col[i]);
For(i,,n-){
in(x);in(y);
push(x,y);
push(y,x);
}
dfs(,);
dfs2(,,);
For(i,,n)
o(ans[i]),p(' ');
return ;
}
codeforces600E. Lomsat gelral(dsu on tree)的更多相关文章
- 【CodeForces】600 E. Lomsat gelral (dsu on tree)
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...
- Codeforces.600E.Lomsat gelral(dsu on tree)
题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- cf600E. Lomsat gelral(dsu on tree)
题意 题目链接 给出一个树,求出每个节点的子树中出现次数最多的颜色的编号和 Sol dsu on tree的裸题. 一会儿好好总结总结qwq #include<bits/stdc++.h> ...
- CF 600E. Lomsat gelral(dsu on tree)
解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...
- [Codeforces600E] Lomsat gelral(树上启发式合并)
[Codeforces600E] Lomsat gelral(树上启发式合并) 题面 给出一棵N个点的树,求其所有子树内出现次数最多的颜色编号和.如果多种颜色出现次数相同,那么编号都要算进答案 N≤1 ...
- codeforces600E Lomsat gelral【线段树合并/DSU】
第一次AC这道题,是三年前的一个下午,也许晚上也说不定.当时使用的\(DSU\) \(on\) \(tree\)算法,如今已经淡忘,再学习新的算法过程中,却与旧物重逢.生活中充满不可知会的相遇,即使重 ...
- codeforces600E Lomsat gelral
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Educational Codeforces Round 2 E. Lomsat gelral(dsu)
题目链接 题意:给你一棵以1为根n个点的树,问你以i为根的子树的众数和是多少 思路:dsu是一种优化暴力的手段 首先进行轻重链剖分 然后只记录重链的信息 轻链的信息就直接暴力查找 经过证明这样复杂度可 ...
随机推荐
- cocos2D-X 常用功能封装
Packaging_Kernel.h #pragma once #include <string> #include <map> #include <vector> ...
- STM32硬件错误HardFault_Handler的处理方法
https://blog.csdn.net/electrocrazy/article/details/78173558
- AutoLayout(自动布局)入门
这是WWDC2012笔记系列中的一篇,完整的笔记列表可以参看这里.如果您是首次来到本站,也许您会有兴趣通过RSS的方式订阅本站. AutoLayout在去年的WWDC上被引入Cocoa,而在今年的WW ...
- service资源
service的作用:帮助外界用户访问k8s内的服务,并且提供负载均衡 创建一个service vim k8s_svc.yml apiVersion: v1 kind: Service metadat ...
- 第五篇:zTree节点的一些操作,权当备份
项目场景:将zTree的一个节点挪到某个已知的根节点下,因为树上的节点都是数据库查询出来的,所以不能直接用addNodes()这个方法(否则一刷新又恢复原样了),而是要把这个节点及其某些属性数据保存到 ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
- 【LGP4714】「数学」约数个数和
题目 众所周知,除数个数函数\(\sigma_0=I^2\),\(I\)就是狄利克雷卷积里的\(1\)函数 于是熟悉狄利克雷卷积的话很快就能看出我们要求的就是\(I\times I^{k}\),即\( ...
- 关系型数据库——MySQL
[MySQL架构图] MySQL简要架构图如下图所示,引擎层以插件方式集成了不同的存储引擎,它们共用Server层对外提供服务. 连接器:用于连接管理,进行身份认证及权限相关的管理.(登录MySQ ...
- jQuery.event.move
http://stephband.info/jquery.event.move/ Move events movestart Fired following touchmove or mousemov ...
- 企业网盘居然支持高速局域网文件传输工具(速度可达20M)
高速局域网文件传输工具Mobox,局域网内文件共享是公司内非常必须的功能,原本文件共享可以通过:1)windows目录共享目录来实现文件交互:2)通过U盘拷贝给对方:3)通过QQ发送给对方:4)通过邮 ...