Arkady and a Nobody-men CodeForces - 860E (虚树)
大意: 给定有根树, 根节点深度为$1$.
定义$r(a,b)$为$b$子树内深度不超过$a$的节点数$-1$
定义$z_a$为$a$的所有祖先的$r$之和. 对于所有点求出$z$的值.
一个点$y$对$x$的贡献显然为$dep[lca(x,y)]$, 直接计算是$O(n^2logn)$, 考虑优化.
注意到点$y$对$x$子树内所有点贡献都相同, 我们考虑深度相同的点之间的贡献, 就转化为对下面程序的优化.
REP(d,1,n) q[dep[i]].push_back(d);
REP(d,1,n) {
for (int x:q[d]) for (int y:q[d]) {
ans[x]+=dep[lca(x,y)];
}
}
显然建出虚树然后DP即可, 复杂度为$O(nlogn)$
#include <iostream>
#include <sstream>
#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'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
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;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, rt, fa[N];
vector<int> g[N], q[N], gg[N];
int sz[N], dep[N], son[N];
int L[N], R[N], top[N];
ll ans[N], c[N]; void dfs(int x, int d) {
L[x]=++*L,dep[x]=d,sz[x]=1;
for (int y:g[x]) {
dfs(y,d+1),sz[x]+=sz[y];
if (sz[y]>sz[son[x]]) son[x]=y;
}
R[x]=*L;
}
void dfs2(int x, int tf) {
top[x]=tf;
if (son[x]) dfs2(son[x],tf);
for (int y:g[x]) if (!top[y]) dfs2(y,y);
}
int lca(int x, int y) {
while (top[x]!=top[y]) {
if (dep[top[x]]<dep[top[y]]) swap(x,y);
x = fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
void dfs(int x) {
for (int y:g[x]) ans[y]+=ans[x],dfs(y);
}
int s[N], cnt;
void DP1(int x) {
sz[x]=gg[x].empty();
for (int y:gg[x]) DP1(y),sz[x]+=sz[y];
}
void DP2(int x) {
for (int y:gg[x]) c[y]=(ll)dep[x]*(sz[x]-sz[y])+c[x],DP2(y);
}
void solve(vector<int> a) {
int sz = a.size();
sort(a.begin(),a.end(),[](int a,int b){return L[a]<L[b];});
REP(i,1,sz-1) a.pb(lca(a[i],a[i-1]));
sort(a.begin(),a.end(),[](int a,int b){return L[a]<L[b];});
a.erase(unique(a.begin(),a.end()),a.end());
s[cnt=1]=a[0], sz = a.size();
REP(i,1,sz-1) {
while (cnt>=1) {
if (L[s[cnt]]<=L[a[i]]&&L[a[i]]<=R[s[cnt]]) {
gg[s[cnt]].pb(a[i]);
break;
}
--cnt;
}
s[++cnt]=a[i];
}
DP1(s[1]),c[s[1]]=0,DP2(s[1]);
for (int x:a) gg[x].clear();
} int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d",fa+i),g[fa[i]].pb(i);
rt = min_element(fa+1,fa+1+n)-fa;
dfs(rt,1),dfs2(rt,rt);
REP(i,1,n) q[dep[i]].pb(i);
REP(d,1,n) if (q[d].size()) {
solve(q[d]);
for (int x:q[d]) ans[x]=c[x]+dep[x];
}
dfs(rt);
REP(i,1,n) printf("%lld ", ans[i]-dep[i]);hr;
}
Arkady and a Nobody-men CodeForces - 860E (虚树)的更多相关文章
- [Codeforces]860E Arkady and a Nobody-men
屯一个虚树的板子,顺便总结一下这样的题型. Description 给定一棵n个节点的有根树,在输入数据通过给出每个节点的父亲来表示这棵树.若某个节点的父亲为0,那么该节点即为根.现在对于每个点,询问 ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)
https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...
- Codeforces Round #613 Div.1 D.Kingdom and its Cities 贪心+虚树
题目链接:http://codeforces.com/contest/613/problem/D 题意概述: 给出一棵树,每次询问一些点,计算最少删除几个点可以让询问的点两两不连通,无解输出-1.保证 ...
- CodeForces - 592D: Super M(虚树+树的直径)
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...
- [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)
[Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...
- 【Codeforces 809E】Surprise me!(莫比乌斯反演 & 虚树)
Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 的权值为 \(a_i\).求: \[\frac{1}{n(n-1)}\sum_{i=1}^n\sum_{j=1}^n\var ...
- Codeforces 1606F - Tree Queries(虚树+树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 显然我们选择删除的点连同 \(u\) 会形成一个连通块,否则我们如果选择不删除不与 \(u\) 在同一连通块中的点,答案一定更优. 注意到 ...
- Codeforces 809E - Surprise me!(虚树+莫比乌斯反演)
Codeforces 题目传送门 & 洛谷题目传送门 1A,就 nm 爽( 首先此题一个很棘手的地方在于贡献的计算式中涉及 \(\varphi(a_ia_j)\),而这东西与 \(i,j\) ...
随机推荐
- Java - 可循环队列
队列是一种特殊的线性表,是一种先进先出的数据结构.只允许在表的前端进行删除操作,在表的后端进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 简单的循环队 ...
- SRS之SrsRtmpServer::connect_app详解
1. connect('live') 2. SrsRtmpServer::connect_app 位于 srs_rtmp_stack.cpp.在 SRS 的 RTMP 连接处理线程 conn 中,当与 ...
- State Threads之网络架构库
原文: State Threads for Internet Applications 介绍 State Threads is an application library which provide ...
- 黑马在线教育项目---15-16、datatables插件
黑马在线教育项目---15-16.datatables插件 一.总结 一句话总结: datatables插件也比较好用,引入好插件的js和css后,核心代码也就是插件的初始化,如果要修改配置可以百度 ...
- GitHub-Microsoft:DotNet2
ylbtech-GitHub-Microsoft:DotNet2 1.返回顶部 · SignService Code Signing service and Client for Authentico ...
- Java界面程序实现图片的放大缩小
Java界面程序实现图片的放大缩小.这个程序简单地实现了图片的打开.保存.放大一倍.缩小一倍和固定缩放尺寸,但是并没有过多的涵盖对图片的细节处理,只是简单地实现了图片大小的放缩. 思维导图如下: 效果 ...
- Java计算器的图形界面应用程序
JAVA计算器的图形界面应用程序 题目简介: 整体分析: 实验代码: /*部分使用插件做界面*/ import java.awt.EventQueue; import javax.swing.JB ...
- 通过TCODE查找SPRO路径
1.SE11:CUS_ACTOBJ,根据OBJECTNAME(对象名称),即视图名称,获取Customizing activity(ACT_ID) 2.根据ACT_ID在表CUS_IMGACT获取说明 ...
- 在Linux中使用minikube
Minikebe Minikube是一个轻量级Kubernetes实现,它在本地机器上创建一个VM,并部署一个只包含一个节点的简单集群. Minikube使用Docker机器来管理Kubernetes ...
- docker镜像的仓库
一.docker镜像的仓库 --- repos-registry的创建: 仓库分为公共仓库和私有仓库 DockerHub的官方仓库 https://hub.docker.com DockerPool社 ...