题目链接 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. php 计算当天凌晨时间戳 以及获取其他常用时间戳

    php 计算当日凌晨时间戳 以及获取其他常用时间戳(持续补充中...) 获取当天凌晨时间戳: echo strtotime(date('Y-m-d')); 以下再列举一些获取其他常用时间戳的方法 获取 ...

  2. nginx作为正向代理,反向代理的一些应用

    正向代理代理的对象是客户端 反向代理代理的对象是服务端 举例说下nginx作为正向代理作访问控制 server{ listen 80; server_name localhost jeson.gaos ...

  3. 【php】php安全问题

    使用 —enable-force-cgi-redirect 选项 设置 doc_root 或 user_dir 或 open_basedir PHP运行的用户身份不能为ROOT 数据库字段加密 程序不 ...

  4. LeetCode(228) Summary Ranges

    题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...

  5. 牛客练习赛22 C 简单瞎搞题

    //位运算 // & 都是1 才是 1 // | 都是0 才是0 // ^ 不一样才是1 #include <iostream> #include <cstdio> # ...

  6. selenium2自动处理验证码

    最近在研究web自动化,登录时发现要输入验证码,之前在做手机app自动化时,就被验证码block了.这次做web时又遇到了,探索之后,发现有如下几个解决办法: 1.联系开发人员,让其帮忙在测试环境中注 ...

  7. RQNOJ:PID30 / [stupid]愚蠢的矿工☆(树形背包)

    PID30 / [stupid]愚蠢的矿工☆ 背景 Stupid 家族得知在HYC家的后花园里的中央花坛处,向北走3步,向西走3步,再向北走3步,向东走3步,再向北走6步,向东走3步,向南走12步,再 ...

  8. 《Scrum实战》第3次课【富有成效的每日站会】作业汇总

    1组 崔儒: http://kecyru.blog.163.com/blog/static/2741661732017626101944123/ 2017-07-26 孟帅: http://www.c ...

  9. FFT、NTT学习笔记

    参考资料 picks miskcoo menci 胡小兔 unname 自为风月马前卒 上面是FFT的,学完了就来看NTT吧 原根 例题:luogu3803 fft优化后模板 #include < ...

  10. python 学习分享-基础篇

    1.python起手式 写下第一个代码,打印‘hello world’ print('hello world') 2.变量 变量是为了存储信息,在程序中被调用,标识数据名称或类型. 变量定义的规则: ...