题目链接: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. BZOJ3028: 食物

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3028 题解:列出母函数乘起来化简之后再展开,用插板法即可. 代码: #include<c ...

  2. OK335xS ethtool 移植

    /******************************************************************* * OK335xS ethtool 移植 * 声明: * 由于 ...

  3. 在SQL语言中,join什么时候用,什么时候不用啊?请高手举例解释一下。谢谢

    JOIN 在内连接时,可以不使用,其它类型连接必须使用.如SELECT * FROM TABLEA INNER JOIN TABLEB ON A.ID=B.ID可以这样写:SELECT * FROM ...

  4. App中嵌入网页浏览器

    TOWebViewController 插件 NSURL *url =[NSURL URLWithString:@"http://192.168.1.134:8180/Home/IndexP ...

  5. socket基础实例(一个服务端对应一个客户端情形)

    服务端处理1个客户端的例子 运行结果: (1) while(accept+if(recv)) 情形 执行服务端进程: [root@localhost single_link]# ./server [s ...

  6. 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 ...

  7. FreeMarker笔记 第四章 其它

    4.1 自定义指令 4.1.1 简介 自定义指令可以使用macro指令来定义.Java程序员若不想在模板中实现定义指令,而是在Java语言中实现指令的定义,这时可以使用freemarker.templ ...

  8. html中的div、td 、p 等容器内强制换行和不换行的实现

    div.td .p 等容器内强制换行和不换行,在某些情况下还是比较实用的,下面本文整理了一些相关方面的知识,并有具体的实现方法,需要的朋友可以参考下1.强制不换行,同时以省略号结尾. 代码如下:< ...

  9. 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II

     217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...

  10. 【LeetCode】228 - Summary Ranges

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