dsu on tree练习
板子
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练习的更多相关文章
- 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,求每个子树中最长的边,满 ...
- CF 570D. Tree Requests [dsu on tree]
传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...
- [dsu on tree]【学习笔记】
十几天前看到zyf2000发过关于这个的题目的Blog, 今天终于去学习了一下 Codeforces原文链接 dsu on tree 简介 我也不清楚dsu是什么的英文缩写... 就像是树上的启发式合 ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- dsu on tree 树上启发式合并 学习笔记
近几天跟着dreagonm大佬学习了\(dsu\ on\ tree\),来总结一下: \(dsu\ on\ tree\),也就是树上启发式合并,是用来处理一类离线的树上询问问题(比如子树内的颜色种数) ...
- UOJ#266. 【清华集训2016】Alice和Bob又在玩游戏 博弈,DSU on Tree,Trie
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ266.html 题解 首先我们可以直接暴力 $O(n^2)$ 用 sg 函数来算答案. 对于一个树就是枚举 ...
- dsu on tree入门
先瞎扯几句 说起来我跟这个算法好像还有很深的渊源呢qwq.当时在学业水平考试的考场上,题目都做完了不会做,于是开始xjb出题.突然我想到这么一个题 看起来好像很可做的样子,然而直到考试完我都只想出来一 ...
- 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree
原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...
- [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree
题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ...
- BZOJ.4182.Shopping(点分治/dsu on tree 树形依赖背包 多重背包 单调队列)
BZOJ 题目的限制即:给定一棵树,只能任选一个连通块然后做背包,且每个点上的物品至少取一个.求花费为\(m\)时最大价值. 令\(f[i][j]\)表示在点\(i\),已用体积为\(j\)的最大价值 ...
随机推荐
- 如何 使用vim的 session和viminfo 恢复上一次工作的环境??
使用vim的 session和viminfo 恢复上一次工作的环境, 主要有两个方面的内容需要保存: 要使用session,保存窗口和视图, 及全局设置 要使用viminfo保存 命令行历史, 搜索历 ...
- Mysql的唯一性索引unique
目录 唯一性索引unique影响: unique与primary key的区别: 存在唯一键冲突时,避免策略: insert ignore: replace into: insert on dupli ...
- RHEL7防火墙策略设置
注意查看firewall防火墙状态,并设置. 打开防火墙且没有放行端口的情况下rhel7这台机器是ping不通的. 放行端口需要永久放行,加--permernant,否则重启后失效,仍然无法访问该端口 ...
- Mysql优化知识点总结(转自CS-Notes)
转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/MySQL.md 一.索引 B+ Tree 原理 MySQL 索引 索引优化 索引 ...
- KNN——图像分类
内容参考自:https://zhuanlan.zhihu.com/p/20894041?refer=intelligentunit 用像素点的rgb值来判断图片的分类准确率并不高,但是作为一个练习kn ...
- vs里32位项目和64位项目的区别
由于操作系统内存分配的不同,导致软件开发过程中,需要编译不同版本的软件. 1.编译程序根据需要选择不同的编译环境. x86和win32为32位程序,x64为64位程序,可以选择不同的编译条件形成不同位 ...
- 【Ruby】【遇到的问题】
1 Error fetching https://gems.ruby-china.org/: certificate verify failed (https://gems.ruby-china.or ...
- Codeforces 776E The Holmes Children
题目链接:http://codeforces.com/problemset/problem/776/E ${\because gcd(i,n-i)=1\Leftrightarrow gcd(i,n)= ...
- codeforces gym 100971 K Palindromization 思路
题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...
- sklearn中的train_test_split (随机划分训练集和测试集)
官方文档:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html ...