codeforces600E. Lomsat gelral(dsu on tree笔记)
知识前驱:树链剖分
题意:给出一个树,求出每个节点的子树中出现次数最多的颜色的编号和
分析:递归求解,对于一棵树,求出他的所有子树的颜色编号次数加上它本身,找到最大即是它的答案。对于他的兄弟来说,应该将计数器清0并同过程求解,在最后一个兄弟计算完毕后,此时计数器可不清0,再次统计该兄弟的兄弟(即该兄弟的同深度其他节点),便求出了它的父亲的所有子树的颜色编号次数,再加上它本身,找到最大即是它父亲的答案。
dsu on tree在这样一个暴力的过程中,将重儿子作为上述的最后一个兄弟,此时计数器不清0,再暴力统计轻儿子以获得答案。
1 #pragma GCC optimize(3, "Ofast", "inline")
2
3 #include <bits/stdc++.h>
4
5 #define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
6 #define ll long long
7 #define LL long long
8 #define uu unsigned int
9 #define int ll
10 #define ls st<<1
11 #define rs st<<1|1
12 using namespace std;
13 const int maxn = (ll) 1e6 + 5;
14 const int mod = 1000000007;
15 const int inf = 0x3f3f3f3f;
16
17 int son[maxn], siz[maxn], cnt[maxn], col[maxn], ans[maxn];
18 vector<int> v[maxn];
19
20 void dfs(int x, int pre) {
21 siz[x] = 1;
22 for (auto &to:v[x]) {
23 if (to == pre)
24 continue;
25 dfs(to, x);
26 siz[x] += siz[to];
27 if (siz[to] > siz[son[x]])
28 son[x] = to;
29 }
30 }
31
32 int sum, maxx, now;
33
34 void add(int x, int pre, int val) {
35 cnt[col[x]] += val;
36 if (cnt[col[x]] > maxx) {
37 maxx = cnt[col[x]];
38 sum = col[x];
39 } else if (cnt[col[x]] == maxx)
40 sum += col[x];
41 for (auto &to:v[x]) {
42 if (to == now || to == pre)
43 continue;
44 add(to, x, val);
45 }
46 }
47
48 void dfs2(int x, int pre, bool ord) {
49 for (auto &to:v[x]) {
50 if (to == pre || to == son[x])
51 continue;
52 dfs2(to, x, false);
53 }
54 if (son[x]) {
55 dfs2(son[x], x, true);
56 now = son[x];
57 }
58 add(x, pre, 1);
59 ans[x] = sum;
60 if (!ord) {
61 now = 0;
62 add(x, pre, -1);
63 sum = 0;
64 maxx = 0;
65 }
66 }
67
68 signed main() {
69 start;
70 int n;
71 cin >> n;
72 for (int i = 1; i <= n; ++i)
73 cin >> col[i];
74 for (int i = 1; i < n; ++i) {
75 int x, y;
76 cin >> x >> y;
77 v[x].push_back(y);
78 v[y].push_back(x);
79 }
80 dfs(1, 0);
81 dfs2(1, 0, 0);
82 for (int i = 1; i <= n; ++i)
83 cout << ans[i] << ' ';
84 return 0;
85 }
codeforces600E. Lomsat gelral(dsu on tree笔记)的更多相关文章
- codeforces600E. Lomsat gelral(dsu on tree)
dsu on tree先分轻重儿子先处理轻边,再处理重儿子再加上轻儿子的答案 #include<iostream> #include<cstdio> #include<q ...
- 【CodeForces】600 E. Lomsat gelral (dsu on tree)
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...
- Codeforces.600E.Lomsat gelral(dsu on tree)
题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- cf600E. Lomsat gelral(dsu on tree)
题意 题目链接 给出一个树,求出每个节点的子树中出现次数最多的颜色的编号和 Sol dsu on tree的裸题. 一会儿好好总结总结qwq #include<bits/stdc++.h> ...
- CF 600E. Lomsat gelral(dsu on tree)
解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...
- [Codeforces600E] Lomsat gelral(树上启发式合并)
[Codeforces600E] Lomsat gelral(树上启发式合并) 题面 给出一棵N个点的树,求其所有子树内出现次数最多的颜色编号和.如果多种颜色出现次数相同,那么编号都要算进答案 N≤1 ...
- codeforces600E Lomsat gelral【线段树合并/DSU】
第一次AC这道题,是三年前的一个下午,也许晚上也说不定.当时使用的\(DSU\) \(on\) \(tree\)算法,如今已经淡忘,再学习新的算法过程中,却与旧物重逢.生活中充满不可知会的相遇,即使重 ...
- codeforces600E Lomsat gelral
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Educational Codeforces Round 2 E. Lomsat gelral(dsu)
题目链接 题意:给你一棵以1为根n个点的树,问你以i为根的子树的众数和是多少 思路:dsu是一种优化暴力的手段 首先进行轻重链剖分 然后只记录重链的信息 轻链的信息就直接暴力查找 经过证明这样复杂度可 ...
随机推荐
- 修改本地hosts
本地hosts文件地址 C:\Windows\System32\drivers\etc 修改前 修改后 可以使用记事本打开hosts文件或使用Notepad++ 工具打开 按图示添加,修改即可
- L3-017 森森快递
一.题目: 7-2 森森快递 (30 分) 森森开了一家快递公司,叫森森快递.因为公司刚刚开张,所以业务路线很简单,可以认为是一条直线上的N个城市,这些城市从左到右依次从0到(N−1)编号.由于道路限 ...
- openlayers 使用canvas绘制圆形头像图标
记录一个使用canvas 将一张图片等比缩放,裁剪为一个圆 1.原始图片 2.绘制后在地图中呈现的样式 3.设置样式的函数 /** * 设置Style */ setStyleOnPersonLocat ...
- 【python基础】复杂数据类型-列表类型(排序/长度/遍历)
1.列表数据元素排序 在创建的列表中,数据元素的排列顺序常常是无法预测的.这虽然在大多数情况下都是不可避免的,但经常需要以特定的顺序呈现信息.有时候希望保留列表数据元素最初的排列顺序,而有时候又需要调 ...
- 高精度减法(模板yxc)
#include <bits/stdc++.h> using namespace std; bool cmp(vector<int> &A, vector<int ...
- go语言编写算法
1.冒泡排序 // 冒泡排序 a := []uint8{9, 20, 10, 23, 7, 22, 88, 102} for i := 0; i < len(a); i++ { for k := ...
- CSS3+Jquery实现带动画效果的下拉选择框
CSS3+JQuery实现带动画效果的下拉选择框. 元素结构为: 1 <div class="box"> 2 <p>this is the first li ...
- ObjectInputStream_报错问题
报错: Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: CE ...
- 【环境搭建】docker+nginx部署PHP
目的 使用docker容器完成nginx的安装以及部署PHP网页 步骤 一. 安装nginx 1. 拉取Nginx镜像 docker pull nginx //拉取镜像 docker images / ...
- Django学习笔记:第二章django的安装和创建应用
1.安装Django 终端运行 pip install django 查看django是否安装成功 python -m django --version 1.1 安装虚拟环境 在控制台运行 pip i ...