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\) ...
随机推荐
- python3 基本数据类型转换
'''基本数据类型1:虽然python中的变量不需要声明,但使用时必须赋值 1.整形变量 2.浮点型变量 3.字符型2:可以一个给多个变量赋值,也可以多个给多个变量赋值3:python3中有6个标准数 ...
- 小程序支持npm包
- Java-JVM 类加载机制
类的生命周期中的第一步,就是要被 JVM 加载进内存,类加载器就是来干这件事. 一.类加载器种类 系统提供了 3 种类加载器: 1.启动类加载器(Bootstrap ClassLoader) 由 C ...
- ReentrantLock源码探究1:非公平锁的获取和释放
1.AQS简单介绍 Sync是ReentrantLock的一个内部类,它继承了AbstractQueuedSynchronizer,即AQS,在CountDownLatch.FutureTask. ...
- Log4j rootLogger根配置以及4种日志级别
Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 把指定级别以上的日志信息输出到指定的一个或者多个位置 ...
- Vue Router实现页面跳转拦截
场景: 某些页面需要登录之后才可以访问,在页面跳转前做处理,如果要访问的页面A需要登录,则强制调到登录页,并且记录要访问的页面A的链接,在登录成功后自动跳转到页面A 1.在router下的index. ...
- nodejs本版问题
接到一个新项目,前端用的是VUE,这就需要安装nodejs.记得之前安装的是V6.X,在npm install加载依赖库node_modules的时候ant-design-vue总是报错.看到同事用n ...
- Cookie类
Cookie类:用于存储会话数据 1)构造Cookie对象 Cookie(java.lang.String name, java.lang.String value) 2)设置cookie void ...
- [ubuntu]android SDK 与Gradle环境的安装与配置|搭建android基础开发/构建环境
系统环境: linux:ubuntu18 已配置jdk 环境变量 切换到root账户 sudo su 安装Android-sdk (0)准备工作 切换到/usr/local目录: /usr/local ...
- tensorflow卷积神经网络与手写字识别
1.知识点 """ 基础知识: 1.神经网络(neural networks)的基本组成包括输入层.隐藏层.输出层.而卷积神经网络的特点在于隐藏层分为卷积层和池化层(po ...