codeforces 600E Lomsat gelral
学习一下$dsu \ on \ tree$。。
这个东西可以处理很多无修改子树问题,复杂度通常为$O(nlogn)$。
主要操作是:我们先把整棵树链剖一下,然后每次先递归轻儿子,再递归重儿子。
对于每棵子树,我们暴力加入整棵子树的贡献。如果是重儿子的子树则另外处理:加入贡献时不考虑加重儿子所在的子树,而在消除贡献时也不消除重儿子的子树,直到它成为某个点的轻儿子的子树的一部分时再消除贡献。
复杂度:因为每个轻儿子最多被加入$O(logn)$次(递归轻儿子时$size$至少$/2$),每条重链最多只会被加入$O(logn)$次,所以复杂度是$O(nlogn)$的。
说得有点玄学,还是看看代码吧。。
//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf (1<<30)
#define N (200010)
#define il inline
#define RG register
#define ll long long using namespace std; struct edge{ int nt,to; }g[N<<]; int head[N],col[N],son[N],sz[N],c[N],n,num,Mx,flag;
ll ans[N],Sum; il int gi(){
RG int x=,q=; RG char ch=getchar();
while ((ch<'' || ch>'') && ch!='-') ch=getchar();
if (ch=='-') q=-,ch=getchar();
while (ch>='' && ch<='') x=x*+ch-,ch=getchar();
return q*x;
} il void insert(RG int from,RG int to){
g[++num]=(edge){head[from],to},head[from]=num; return;
} il void dfs1(RG int x,RG int p){
sz[x]=; RG int v;
for (RG int i=head[x];i;i=g[i].nt){
v=g[i].to; if (v==p) continue;
dfs1(v,x),sz[x]+=sz[v];
if (sz[son[x]]<=sz[v]) son[x]=v;
}
return;
} il void add(RG int x,RG int p,RG int val){
col[c[x]]+=val; RG int v;
if (Mx<col[c[x]]) Mx=col[c[x]],Sum=c[x];
else if (Mx==col[c[x]]) Sum+=c[x];
for (RG int i=head[x];i;i=g[i].nt){
v=g[i].to; if (v==p || v==flag) continue;
add(v,x,val);
}
return;
} il void dfs2(RG int x,RG int p,RG int fg){
RG int v;
for (RG int i=head[x];i;i=g[i].nt){
v=g[i].to; if (v!=p && v!=son[x]) dfs2(v,x,);
}
if (son[x]) dfs2(son[x],x,),flag=son[x];
add(x,p,),flag=,ans[x]=Sum;
if (fg) add(x,p,-),Mx=Sum=; return;
} int main(){
#ifndef ONLINE_JUDGE
freopen("600E.in","r",stdin);
freopen("600E.out","w",stdout);
#endif
n=gi();
for (RG int i=;i<=n;++i) c[i]=gi();
for (RG int i=,u,v;i<n;++i)
u=gi(),v=gi(),insert(u,v),insert(v,u);
dfs1(,),dfs2(,,);
for (RG int i=;i<=n;++i) printf("%I64d ",ans[i]);
return ;
}
codeforces 600E Lomsat gelral的更多相关文章
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- Codeforces 600E Lomsat gelral (树上启发式合并)
题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- Codeforces 600E Lomsat gelral(dsu on tree)
dsu on tree板子题.这个trick保证均摊O(nlogn)的复杂度,要求资瓷O(1)将一个元素插入集合,清空集合时每个元素O(1)删除.(当然log的话就变成log^2了) 具体的,每次先遍 ...
- codeforces 600E. Lomsat gelral 启发式合并
题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...
- codeforces 600E . Lomsat gelral (线段树合并)
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...
- Codeforces.600E.Lomsat gelral(dsu on tree)
题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...
- 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 ...
- 【Codeforces】600E. Lomsat gelral
Codeforces 600E. Lomsat gelral 学习了一下dsu on tree 所以为啥是dsu而不是dfs on tree??? 这道题先把这棵树轻重链剖分了,然后先处理轻儿子,处理 ...
随机推荐
- c++从txt中读取数据,数据并不是一行路径(实用)
#include <iostream>#include <fstream>#include <string> using namespace std; //输出空行 ...
- linux 文件 s 权限
s权限的作用:表示对文件具用可执行权限的用户将使用文件拥有者的权限或文件拥有者所在组的权限在对文件进行执行. s权限的设置:4,用户拥有者的执行权限位, 6,用户组的执行权限位, 2, 两者都设置, ...
- Kafka monitoring监控
一.Metrics kafka有两个metrics包,在看源码的时候很容易混淆 package kafka.metrics package org.apache.kafka.common.metric ...
- powerdesigner 遇到的各种问题总结
1. 设置自增 打开表 -- 在具体列的前面双击即可添加各种属性 2. 生成sql 时设置编码 database --> generate database --> format --&g ...
- 3d Max 2013安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- maya2014安装失败如何卸载重装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- is 和 == 区别
== 和 is 的区别 == 比较 比较的是两个值 适用于 列表a = '[1:2]'b = '[1:2]'print(a == b) #True 字典a = '{1,2,3}'b = '{ ...
- python3+Appium自动化12-H5元素定位环境搭建
前言 在混合开发的App中,经常会有内嵌的H5页面.那么这些H5页面元素该如何进行定位操作呢? 针对这种场景直接使用前面所讲的方法来进行定位是行不通的,因为前面的都是基于Andriod原生控件进行元素 ...
- java实现连接mysql数据库单元测试查询数据项目分享
1.按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model .db.dao.test;具体的架构详见下图: 2.根据搭建的项目架构新建数据库test和数据库表t_userinfo并且 ...
- Hadoop学习笔记(3) Hadoop文件系统二
1 查询文件系统 (1) 文件元数据:FileStatus,该类封装了文件系统中文件和目录的元数据,包括文件长度.块大小.备份.修改时间.所有者以及版权信息.FileSystem的getFileSta ...