Codeforces 600E Lomsat gelral(dsu on tree)
dsu on tree板子题。这个trick保证均摊O(nlogn)的复杂度,要求资瓷O(1)将一个元素插入集合,清空集合时每个元素O(1)删除。(当然log的话就变成log^2了)
具体的,每次先遍历轻儿子的子树,暴力求得所需信息,每遍历完一棵轻子树都将其信息清空。然后遍历重子树,暴力求得所需信息,保留信息,再重新遍历轻子树将信息合并,最后加上根本身得到原子树的信息。
复杂度证明考虑每个点的信息被统计的次数,显然这只与其到根的路径上轻边条数有关,于是复杂度O(nlogn)。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,a[N],p[N],fa[N],size[N],son[N],cnt[N],mx,t;
ll ans[N],cur;
struct data{int to,nxt;
}edge[N<<];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void make(int k)
{
size[k]=;
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=fa[k])
{
fa[edge[i].to]=k;
make(edge[i].to);
size[k]+=size[edge[i].to];
if (size[edge[i].to]>size[son[k]]) son[k]=edge[i].to;
}
}
void update(int x,int op)
{
cnt[x]+=op;
if (op==)
{
if (cnt[x]>mx) mx=cnt[x],cur=;
if (cnt[x]>=mx) cur+=x;
}
else mx=cur=;
}
void add(int k,int op)
{
update(a[k],op);
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=fa[k]) add(edge[i].to,op);
}
void dfs(int k)
{
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=fa[k]&&edge[i].to!=son[k]) dfs(edge[i].to),add(edge[i].to,-);
if (son[k]) dfs(son[k]);
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=fa[k]&&edge[i].to!=son[k]) add(edge[i].to,);
update(a[k],);ans[k]=cur;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("600E.in","r",stdin);
freopen("600E.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<n;i++)
{
int x=read(),y=read();
addedge(x,y),addedge(y,x);
}
make();
dfs();
for (int i=;i<=n;i++) printf("%I64d ",ans[i]);
return ;
}
Codeforces 600E Lomsat gelral(dsu on tree)的更多相关文章
- 【CF600E】Lomsat gelral(dsu on tree)
[CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- 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 ...
- CF600E Lomsat gelral(dsu on tree)
dsu on tree跟冰茶祭有什么关系啊喂 dsu on tree的模板题 思想与解题过程 类似树链剖分的思路 先统计轻儿子的贡献,再统计重儿子的贡献,得出当前节点的答案后再减去轻儿子对答案的贡献 ...
- Codeforces 600E Lomsat gelral (树上启发式合并)
题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...
- 【cf600】E. Lomsat gelral(dsu on tree)
传送门 题意: 求子树众数. 思路: \(dsu\ on\ tree\)模板题,用一个桶记录即可. 感觉\(dsu\ on\ tree\)这个算法的涉及真是巧妙呀,保留重链的信息,不断暴力轻边,并且不 ...
- [Codeforces600E] Lomsat gelral(树上启发式合并)
[Codeforces600E] Lomsat gelral(树上启发式合并) 题面 给出一棵N个点的树,求其所有子树内出现次数最多的颜色编号和.如果多种颜色出现次数相同,那么编号都要算进答案 N≤1 ...
- 【Luogu U41492】树上数颜色——树上启发式合并(dsu on tree)
(这题在洛谷主站居然搜不到--还是在百度上偶然看到的) 题目描述 给一棵根为1的树,每次询问子树颜色种类数 输入输出格式 输入格式: 第一行一个整数n,表示树的结点数 接下来n-1行,每行一条边 接下 ...
- Codeforces.600E.Lomsat gelral(dsu on tree)
题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...
随机推荐
- [笔记] ubuntu下添加windows的字体
方法如下: 第一步:将windows下喜欢的字体文件copy到一个文件夹中,例如将XP里WINDOWS/FONTS中的字体文件(本人比较贪心,把整个文件夹copy了过来……),在linux中命名为xp ...
- 【MongoDB】MongoDB的下载 安装 配置及使用
windows系统 教程 1.下载地址 (官方提供根据系统位数选择对应的bit.exe下载) 由于自己win32系统不支持该官方版本,在网上又找了个 mongodb-win32-i386版本 p ...
- c# Login UI with background picture animation
准备4张图片 UI control: <Grid x:Class="Test1.MainBgAd" xmlns="http://schemas.microsoft. ...
- ElasticSearch入门 第九篇:实现正则表达式查询的思路
这是ElasticSearch 2.4 版本系列的第九篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- 分布式事务的CAP理论 与BASE理论
CAP理论 一个经典的分布式系统理论.CAP理论告诉我们:一个分布式系统不可能同时满足一致性(C:Consistency).可用性(A:Availability)和分区容错性(P:Partition ...
- git笔记:通过给grunt-inline打tag看tag操作
晚上review了下grunt-inline的issues,看到有个兄弟pull request,修正了0.3.0版本的一个bug.于是就merge了下,然后发布了0.3.1版本(这里). npm p ...
- Ubuntu侧边任务栏自动隐藏
设置>>Dock>>{自动隐藏Dock}选项打开
- vue项目eslint配置 以及 解释
// https://eslint.org/docs/user-guide/configuring module.exports = { root: true, parserOptions: { pa ...
- 290. Word Pattern【LeetCode by java】
今天发现LintCode页面刷新不出来了,所以就转战LeetCode.还是像以前一样,做题顺序:难度从低到高,每天至少一题. Given a pattern and a string str, fin ...
- From today 2019.02.27
HIT开设软件构造课程,需要在博客上分享记录学习体验,感觉还是挺好的. 以后会不定期更新一些关于学下java的笔记和实验相关的内容.