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\) ...
随机推荐
- Node.js自学完全总结
零.什么是Node.js? 引用Node.js官方网站的解释如下: Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript e ...
- Java——重写hashCode()和euqals()方法
1.顺序表的问题 查找和去重效率较低 对于这样的顺序表来说,如果需要查找元素,就需要从第一个元素逐个检查,进行查找.对于需要去重的存储来说,每次存入一个元素之前,就得将列表中的每个元素都比对一遍,效率 ...
- 浏览器是如何处理页面元素的Download?
首先,浏览器对于script的下载是避免并行进行的.HTTP/1.1协议中规定浏览器和同一host之间只建立最多两个连接,也就是说允许的最 大并行度为2(当然,对IE和Firefox来说,你都可以通过 ...
- mongodb aggregate 聚合 操作(扁平化flatten)
mongodb自带的函数非常多,最近用mongo做持久化数据库,遇到一个需求:子文档是个数组,把数组里的各个字段扁平化合到根文档中,查过资料后(主要是mongodb的文档和stackoverflow) ...
- 解释HTTP中Get和Post。它们有什么区别,哪个使用时更加安全?
Get和Post都是浏览器向网页服务器提交数据的方法. Get把要提交的数据编码在url中,比如/workinfo.jsp/mianshiti?key1=value1&key2=value2中 ...
- RN性能优化(重新探索react吧)
最近做RN遇到了一些性能瓶颈,逼着自己不得不做一些优化 已经做过,或者尝试过得优化方案: 1.点击效果防止重复点击. 2.左右两边分别用两个异步栈进行更新,这样能让右边的缓慢不影响左边的更新. 3.I ...
- GitHub:Alibaba
ylbtech-GitHub:Alibaba 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://github.com/alibaba 2. ...
- 【React自制全家桶】三、React使用ref操作DOM与setState遇到的问题
在React中同时使用ref操作DOM与setState常常会遇到 比如操作的DOM是setState更新之前的DOM内容,与想要的操作不一致.导致这样的原因是setState函数是异步函数. 就是当 ...
- 利用CountDownTimer倒计时的简单使用实现
package com.loaderman.countdowntimerdemo; import android.os.Bundle; import android.os.CountDownTimer ...
- 批量替换word内容
有一个需求需要把word中的一段文档的编号批量替换 如,把133-183批量的替换成31-81,需要批量的替换word,脚本如下 from docx import Document import os ...