E. Lomsat gelral

http://codeforces.com/contest/600/problem/E

题意:

  求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号求和)。

分析:

  dsu on tree。

  这个可以解决一系列不带修改的子树查询问题。

  考虑暴力的思路:就是枚举每个子树,计算每个颜色出现的个数。统计答案。

  dsu on tree:最后一个子树枚举计算完了,它的贡献可以保留,然后用其它的子树去合并。(最后一棵子树是最大的)。现在的复杂度就是nlogn了。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int head[N], nxt[N << ], to[N << ], En;
int fa[N], siz[N], son[N], cnt[N], col[N], Mx;
LL ans[N], Sum; void add_edge(int u,int v) {
++En; to[En] = v; nxt[En] = head[u]; head[u] = En;
++En; to[En] = u; nxt[En] = head[v]; head[v] = En;
} void dfs(int u) {
siz[u] = ;
for (int i=head[u]; i; i=nxt[i]) {
int v = to[i];
if (v == fa[u]) continue;
fa[v] = u;
dfs(v);
siz[u] += siz[v];
if (!son[u] || siz[son[u]] < siz[v]) son[u] = v;
}
} void add(int u) {
cnt[col[u]] ++;
if (cnt[col[u]] > Mx) Mx = cnt[col[u]], Sum = col[u];
else if (cnt[col[u]] == Mx) Sum += col[u];
}
void Calc(int u) {
add(u);
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u]) Calc(to[i]);
}
void Clear(int u) {
cnt[col[u]] --;
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u]) Clear(to[i]);
} void solve(int u,bool c) {
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u] && to[i] != son[u]) solve(to[i], );
if (son[u]) solve(son[u], );
add(u);
for (int i=head[u]; i; i=nxt[i])
if (to[i] != fa[u] && to[i] != son[u]) Calc(to[i]);
ans[u] = Sum;
if (!c) Clear(u), Mx = , Sum = ;
} int main() {
int n = read();
for (int i=; i<=n; ++i) col[i] = read();
for (int i=; i<n; ++i) {
int u = read(), v = read();
add_edge(u, v);
}
dfs();
solve(, );
for (int i=; i<=n; ++i) printf("%I64d ",ans[i]);
return ;
}

CF 600 E. Lomsat gelral的更多相关文章

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

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

  2. Codeforces 600 E - Lomsat gelral

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

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

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

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

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

  5. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  6. 「CF 600E」 Lomsat gelral

    题目链接 戳我 \(Describe\) 给出一棵树,每个节点有一个颜色,求每个节点的子树中颜色数目最多的颜色的和. \(Solution\) 这道题为什么好多人都写的是启发式合并,表示我不会啊. 这 ...

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

  8. 【CF600E】Lomsat gelral(dsu on tree)

    [CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...

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

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

随机推荐

  1. Windows彻底卸载系统自带的office

    由于自带office导致按照新的office会提示要先卸载原来32位的office,又在控制面板或软件管理工具中找不到office,用如下方法删除 1.在C盘删除office文件夹 2.删除注册表 1 ...

  2. Linux学习总结(八)-磁盘格式化,挂载,swap扩容

    上次我们学习了磁盘分区,磁盘分区完后还不能直接使用,需要对分区进行格式化,载入某个类型的文件系统,然后挂载到相应目录下才可使用. 一 磁盘格式化 格式化命令: mke2fs -t [ext2 ext3 ...

  3. MBTiles 离线地图演示 - 基于 Google Maps JavaScript API v3 + SQLite

    MBTiles 是一种地图瓦片存储的数据规范,它使用SQLite数据库,可大大提高海量地图瓦片的读取速度,比通过瓦片文件方式的读取要快很多,适用于Android.IPhone等智能手机的离线地图存储. ...

  4. NSLog的各种打印格式符 和 打印CGRect时用NSStringFromCGRect

    打印CGRect时用NSStringFromCGRect 转载自:http://blog.csdn.net/chenyong05314/article/details/8219270 1. 打印CG开 ...

  5. UI到底应该用xib/storyboard完成,还是用手写代码来完成?

    UI到底应该用xib/storyboard完成,还是用手写代码来完成? 文章来源:http://blog.csdn.net/libaineu2004/article/details/45488665 ...

  6. Jquery Mobile通过超链接跳转后CSS样式不起作用的解决办法

    Jquery Mobile中的超链接默认是采用AJAX跳转的,ajax获取到页面的内容之后,就直接替换当前页面的内容了,它只是单纯的获取页面的HTML代码,并不会再去下载引用的CSS代码和JS代码,因 ...

  7. maven 环境变量 设置

    Maven安装与配置   一.需要准备的东西 1. JDK 2. Eclipse 3. Maven程序包 二.下载与安装 1. 前往https://maven.apache.org/download. ...

  8. JAVA交通规则

    第一个JAVA程序的编写和运行 1.使用记事本编辑: public class Welcome { public static void main(String[] args) { System.ou ...

  9. BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5246  Solved: 1978[Submit][Status][Discuss] Descript ...

  10. hdu_4465_Candy

    LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large cand ...