Codeforces 600E Lomsat gelral (树上启发式合并)
题目链接 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 (树上启发式合并)的更多相关文章
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- 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 ...
- CF 600 E Lomsat gelral —— 树上启发式合并
题目:http://codeforces.com/contest/600/problem/E 看博客:https://blog.csdn.net/blue_kid/article/details/82 ...
- CF600E Lomsat gelral 树上启发式合并
题目描述 有一棵 \(n\) 个结点的以 \(1\) 号结点为根的有根树. 每个结点都有一个颜色,颜色是以编号表示的, \(i\) 号结点的颜色编号为 \(c_i\). 如果一种颜色在以 \(x\) ...
- 【CF600E】Lomsat gelral——树上启发式合并
(题面来自luogu) 题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. ci <= n <= 1e5 裸题.统计时先扫一遍得到出 ...
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- codeforces 600E. Lomsat gelral 启发式合并
题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...
- Codeforces 600 E. Lomsat gelral (dfs启发式合并map)
题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...
随机推荐
- GoF23种设计模式之行为型模式之命令模式
一.概述 将一个请求封装为一个对象,从而可以使用不同的请求对客户端进行参数化.对请求排队或记录请求日志,以及支持撤销的操作. 二.适用性 1.当抽象出待执行的动作以参数化某个对象的时候. 2.当需要在 ...
- MySQL练习50题
介绍一个学习SQL的网站:https://sqlbolt.com/ 习题来源于网络,SQL语句是自己的练习答案,部分参考了网络上的答案. 花了一晚上的时间做完,个人认为其中的难点有:分组提取前几名的数 ...
- SQL登录注册练习
/class User package com.neusoft.bean; public class User { private int password; private String name; ...
- selenium2-框架思想介绍
为什么要有框架? 可维护性 提高编写脚本效率 提高脚本的可读性 框架的几大要素: 1. driver管理 2. 脚本 3. 数据 4. 元素对象 5. Log 6. 报告 7. 运行机制 8. 失败用 ...
- jquery获得iframe内容的高度
html: <iframe name="rightgp" id="right_frame_h" src="/Poster/rightgp&quo ...
- 转:获取GridView中RowCommand的当前索引行
获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设 ...
- jqery实现一个图标上下滑动效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 安装adb工包
下载android sdk (很大) 从D:\AndroidSdk\platform-tools目录可以看到: 将adb工具包: adb.exe,AdbWinapi.dll,AdbWinUSBapi. ...
- 【转】简要分析unity3d中剪不断理还乱的yield
在学习unity3d的时候很容易看到下面这个例子: 1 void Start () { 2 StartCoroutine(Destroy()); 3 } 4 5 IEnumerator Destroy ...
- iOS-----openGL--openGL ES iOS 入门篇2--->绘制一个多边形
在上一篇我们学习了如何搭建IOS下openGL的开发环境,接下来我们来学习如何绘制一个多边形. 在2.0之前,es的渲染采用的是固定管线,何为固定管线,就是一套固定的模板流程,局部坐标变换 -> ...