Lomsat gelral
题目描述
You are given a rooted tree with root in vertex 11 . Each vertex is coloured in some colour.
Let's call colour cc dominating in the subtree of vertex vv if there are no other colours that appear in the subtree of vertex vv more times than colour cc . So it's possible that two or more colours will be dominating in the subtree of some vertex.
The subtree of vertex vv is the vertex vv and all other vertices that contains vertex vv in each path to the root.
For each vertex vv find the sum of all dominating colours in the subtree of vertex vv .
题目分析
首先分析问题,发现求得是以当前节点为子树的众数和
考虑dsu,在更新答案时,我们统计每一个数的出现次数,如果大了就更新sum,否则更新次数
具体的dsu,我们可以采取重链剖分的思路
重儿子的大小最大,所以将每一个节点先赋为重儿子,在一次统计轻儿子,最后清空影响
在操作轻儿子时,可以用dfn来遍历
对于时间复杂度,因为\(wson_size>=n/2\) 所以,每一次只会操作\(n/2\),递归后,时间为\(O(n*log2(n))\)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=1e5+5;
int n,q,root;
int a[MAXN];
int lsh[MAXN];
vector<int>g[MAXN];
int x,y;
int sum;
int maxi;
int son[MAXN];
int size[MAXN];
int num[MAXN];
int dfn[MAXN];
int dfnc[MAXN];
int cnt=0;
void dfs1(int x,int fa)
{
dfn[x]=++cnt;
dfnc[cnt]=x;
size[x]=1;
int maxi=0;
for(int i=0;i<g[x].size();i++)
{
int v=g[x][i];
if(v==fa)
{
continue;
}
dfs1(v,x);
size[x]+=size[v];
if(maxi<size[v])
{
son[x]=v;
maxi=size[v];
}
}
}
int rec[MAXN];
void dfs(int x,int f,int check)
{
for(int i=0;i<g[x].size();i++)
{
int v=g[x][i];
if(v==f||v==son[x])
{
continue;
}
dfs(v,x,0);
}
if(son[x])
{
dfs(son[x],x,1);
}
num[a[x]]++;
if(maxi<num[a[x]])
{
maxi=num[a[x]];
sum=a[x];
}
else if(maxi==num[a[x]])
{
sum+=a[x];
}
for(int i=0;i<g[x].size();i++)
{
int v=g[x][i];
if(v==f||v==son[x])
{
continue;
}
for(int j=dfn[v];j<=dfn[v]+size[v]-1;j++)
{
int key=dfnc[j];
num[a[key]]++;
if(maxi<num[a[key]])
{
maxi=num[a[key]];
sum=a[key];
}
else if(maxi==num[a[key]])
{
sum+=a[key];
}
}
}
rec[x]=sum;
// printf("%d %d\n",x,sum);
if(!check)
{
for(int j=dfn[x];j<=dfn[x]+size[x]-1;j++)
{
int key=dfnc[j];
num[a[key]]--;
}
maxi=0;
sum=0;
}
}
signed main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
for(int i=1;i<n;i++)
{
scanf("%lld %lld",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(1,0);
dfs(1,0,0);
for(int i=1;i<=n;i++)
{
printf("%lld ",rec[i]);
}
}
Lomsat gelral的更多相关文章
- Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...
- Codeforces 600 E - Lomsat gelral
E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...
- 【CF600E】 Lomsat gelral
CF600E Lomsat gelral Solution 考虑一下子树的问题,我们可以把一棵树的dfn序搞出来,那么子树就是序列上的一段连续的区间. 然后就可以莫队飞速求解了. 但是这题还有\(\T ...
- 【CodeForces】600 E. Lomsat gelral (dsu on tree)
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...
- 【CF600E】Lomsat gelral(dsu on tree)
[CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...
- CF 600 E. Lomsat gelral
E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- Codeforces 600E Lomsat gelral (树上启发式合并)
题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths
Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ...
随机推荐
- 过滤敏感词工具类SensitiveFilter
网上过滤敏感词工具类有的存在挺多bug,这是我自己改用的过滤敏感词工具类,目前来说没啥bug,如果有bug欢迎在评论指出 使用前缀树 Trie 实现的过滤敏感词,树节点用静态内部类表示了,都写在一个 ...
- 什么是API?
一.简介 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序,与开发人员基于某软件或硬件得以访问一组例程的能力,而又 ...
- 华为HMS Core图形引擎服务携手三七游戏打造移动端实时DDGI技术
在2021年HDC大会的主题演讲中提到,华为HMS Core图形引擎服务(Scene Kit)正协同三七游戏一起打造实时DDGI(动态漫反射全局光照:Dynamic Diffuse Global Il ...
- HGAME2021 week2 pwn writeup
week2一共有4道pwn题 killerqueen 有格式化字符串漏洞,题不算难,但是故事情节真实让人摸不着头脑,但是仔细分析分析,理清楚逻辑就可以做了. 第一次choose1的时候,可以输入0,泄 ...
- manjaro(arch系)Linux使用(一)
# manjaro的使用 ## 配置源 先切换国内的镜像源仓库 `sudo pacman-mirrors -i -c China -m rank` 在弹出的窗口中选择延迟最小的源 ### 添加arch ...
- LuoguB2101 计算矩阵边缘元素之和 题解
Content 给定一个 \(m\times n\) 的矩阵,求矩阵边缘元素之和. 数据范围:\(1\leqslant m,n\leqslant 100\). Solution 对于新手来说,看到这题 ...
- CF1041B Buying a TV Set 题解
Content 给定四个数 \(a,b,c,d\),求满足以下条件的数对 \((x,y)\) 的个数: \(x\leqslant a,y\leqslant b\). \(\dfrac{x}{y}=\d ...
- 第二周Python笔记之 变量的三元运算
如果变量a小于b,则d的值取a变量的值,否则取c变量的值
- 多线程多进程学习threading,queue线程安全队列,线程间数据状态读取。threading.local() threading.RLock()
http://www.cnblogs.com/alex3714/articles/5230609.html python的多线程是通过上下文切换实现的,只能利用一核CPU,不适合CPU密集操作型任务, ...
- vnc mirror driver
2012年4月,大二下学期,平静的课堂上老师通知了一个比赛...第一届大学生软件设计大赛!然后我选了第六题:windows屏幕录像.就这样我就开始了我的vc开发生涯. 之前学了c/c++作为基础,自认 ...