Lomsat gelral CodeForces - 600E (树上启发式合并)
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.
Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.
The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.
For each vertex v find the sum of all dominating colours in the subtree of vertex v.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of vertices in the tree.
The second line contains n integers ci (1 ≤ ci ≤ n), ci — the colour of the i-th vertex.
Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the edge of the tree. The first vertex is the root of the tree.
Output
Print n integers — the sums of dominating colours for each vertex.
Examples
Input
4
1 2 3 4
1 2
2 3
2 4
Output
10 9 3 4
Input
15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
Output
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
题意:
给你一颗以1为根的树,每一个节点有一个颜色。
询问你对于从1到n每一个节点为根的子树中,颜色最多的是哪个颜色?如果有多个颜色数量一样多,答案应该是他们的sum和。
思路:
dsu on tree 的入门题,
我们知道如果直接暴力求对于每一个节点为根的子树话,时间复杂度是 n * n的,显然会tle,的
我们可以利用树的重儿子和轻儿子的性质来优化暴力,而理论的时间复杂度是 O(nlogn)
我们从树根开始dfs,对于每一个节点,我们先暴力处理他的轻儿子,维护出清儿子的答案,同时清空轻儿子的贡献。
而对于重儿子,我们同样暴力处理,但是不删除他的贡献,因为重儿子节点可以对它的父节点有贡献,即我们在算重儿子的父节点的答案时,就不需要去扫它的重儿子了,因为已经处理过了。
同时,树链剖分的知识我们可以知道,这样处理的话,对于每一个节点,如果他是重儿子,他只会被访问1次,如果是轻儿子,最多访问logn次。所以时间复杂度是 O(nl ogn )
推荐学习本知识点的博客:https://www.cnblogs.com/zwfymqz/p/9683124.html
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
std::vector<int> son[maxn];
int wson[maxn];
int SZ[maxn];
int a[maxn];
void dfs1(int x,int pre)
{
SZ[x]=1;
int maxson=-1;
for(auto y:son[x])
{
if(y!=pre)
{
dfs1(y,x);
SZ[x]+=SZ[y];
if(maxson<SZ[y])
{
maxson=SZ[y];
wson[x]=y;
}
}
}
}
ll ans[maxn];
ll sum;
int isson;
int m;
ll cnt[maxn];
void add(int x,int pre,int val)
{
cnt[a[x]]+=val;
if(cnt[a[x]]>m)
{
m=cnt[a[x]];
sum=a[x];
}else if(cnt[a[x]]==m)
{
sum+=a[x];
}
for(auto y:son[x])
{
if(y==pre||y==isson)
continue;
add(y,x,val);
}
}
void dfs2(int x,int pre,int op)
{
for(auto y:son[x])
{
if(y==pre||y==wson[x])
{
continue;
}
dfs2(y,x,0);
}
if(wson[x])
{
dfs2(wson[x],x,1);
isson=wson[x];
}
add(x,pre,1);
isson=0;
ans[x]=sum;
if(op==0)
{
add(x,pre,-1);
sum=0;
m=0;
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gg(n);
repd(i,1,n)
{
gg(a[i]);
}
int u,v;
repd(i,2,n)
{
gg(u);gg(v);
son[u].pb(v);
son[v].pb(u);
}
dfs1(1,0);
dfs2(1,0,0);
repd(i,1,n)
{
printf("%lld ",ans[i] );
}
printf("\n");
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Lomsat gelral CodeForces - 600E (树上启发式合并)的更多相关文章
- Codeforces - 600E 树上启发式合并
题意:求每一个子树存在最多颜色的颜色代号和(可重复) 本题是离线统计操作,因此可以直接合并重儿子已达到\(O(nlogn)\)的复杂度 PS.不知道什么是启发式合并的可以这样感受一下:进行树链剖分,分 ...
- 【CF600E】Lomset gelral 题解(树上启发式合并)
题目链接 题目大意:给出一颗含有$n$个结点的树,每个节点有一个颜色.求树中每个子树最多的颜色的编号和. ------------------------- 树上启发式合并(dsu on tree). ...
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- [Codeforces600E] Lomsat gelral(树上启发式合并)
[Codeforces600E] Lomsat gelral(树上启发式合并) 题面 给出一棵N个点的树,求其所有子树内出现次数最多的颜色编号和.如果多种颜色出现次数相同,那么编号都要算进答案 N≤1 ...
- 【学习笔记/题解】树上启发式合并/CF600E Lomsat gelral
题目戳我 \(\text{Solution:}\) 树上启发式合并,是对普通暴力的一种优化. 考虑本题,最暴力的做法显然是暴力统计每一次的子树,为了避免其他子树影响,每次统计完子树都需要清空其信息. ...
- Codeforces 208E - Blood Cousins(树上启发式合并)
208E - Blood Cousins 题意 给出一棵家谱树,定义从 u 点向上走 k 步到达的节点为 u 的 k-ancestor.多次查询,给出 u k,问有多少个与 u 具有相同 k-ance ...
- 树上启发式合并(dsu on tree)学习笔记
有丶难,学到自闭 参考的文章: zcysky:[学习笔记]dsu on tree Arpa:[Tutorial] Sack (dsu on tree) 先康一康模板题吧:CF 600E($Lomsat ...
- 神奇的树上启发式合并 (dsu on tree)
参考资料 https://www.cnblogs.com/zhoushuyu/p/9069164.html https://www.cnblogs.com/candy99/p/dsuontree.ht ...
随机推荐
- Java语言 List 和 Array 相互转换
Java语言 List 和 Array 相互转换 List集合 转换为 Array数组 List集合 转换成 Array数组,有 2 种方式,代码如下: import java.util.ArrayL ...
- ubuntu搭建ssh服务
本人在ubuntu16.4.4.0-13下测试 #man uname//用于打印系统信息 sudo apt install update sudo apt install openssh-server ...
- C# List方法中存储的问题
遇到一个bug,抓耳挠塞好久都没有解决,有必要记录一下. 现在我使用了一个多维list. IList<IList<int>> list = new List<IList& ...
- 基于OpenCV的循环行、列移动函数circShift()
///*12 在Matlab中有个circShift()函数,可以实现行.列的循环移动 /// 在返卷积运算中,会用到这个函数.所以,在Opencv中我也定义同样 /// 功能的函数 /// 该函数有 ...
- 直方图匹配原理与python、matlab实现
直方图匹配本质上是让两幅图像的累积直方图尽量相似,累积直方图相似了,直方图也就相似了. 把原图像img的直方图匹配到参考图像ref的直方图,包括以下几个步骤: 1. 求出原图像img的累积直方图img ...
- 【Linux开发】linux设备驱动归纳总结(七):1.时间管理与内核延时
linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- C++学习笔记-继承
类之间可以建立联系,这就使得类可以有某种关系 类之间的关系 has-A:包含关系,一个类使用另一个已经定义好的类的数据 uses-A:使用关系,友元或者对象参数传递 is-A:是的关系,这就是继承,具 ...
- 【linux杂谈】centos6和centos7中固定IP的方法
众所周知,一大部分集合部署的应用服务器内网相互通信都是采用固定IP.在阿里云.腾讯云上申请的云服务器也是固定IP,这就意味着在云平台内部策略划拨肯定是也固定了IP(即便不是采取直接在系统内固定的方式) ...
- Spring Boot 五种热部署方式,极速开发就是生产力!
1.模板热部署 在 Spring Boot 中,模板引擎的页面默认是开启缓存的,如果修改了页面的内容,则刷新页面是得不到修改后的页面的,因此我们可以在application.properties中关闭 ...
- JavaScript更改css样式
来源:https://www.w3school.com.cn/js/js_htmldom_css.asp 1, document.getElementById(id).style.property = ...