板子

void dfs_pre(int x, int f) {
L[x]=++*L, no[*L]=x, sz[x] = 1;
for (auto &&e:g[x]) if (e.to!=f) {
int y = e.to;
fa[y]=f, dfs_pre(y,x), sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
R[x]=*L;
}
void dfs(int x) {
for (auto &&e:g[x]) if (e.to!=fa[x]&&e.to!=son[x]) dfs(e.to);
if (son[x]) dfs(son[x]);
for (auto &&e:g[x]) if (e.to!=fa[x]&&e.to!=son[x]) {
REP(z,L[e.to],R[e.to]) add(no[z],1);
}
if (son[fa[x]]!=x) REP(z,L[x],R[x]) add(no[z],0);
}

练习1. Lomsat gelral CodeForces - 600E

大意: 给定有根树, 对于每个点$x$, 询问$x$子树内颜色最多的点的颜色, 若有同样多的输出颜色和.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n;
vector<int> g[N];
int a[N], no[N], sz[N], fa[N], son[N];
int L[N], R[N], tot[N], ma;
ll ans[N], cnt; void dfs_pre(int x, int f) {
L[x]=++*L, no[*L]=x, sz[x] = 1;
for (auto &&y:g[x]) if (y!=f) {
fa[y]=x, dfs_pre(y,x), sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
R[x]=*L;
} void add(int x) {
++tot[a[x]];
if (tot[a[x]]>ma) ma=tot[a[x]],cnt=a[x];
else if (tot[a[x]]==ma) cnt+=a[x];
}
void dfs(int x) {
for (auto &&y:g[x]) if (y!=fa[x]&&y!=son[x]) dfs(y);
if (son[x]) dfs(son[x]);
for (auto &&y:g[x]) if (y!=fa[x]&&y!=son[x]) {
REP(z,L[y],R[y]) add(no[z]);
}
add(x), ans[x] = cnt;
if (son[fa[x]]!=x) {
cnt=ma=0;
REP(z,L[x],R[x]) tot[a[no[z]]]=0;
}
} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d", a+i);
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs_pre(1,0),dfs(1);
REP(i,1,n) printf("%lld ",ans[i]);hr;
}

  

dsu on tree练习的更多相关文章

  1. CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]

    D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...

  2. CF 570D. Tree Requests [dsu on tree]

    传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...

  3. [dsu on tree]【学习笔记】

    十几天前看到zyf2000发过关于这个的题目的Blog, 今天终于去学习了一下 Codeforces原文链接 dsu on tree 简介 我也不清楚dsu是什么的英文缩写... 就像是树上的启发式合 ...

  4. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  5. dsu on tree 树上启发式合并 学习笔记

    近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...

  6. UOJ#266. 【清华集训2016】Alice和Bob又在玩游戏 博弈,DSU on Tree,Trie

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ266.html 题解 首先我们可以直接暴力 $O(n^2)$ 用 sg 函数来算答案. 对于一个树就是枚举 ...

  7. dsu on tree入门

    先瞎扯几句 说起来我跟这个算法好像还有很深的渊源呢qwq.当时在学业水平考试的考场上,题目都做完了不会做,于是开始xjb出题.突然我想到这么一个题 看起来好像很可做的样子,然而直到考试完我都只想出来一 ...

  8. 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree

    原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...

  9. [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree

    题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ...

  10. BZOJ.4182.Shopping(点分治/dsu on tree 树形依赖背包 多重背包 单调队列)

    BZOJ 题目的限制即:给定一棵树,只能任选一个连通块然后做背包,且每个点上的物品至少取一个.求花费为\(m\)时最大价值. 令\(f[i][j]\)表示在点\(i\),已用体积为\(j\)的最大价值 ...

随机推荐

  1. 【Spring Security】六、自定义认证处理的过滤器

    这里接着上一章的自定义过滤器,这里主要的是配置自定义认证处理的过滤器,并加入到FilterChain的过程.在我们自己不在xml做特殊的配置情况下,security默认的做认证处理的过滤器为Usern ...

  2. [蓝桥] 算法训练 P0505

    时间限制:1.0s 内存限制:256.0MB 一个整数n的阶乘可以写成n!,它表示从1到n这n个整数的乘积.阶乘的增长速度非常快,例如,13!就已经比较大了,已经无法存放在一个整型变量中:而35!就更 ...

  3. it做形式主语的句子

    1. it was considerate of you to visit my mother every day and (to) bring me your notes to help me wi ...

  4. Validation in jQuery

    jquery.validate.js github地址 官方主页 doc demo jquery-validation-unobtrusive github地址 demo doc

  5. Docker7之Docker overview

    Docker is an open platform for developing, shipping, and running applications. Docker enables you to ...

  6. sublime插件开发手记

    原:http://blog.hickwu.com/sublime插件开发手记   标题: sublime插件开发手记 时间: 2014-01-05 14:58:02 正文: 插件基本结构 基本插件实现 ...

  7. 1、Keepalived及VRRP原理介绍

    keepalived:即在linux中vrrp协议的实现 http://www.keepalived.org/ 什么是Keepalived?  Keepalived是一个用C语言编写的路由软件.该项目 ...

  8. 【UOJ】#273. 【清华集训2016】你的生命已如风中残烛

    题目链接:http://uoj.ac/problem/273 $${Ans=\frac{\prod _{i=1}^{m}i}{w-n+1}}$$ #include<iostream> #i ...

  9. siege 高并发测试工具

    安装siege 下载 http://download.joedog.org/siege/siege-4.0.4.tar.gz 解压 sudo tar -zvxf siege-4.0.4.tar.gz ...

  10. 如何在python中调用C语言代码

    1.使用C扩展CPython还为开发者实现了一个有趣的特性,使用Python可以轻松调用C代码 开发者有三种方法可以在自己的Python代码中来调用C编写的函数-ctypes,SWIG,Python/ ...