CF 600 E. Lomsat gelral
E. Lomsat gelral
http://codeforces.com/contest/600/problem/E
题意:
求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号求和)。
分析:
dsu on tree。
这个可以解决一系列不带修改的子树查询问题。
考虑暴力的思路:就是枚举每个子树,计算每个颜色出现的个数。统计答案。
dsu on tree:最后一个子树枚举计算完了,它的贡献可以保留,然后用其它的子树去合并。(最后一棵子树是最大的)。现在的复杂度就是nlogn了。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int head[N], nxt[N << ], to[N << ], En;
int fa[N], siz[N], son[N], cnt[N], col[N], Mx;
LL ans[N], Sum; void add_edge(int u,int v) {
++En; to[En] = v; nxt[En] = head[u]; head[u] = En;
++En; to[En] = u; nxt[En] = head[v]; head[v] = En;
} void dfs(int u) {
siz[u] = ;
for (int i=head[u]; i; i=nxt[i]) {
int v = to[i];
if (v == fa[u]) continue;
fa[v] = u;
dfs(v);
siz[u] += siz[v];
if (!son[u] || siz[son[u]] < siz[v]) son[u] = v;
}
} void add(int u) {
cnt[col[u]] ++;
if (cnt[col[u]] > Mx) Mx = cnt[col[u]], Sum = col[u];
else if (cnt[col[u]] == Mx) Sum += col[u];
}
void Calc(int u) {
add(u);
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u]) Calc(to[i]);
}
void Clear(int u) {
cnt[col[u]] --;
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u]) Clear(to[i]);
} void solve(int u,bool c) {
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u] && to[i] != son[u]) solve(to[i], );
if (son[u]) solve(son[u], );
add(u);
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u] && to[i] != son[u]) Calc(to[i]);
ans[u] = Sum;
if (!c) Clear(u), Mx = , Sum = ;
} int main() {
int n = read();
for (int i=; i<=n; ++i) col[i] = read();
for (int i=; i<n; ++i) {
int u = read(), v = read();
add_edge(u, v);
}
dfs();
solve(, );
for (int i=; i<=n; ++i) printf("%I64d ",ans[i]);
return ;
}
CF 600 E. Lomsat gelral的更多相关文章
- CF 600 E Lomsat gelral —— 树上启发式合并
题目:http://codeforces.com/contest/600/problem/E 看博客:https://blog.csdn.net/blue_kid/article/details/82 ...
- Codeforces 600 E - Lomsat gelral
E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...
- 【CodeForces】600 E. Lomsat gelral (dsu on tree)
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- Codeforces 600 E. Lomsat gelral (dfs启发式合并map)
题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...
- 「CF 600E」 Lomsat gelral
题目链接 戳我 \(Describe\) 给出一棵树,每个节点有一个颜色,求每个节点的子树中颜色数目最多的颜色的和. \(Solution\) 这道题为什么好多人都写的是启发式合并,表示我不会啊. 这 ...
- 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 ...
- 【CF600E】Lomsat gelral(dsu on tree)
[CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...
- codeforces 600E E. Lomsat gelral (线段树合并)
codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...
随机推荐
- win10 x64 注册ZQDeviceOcx.ocx控件
正常的方式在32位系统下可行, 但是在64位系统下是不可行的. 在64位系统中正确的注册步骤是: 1. 将对应的ocx和dll放到C:\Windows\SysWOW64目录下. 2. 然后找到C:\W ...
- Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
- 在CentOS实现mysql数据库的自动备份
数据是一个比较重要的数据,经常需要备份,每次都手动比较麻烦.本脚本主要现实在CentOS中实现对数据库的备份和保留最近十五天的备份文件.避免太多无用陈旧的备份占用空间. #!/bin/bashid=& ...
- [19/03/30-星期六] IO技术_四大抽象类_ 字节流( 字节输入流 InputStream 、字符输出流 OutputStream )_(含字节文件缓冲流)
一.概念及分类 InputStream(输入流)/OutputStream(输出流)是所有字节输入输出流的父类 [注]输入流和输出流的是按程序运行所在的内存的角度划分的 字节流操作的数据单元是8的字节 ...
- springboot——我的第一个工程
前言:使用Spring Boot 微服务架构有一段时间了,打算从今天开始记录使用过程. 一.Spring Boot介绍: 简介:Spring Boot 框架的产生,是为了方便我们简化Spring 框架 ...
- matlab中boxplot字体大小设置
网上找到的:set(findobj(gca,'Type','text'),'FontSize',18) boxplot() uses the default axes labeling for the ...
- maven中的坐标和仓库
1.坐标 pom.xml中的groupId.artifactId和version都可以构成项目的坐标. <dependency> <groupId></groupI ...
- pl/sql developer开发工具的beautifier美化插件
对于存储过程中需要编写大量的sql语句,这必然需要美化语句,使得程序可读性更高. pl/sql developer开发工具自带美化工具,不过美化的时候容易使得语句全部改变成大写格式,这样就需要一个插件 ...
- phalcon框架与Volt 模块引擎 使用简介
———— 近期工作中web页面使用由C语言编写的Volt模板引擎,相比之前由js动态加载页面速度更快,更利于百度数据的抓取,现根据文档整理一下使用思路 (Volt是一个超快速和设计者友好的模板语言 ...
- 5 替换空格 JavaScript
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 原来一个空格字符,替换之后 ...