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^ ...
随机推荐
- JDBC(2):JDBC对数据库进行CRUD
一. statement对象 JDBC程序中的Connection用于代表数据库的链接:Statement对象用于向数据库发送SQL语句:ResultSet用于代表Sql语句的执行结果 JDBC中的s ...
- 连接 MySQL 数据库出现问题:The server time zone value ‘�й���ʱ��‘ is unrecogni....
出现问题 The server time zone value '�й���ʱ��' is unrecogni.... 解决方案 在 URL 后面加上 ?serverTimezone=UTC 如下: ...
- 【Linux】【Services】【Docker】网络
容器的网络模型: closed container: 仅有一个接口:loopback 不参与网络通信,仅适用于无须网络通信的应用场景,例如备份.程序调试等: --net none bridged co ...
- 基于TSN 802.1AS协议的时间同步分析(7种延时,1次厘清)
前言 往期的时间同步--802.1AS协议介绍一文向大家介绍了802.1AS协议的基本内容,基于时间同步协议就能实现主节点与各从节点的时间同步,但是如何评价时间同步的优劣呢?这就需要我们通过一些时间同 ...
- mysql深度优化与理解(迄今为止读到最优秀的mysql博客)
转载:https://www.cnblogs.com/shenzikun1314/p/6396105.html 本篇深入了解查询优化和服务器的内部机制,了解MySql如何执行特定查询,从中也可以知道如 ...
- 转: iPhone屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- Mit6.830 - simpledb - 总览
总览 github 地址: https://github.com/CreatorsStack/CreatorDB 在开始 simpledb 旅途之前, 我们先从整体上来看看 SimpleDb 是一个 ...
- house of force----gyctf_2020_force!!
做这道题前线学习一下house of force的用法 Linux下堆溢出利用2-House of force基本原理_haibiandaxia的博客-CSDN博客 老样子例行检查(这里我就不放了) ...
- 解析Redis操作五大数据类型常用命令
摘要:分享经常用到一些命令和使用场景总结,以及对Redis中五大数据类型如何使用cmd命令行的形式进行操作的方法. 本文分享自华为云社区<Redis操作五大数据类型常用命令解析>,作者:灰 ...
- 创建Harbor私有仓库
前提 1.安装docker服务 参考:https://blog.csdn.net/weixin_36522099/article/details/108861134 老名字:docker.docker ...