题目链接: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. 使用 github.io 免费建站

    /*************************************************************************** * 使用 github.io 免费建站 * 说 ...

  2. 点分十进制IP校验、转换,掩码校验

    /***************************************************************************** * 点分十进制IP校验.转换,掩码校验 * ...

  3. 【C#学习笔记】从粘贴板复制文本

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 【转】让Souce Insight支持多种语言的语法高亮:Python,Ruby,ARM汇编,windows脚本文件(bat/batch),PPC,SQL,TCL,Delphi等

    原文网址:http://www.crifan.com/source_insight_support_highlight_for_python_ruby_arm_batch_ppc_sql_tcl_de ...

  5. Android Traceroute 功能实现

    经常在windows下开发网络功能的人 经常会使用的命令就是tracert .而实际上 在app开发中,我们也经常要碰到类似的情况.比如你的app 出现了问题,你总不能让用户想办法 去tracert吧 ...

  6. NodeJS学习笔记之MongoDB模块

    其中还有,nodejs远程连接mysql数据库 一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于 ...

  7. Java 循环语句之多重循环

    循环体中包含循环语句的结构称为多重循环.三种循环语句可以自身嵌套,也可以相互嵌套,最常见的就是二重循环.在二重循环中,外层循环每执行一次,内层循环要执行一圈. 如下所示: 例如:使用 * 打印长方形: ...

  8. linux apache 配置fastcgi

    Redhat 上 FastCGI 安装与配置 软件包 相关软件包: httpd 2.2.14      //注意版本 这个版本不会出问题   注:apache httpd安装 fcgi-2.4.0.t ...

  9. min_free_kbytes

    http://kernel.taobao.org/index.php?title=Kernel_Documents/mm_sysctl min_free_kbytes 先看官方解释:This is u ...

  10. 1、android源代码下载与跟踪

     学习Android源代码的目的 理解Android API查找API(Activity.Content Provider等) 高级应用开发(ROM定制)  在不同平台下载Android源代码 W ...