题目链接:http://codeforces.com/contest/600/problem/E

给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少。

最容易想到的是n*n的暴力,但是会超时。所以这里用到类似并查集的合并,对于每个子树颜色种数少的合并到颜色种数大的当中。

不懂的看代码应该可以明白。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
map <int, int> cnt[N]; //first代表颜色编号,second代表出现次数
map <int, LL> sum[N]; //first代表出现次数,second代表颜色编号之和
LL ans[N];
int head[N], tot, a[N];
struct Edge {
int next, to;
}edge[N << ]; void init() {
memset(head, -, sizeof(head));
} inline void add(int u, int v) {
edge[tot].next = head[u];
edge[tot].to = v;
head[u] = tot++;
} void dfs(int u, int p) {
cnt[u][a[u]] = ; //初始化:a[u]颜色出现一次
sum[u][] = (LL)a[u]; //初始化:出现一次的颜色之和为a[u]
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs(v, u);
if(cnt[u].size() < cnt[v].size()) { //颜色种类少的合并到颜色种类多的,u为颜色种类多的子树
swap(cnt[u], cnt[v]);
swap(sum[u], sum[v]);
}
for(auto it: cnt[v]) {
cnt[u][it.first] += it.second; //it.first颜色出现次数合并累加
int temp = cnt[u][it.first]; //temp为it.first颜色次数
sum[u][temp] += (LL)it.first; //累加出现temp次的颜色
}
cnt[v].clear(); //清空
sum[v].clear();
}
ans[u] = sum[u].rbegin()->second; //最大出现次数的颜色之和
} int main()
{
init();
int n, u, v;
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
}
for(int i = ; i < n; ++i) {
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, -);
for(int i = ; i <= n; ++i) {
printf("%lld%c", ans[i], i == n? '\n': ' ');
}
return ;
}

Codeforces 600 E. Lomsat gelral (dfs启发式合并map)的更多相关文章

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

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

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

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

  3. Codeforces 600 E - Lomsat gelral

    E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...

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

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

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

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

  6. Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map

    E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...

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

    codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...

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

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

  9. CF 600 E. Lomsat gelral

    E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...

随机推荐

  1. UVa 10891 (博弈+DP) Game of Sum

    最开始的时候思路就想错了,就不说错误的思路了. 因为这n个数的总和是一定的,所以在取数的时候不是让自己尽可能拿的最多,而是让对方尽量取得最少. 记忆化搜索(时间复杂度O(n3)): d(i, j)表示 ...

  2. nginx日志切割并使用flume-ng收集日志

    nginx的日志文件没有rotate功能.如果你不处理,日志文件将变得越来越大,还好我们可以写一个nginx日志切割脚本来自动切割日志文件.第一步就是重命名日志文件,不用担心重命名后nginx找不到日 ...

  3. 对比C++中的指针和引用

    指针和引用在形式上比较好区分,由于有很多相似的功能,因此在使用上容易混淆.因此有必要对指针和引用进行对比,以便于在使用时使程序正确高效. 1.引用不可以为空,而指针可以为空. 我们知道引用是对象的别名 ...

  4. 09day2

    多米诺骨牌 递推+高精度 [问题描述] Jzabc 对多米诺骨牌有很大兴趣,然而他的骨牌比较特别,只有黑色的和白色的两种.他觉得如果存在连续三个骨牌是同一种颜色,那么这个骨牌排列便是不美观的.现在他有 ...

  5. for-in遍历json数据

    1.for遍历json数据 ','fun':'前端开发'} for(var attr in json){ alert(json[attr]) //遍历json属性的数据 alert(json['nam ...

  6. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.2.Oracleasm Createdisk ASM磁盘失败:Instantiating disk: failed

    1.错误信息:Instantiating disk: failed [root@linuxrac1 /]# /usr/sbin/oracleasm createdisk OCR_VOTE /dev/s ...

  7. Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者

    Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者 这一篇主要讲作为一名使用公众平台接口的开发者,你需要知道的一些东西.其中也涉及到一些微信官方的规定或比较掩蔽的注意点 ...

  8. getHibernateTemplate() 一直报NullPointerException 错误

    原来是调用方法有误正确调用方法: ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContex ...

  9. file的getPath getAbsolutePath和getCanonicalPath的不同

    file的这几个取得path的方法各有不同,下边说说详细的区别 概念上的区别:(内容来自jdk,个人感觉这个描述信息,只能让明白的人明白,不明白的人看起来还是有点难度(特别试中文版,英文版稍好些)所以 ...

  10. php codeigniter (CI) oracle 数据库配置-宋正河整理

    database.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $active_group = 'default'; $active_record ...