题目链接

给一颗树, 每个节点有初始的颜色值。 1为根节点。定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和。

每一个叶子节点是一个map, 然后从叶子节点并上去, 注意并的时候把小的map并到大的map里面。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e5+;
int head[maxn*], color[maxn], num, cnt[maxn], id[maxn];
ll anss[maxn], ans[maxn];
struct node
{
int to, nextt;
}e[maxn*];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
map <int, int> m[maxn];
void combine(int &u, int &v) {
if(m[u].size()<m[v].size())
swap(u, v);
for(auto it = m[v].begin(); it!=m[v].end(); it++) {
m[u][it->first] += it->second;
if(m[u][it->first]>cnt[u]) {
cnt[u] = m[u][it->first];
ans[u] = it->first;
} else if(m[u][it->first] == cnt[u]) {
ans[u] += it->first;
}
}
}
void dfs(int u, int fa) {
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs(v, u);
combine(id[u], id[v]);
}
anss[u] = ans[id[u]];
}
int main()
{
mem1(head);
int n, x, y;
cin>>n;
for(int i = ; i<=n; i++) {
scanf("%d", &x);
id[i] = i;
m[i][x] = ;
cnt[i] = ;
ans[i] = x;
}
for(int i = ; i<n-; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(, );
for(int i = ; i<=n; i++) {
printf("%I64d ", anss[i]);
}
return ;
}

codeforces 600E. Lomsat gelral 启发式合并的更多相关文章

  1. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

  2. Codeforces 600E Lomsat gelral (树上启发式合并)

    题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...

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

  4. Codeforces 600E. Lomsat gelral(Dsu on tree学习)

    题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...

  5. Lomsat gelral CodeForces - 600E (树上启发式合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

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

  7. Codeforces 600E Lomsat gelral(dsu on tree)

    dsu on tree板子题.这个trick保证均摊O(nlogn)的复杂度,要求资瓷O(1)将一个元素插入集合,清空集合时每个元素O(1)删除.(当然log的话就变成log^2了) 具体的,每次先遍 ...

  8. codeforces 600E Lomsat gelral

    题面:codeforces600E 学习一下$dsu \ on \ tree$.. 这个东西可以处理很多无修改子树问题,复杂度通常为$O(nlogn)$. 主要操作是:我们先把整棵树链剖一下,然后每次 ...

  9. Codeforces.600E.Lomsat gelral(dsu on tree)

    题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...

随机推荐

  1. Android Studio的配置

    第一次使用Android Studio时你应该知道的一切配置 - 生命壹号 - 博客园http://www.cnblogs.com/smyhvae/p/4390905.html

  2. java:添加一条数据到数据库中文乱码

    在数据库链接地址后面加上:characterEncoding=UTF8 如:jdbc\:mysql\://localhost\:3306/db_sjzdaj?relaxAutoCommit=true& ...

  3. 20160115--Hibernate

    package com.hanqi.dao; import static org.junit.Assert.*; import java.util.*; import org.hibernate.se ...

  4. 又一个类dapper轮子:VIC.DataAccess

    DataAccess Author: Victor.X.Qu Email: fs7744@hotmail.com DataAccess is a c# project for sql data map ...

  5. C#学习日志 day 6 ------ 常用正则表达式例举

    c#支持正则表达式匹配文本,这里讨论一下正则表达式的使用方法以及例举一些常用的正则表达式的匹配. 1.在vs中使用正则表达式 在vs中使用正则表达式,需要using System.Text.Regul ...

  6. [Python]豆瓣用户读书短评下载工具

    简介 朋友问我能不能做一个下载他在豆瓣读书上的短评的工具,于是就做了这个“豆瓣用户读书短评下载工具”. GitHub链接:https://github.com/xiaff/dbc-downloader ...

  7. Glib程序使用Valgrind查找内存泄露

    G_DEBUG=gc-friendly G_SLICE=always-malloc     //glib有缓存  故需使用 上述两条设置环境变量 G_SLICE和G_DEBUG排除由内存分配机制带来的 ...

  8. Linux 如何打开端口

    举例: 开放10000端口的解决步骤如下: 1.修改/etc/sysconfig/iptables文件,增加如下一行: -A RH-Firewall-1-INPUT -m state --state ...

  9. Ajax与C#应用详细实例

    实现刷新的方法主要是Ajax,本文档实现Ajax有两个方法(Jquery 和 W3C的JS方法):其次,使用JS也可以实现刷新数据不刷新页面(详见其他刷新页面方法JS实现):对于CallbackRef ...

  10. Umbraco模型默认属性

    Media Model的属性: umbracoFileumbracoWidthumbracoHeightumbracoBytesumbracoExtension