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\)的最大价值 ...
随机推荐
- Docker 编排工具Rancher 2.0
下载镜像: [root@localhost hongdada]# docker pull rancher/server:preview 运行容器: [root@localhost hongdada]# ...
- JQ插入节点方法
1.append() appendTo() prepend() prependTo() 2. before() insertBefore() after() insertAfter()
- SQLServer2014 安装错误:等待数据库引擎恢复句柄失败
查了很多资料最后靠百度百科里的一票报道彻底解决困难.在次发表一下以便给后人排忧解难 已下为百度连接 https://jingyan.baidu.com/article/7908e85cb24c19af ...
- (zhuan) Attention in Long Short-Term Memory Recurrent Neural Networks
Attention in Long Short-Term Memory Recurrent Neural Networks by Jason Brownlee on June 30, 2017 in ...
- [CodeForces - 919B] Perfect Number
题目链接:http://codeforces.com/problemset/problem/919/B AC代码: #include<cstdio> using namespace std ...
- _itemmod_gem_remove
该表可配置以一定代价移除宝石,移除后获得该宝石 `entry`宝石ID `reqId` 需求ID `chance`几率 `comond` 备注
- JaveWeb 公司项目(3)----- 通过Thrift端口获取数据库数据
前面两篇博客的内容主要是界面搭建的过程,随着界面搭建工作的完成,网页端需要加入数据,原先的B/S架构中C#通过Thrift接口获取数据,所以在网页端也沿用这个设计 首先,新建一个Maven下的Web项 ...
- 清华镜像方法更新python包
来自:Jinlong_Xu cmd环境下执行: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pk ...
- matplotlib.transforms
来自:龙哥盟飞龙 变换教程 像任何图形包一样,matplotlib建立在变换框架之上,以便在坐标系,用户数据坐标系,轴域者坐标系,图形坐标系和显示坐标系之间轻易变换.在95%的绘图中,你不需要考虑这一 ...
- String和常量池
1.Java 会确保一个字符串常量只有一个拷贝 2.用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间 ...