题意

题目链接

给出一个树,求出每个节点的子树中出现次数最多的颜色的编号和

Sol

dsu on tree的裸题。

一会儿好好总结总结qwq

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, col[MAXN], son[MAXN], siz[MAXN], cnt[MAXN], Mx, Son;
LL sum = , ans[MAXN];
vector<int> v[MAXN];
void dfs(int x, int fa) {
siz[x] = ;
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa) continue;
dfs(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void add(int x, int fa, int val) {
cnt[col[x]] += val;
if(cnt[col[x]] > Mx) Mx = cnt[col[x]], sum = col[x];
else if(cnt[col[x]] == Mx) sum += (LL)col[x];
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa || to == Son) continue;
add(to, x, val);
}
}
void dfs2(int x, int fa, int opt) {
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa) continue;
if(to != son[x]) dfs2(to, x, );
}
if(son[x]) dfs2(son[x], x, ), Son = son[x]; add(x, fa, ); Son = ;
ans[x] = sum;
if(!opt) add(x, fa, -), sum = , Mx = ;
}
int main() {
N = read();
for(int i = ; i <= N; i++) col[i] = read();
for(int i = ; i <= N - ; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dfs(, );
dfs2(, , );
for(int i = ; i <= N; i++) printf("%I64d ", ans[i]);
return ;
}

cf600E. Lomsat gelral(dsu on tree)的更多相关文章

  1. 【CodeForces】600 E. Lomsat gelral (dsu on tree)

    [题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...

  2. Codeforces.600E.Lomsat gelral(dsu on tree)

    题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...

  3. Codeforces 600E. Lomsat gelral(Dsu on tree学习)

    题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...

  4. CF 600E. Lomsat gelral(dsu on tree)

    解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...

  5. codeforces600E. Lomsat gelral(dsu on tree)

    dsu on tree先分轻重儿子先处理轻边,再处理重儿子再加上轻儿子的答案 #include<iostream> #include<cstdio> #include<q ...

  6. CF600E Lomsat gelral(dsu on tree)

    dsu on tree跟冰茶祭有什么关系啊喂 dsu on tree的模板题 思想与解题过程 类似树链剖分的思路 先统计轻儿子的贡献,再统计重儿子的贡献,得出当前节点的答案后再减去轻儿子对答案的贡献 ...

  7. dsu on tree(CF600E Lomsat gelral)

    题意 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. dsu on tree 用来解决子树问题 好像不能带修改?? 暴力做这个题,就是每次扫一遍子树统 ...

  8. CF600E Lomsat gelral——线段树合并/dsu on tree

    题目描述 一棵树有$n$个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. 这个题意是真的窒息...具体意思是说,每个节点有一个颜色,你要找的是每个子树中颜色的众数 ...

  9. CF600E Lomsat gelral 和 CF741D Dokhtar-kosh paths

    Lomsat gelral 一棵以\(1\)为根的树有\(n\)个结点,每个结点都有一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号(若有数量一样的,则求编号和). \(n \le 10^ ...

随机推荐

  1. centos 7之文件共享

    一,安装过程 1.在VirtualBox的软件菜单里面选择 “设备”--“存储”,添加VBoxGuestAdditions.iso(在VirtualBox目录下).      2.在桌面上出现一个光驱 ...

  2. pig入门教程(2)

    本文可以让刚接触pig的人对一些基础概念有个初步的了解. 本文的大量实例都是作者Darran Zhang(website: codelast.com)在工作.学习中总结的经验或解决的问题,并且添加了较 ...

  3. C#将PDF转换为图片的方法

    1.需要添加引用  O2S.Components.PDFRender4NET 链接: https://pan.baidu.com/s/1ZPTaLTnFkex6QrsndLjwxg 提取码: jdh2 ...

  4. Golang : cobra 包解析

    笔者在<Golang : cobra 包简介>一文中简要的介绍了 cobra 包及其基本的用法,本文我们从代码的角度来了解下 cobra 的核心逻辑. Command 结构体 Comman ...

  5. 51nod1347(简单逻辑)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1347 题意:中文题诶- 思路:稍推理一下就可以发现字符串a是 ...

  6. 洛谷P1480 A/B Problem(高精除高精)

    P1480 A/B Problem 题目描述 输入两个整数a,b,输出它们的商(a<=10^5000,b<=10^9) 输入输出格式 输入格式: 两行,第一行是被除数,第二行是除数. 输出 ...

  7. mac上gradle升级版本

    参考:https://www.jianshu.com/p/9fa9d2b4dbc9    http://www.gradle.org/downloads下载gradle 终端输入:open .bash ...

  8. 洛谷P3078 [USACO13MAR]扑克牌型Poker Hands

    题目描述 Bessie and her friends are playing a unique version of poker involving a deck with \(N\) (\(1 \ ...

  9. 译——meta viewport

    此文翻译PPK大神的——meta viewport. 主要讲到了第三种viewport——ideal viewport,以及使用meta viewport 设置移动浏览器的视口. the meta v ...

  10. LDAP相关操作注意事项

    lc.Modify(entry.DN, new LdapModification(LdapModification.REPLACE, new LdapAttribute("mDBUseDef ...