题目链接 Lomsat gelral

占坑……等深入理解了再来补题解……

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)    for (int i(a); i <= (b); ++i)

typedef long long LL;

const int N = 600010;

int n;
int cc[N], col[N], sz[N], son[N];
LL ans[N]; vector <int> v[N]; void getsize(int x, int fa){
sz[x] = 1;
for (auto u : v[x]){
if (u == fa) continue;
getsize(u, x); sz[x] += sz[u];
if (!son[x] || sz[u] > sz[son[x]]) son[x] = u;
}
} bool skip[N];
LL sum = 0;
int cx = 0;
int x, y; void edt(int x, int fa, int k){
cc[col[x]] += k;
if (k > 0 && cc[col[x]] >= cx){
if (cc[col[x]] > cx)
sum = 0, cx = cc[col[x]];
sum += col[x];
} for (auto u : v[x]){
if (u == fa || skip[u]) continue;
edt(u, x, k);
} } void dfs(int x, int fa, int kep = 0){
for (auto u : v[x]){
if (u == fa || u == son[x]) continue;
dfs(u, x, 0);
} if (son[x]) dfs(son[x], x, 1), skip[son[x]] = 1;
edt(x, fa, 1); ans[x] = sum;
if (son[x]) skip[son[x]] = 0;
if (!kep) edt(x, fa, -1), cx = sum = 0;
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%d", col + i); rep(i, 1, n - 1){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} getsize(1, 0);
dfs(1, 0, 0); rep(i, 1, n) printf("%lld\n", ans[i]);
return 0;
}

Codeforces 600E Lomsat gelral (树上启发式合并)的更多相关文章

  1. CF EDU - E. Lomsat gelral 树上启发式合并

    学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...

  2. codeforces 600E . Lomsat gelral (线段树合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  3. CF 600 E Lomsat gelral —— 树上启发式合并

    题目:http://codeforces.com/contest/600/problem/E 看博客:https://blog.csdn.net/blue_kid/article/details/82 ...

  4. CF600E Lomsat gelral 树上启发式合并

    题目描述 有一棵 \(n\) 个结点的以 \(1\) 号结点为根的有根树. 每个结点都有一个颜色,颜色是以编号表示的, \(i\) 号结点的颜色编号为 \(c_i\)​. 如果一种颜色在以 \(x\) ...

  5. 【CF600E】Lomsat gelral——树上启发式合并

    (题面来自luogu) 题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. ci <= n <= 1e5 裸题.统计时先扫一遍得到出 ...

  6. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

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

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

  8. codeforces 600E. Lomsat gelral 启发式合并

    题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...

  9. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

随机推荐

  1. 动态设置html的title

    使用vue前端框架做,竟然丢弃了很多javascript和html的东西了..动态设置title的方法: 1.使用vue的自定义指令 <div v-title>{{htmltitle}}& ...

  2. 【mysql】【转发】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,I

    [Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for o ...

  3. Huawei warns against 'Berlin Wall' in digital world

    From China Daily Huawei technologies criticized recent registration imposed on the Chinese tech comp ...

  4. w3resource_MySQL练习:Joins

    w3resource_MySQL练习题:Joins 1. Write a query to find the addresses (location_id, street_address, city, ...

  5. NordicSemiconductor.nRF_DeviceFamilyPack 更新历史记录

    Version: 8.17.0 (2018-04-27) NordicSemiconductor.nRF_DeviceFamilyPack.8.17.0.packDownload For nRF528 ...

  6. PAT Basic 1082

    1082 射击比赛 本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军:谁差得最远,谁就是菜鸟.本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟.我们假设靶心在原 ...

  7. hql的笔记

    删除方法 getSession().delete(arg0); 今天写这个删除语句的时候运用这delete()这个方法,根据id删除记录,hql和普通的sql还是有区别的, 普通的sql封装好了del ...

  8. 遍历Request.QueryString

    Request.QueryString 返回的是 NameValueCollection, 而NameValueCollection实现了IEnumerable的GetEnumerator方法,只是G ...

  9. oracle游标遍历

    --创建存储过程 CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p (--参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型. is_ ...

  10. Cookie和Session的作用和工作原理

    一.Cookie详解 (1)简介 因为HTTP协议是无状态的,即服务器不知道用户上一次做了什么,这严重阻碍了交互式Web应用程序的实现.在典型的网上购物场景中,用户浏览了几个页面,买了一盒饼干和两饮料 ...