Educational Codeforces Round 2 E. Lomsat gelral(dsu)
题意:给你一棵以1为根n个点的树,问你以i为根的子树的众数和是多少
思路:dsu是一种优化暴力的手段 首先进行轻重链剖分 然后只记录重链的信息 轻链的信息就直接暴力查找 经过证明这样复杂度可以是nlogn。
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int N=100007;
vector<int> G[N];
int col[N],dp[N],son[N];
ll cnt[N],ans[N];
ll maxx=-inf,sum=0;
int po;
void dfs(int u,int fa){
dp[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
dfs(v,u);
dp[u]+=dp[v];
if(dp[v]>dp[son[u]]) son[u]=v; //找重儿子
}
}
void add(int u,int fa,int w){
cnt[col[u]]+=w;
if(cnt[col[u]]>maxx) maxx=cnt[col[u]],sum=col[u];
else if(cnt[col[u]]==maxx) sum+=col[u];
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa||v==po) continue;
add(v,u,w);
}
}
void dfss(int u,int fa,int opt){
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa) continue;
if(son[u]!=v) dfss(v,u,0); //优先遍历轻儿子
}
if(son[u]) dfss(son[u],u,1),po=son[u];//po是标记当重链的重儿子 在算当前节点时可以不用遍历重儿子
add(u,fa,1);
po=0;
ans[u]=sum;
if(!opt) add(u,fa,-1),maxx=-inf,sum=0; //消除轻儿子的影响
}
int main(){
ios::sync_with_stdio(false);
int n; cin>>n;
for(int i=1;i<=n;i++){
cin>>col[i];
}
for(int i=1;i<=n-1;i++){
int u,v; cin>>u>>v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,0); dfss(1,0,1);
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
return 0;
}
Educational Codeforces Round 2 E. Lomsat gelral(dsu)的更多相关文章
- 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 ...
- Educational Codeforces Round 2 E - Lomsat gelral
题意:每个节点有个值,求每个节点子树众数和 题解:可线段树合并,维护每个数出现次数和最大出现次数,以及最大出现次数的数的和 //#pragma GCC optimize(2) //#pragma GC ...
- 【CodeForces】600 E. Lomsat gelral (dsu on tree)
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...
- Educational Codeforces Round 64部分题解
Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
随机推荐
- Command2
Ctrl^c 强制终止当前命令执行 chmod {ugoa(user/group/other/all)}{+-=(wrx权限增删改)} 文件或目录 权限 对文件 对目录 r 可查看文件内容 可以列出目 ...
- zabbix v3.0安装部署【转】
关于zabbix及相关服务软件版本: Linux:oracle linux 6.5 nginx:1.9.15 MySQL:5.5.49 PHP:5.5.35 一.安装nginx: 安装依赖包: yum ...
- 切换用户后whoami打印用户的问题
问题: 为何第二个whoami打印的还是root? root@localhost /]# [root@localhost /]# [root@localhost /]# more test.sh #! ...
- 剑指offer 面试题0:高质的代码:即考虑边界条件、特殊输入和错误处理
Q:把一个字符串转换为整数. A1:一个普通但漏洞百出的解法. int StrToInt(char* str) { int number = 0; while (*str != 0) { number ...
- GMT UTC CST ISO 夏令时 时间戳,都是些什么鬼?
目录 ✍前言 本文提纲 版本约定 ✍正文 GMT:格林威治时间 凭什么格林威治作为标准时间? 地球自转 中国有哪几个时区? 美国有哪几个时区? GMT和Http协议的渊源 UTC:世界标准时间 UTC ...
- 基础Markdown语法
Markdown语法 1.标题 //标题语法 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 一级标题 二级标题 三级标题 四级标题 ...
- 爬虫-使用lxml解析html数据
使用lxml之前,我们首先要会使用XPath.利用XPath,就可以将html文档当做xml文档去进行处理解析了. 一.XPath的简单使用: XPath (XML Path Language) 是一 ...
- 使用yaml配置文件管理资源
[root@k8s-master ~]# vim nginx-deployment.yaml apiVersion: apps/v1beta2 kind: Deployment metadata: n ...
- [Usaco2007 Feb]Cow Party
题目描述 农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前 ...
- 【高并发】ReadWriteLock怎么和缓存扯上关系了?!
写在前面 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在并发场景中, ...