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\) ...
随机推荐
- Chrome 神器,神奇的技巧
谷歌开发者工具是前端日常不可缺少的神奇,写布局,找 bug,优化加载速度统统靠他.但是你真的了解他么?这篇文章是专门介绍谷歌浏览器各种好用小技巧的.不是什么长篇大论,旨在提高你的开发效率,早日完工回家 ...
- PHP依赖管理工具Composer入门
作者: JeremyWei | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明 网址: http://weizhifeng.net/manage-php-dependency-wi ...
- sql把一段时间分割成周,月,季度,年的时间段
--本周 select TO_CHAR(CREATE_DATE ,'yyyy-MM-dd')as NEW_DATE , TO_CHAR(trunc(CREATE_DATE, ,'yyyy-MM-dd' ...
- Python 之 subprocess模块
一.subprocess以及常用的封装函数运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序.在Python ...
- Mac平台最好用的万能开源免费播放器-IINA
1.安装 1)官网下载地址 https://iina.io/ 2)brew 方式安装 testdeMacBook-Pro:~ test$ brew cask install iina Updating ...
- legend3---6、legend3爬坑杂记
legend3---6.legend3爬坑杂记 一.总结 一句话总结: 学东西不做项目也学不到深处,其实也就是学了没理解透, 1.lavarel中模型关联可以用的实质是? lavarel在数据库中插入 ...
- windows驱动程序
官网文档 https://docs.microsoft.com/zh-cn/windows-hardware/drivers/
- 使用Statement执行DML和DQL语句
import com.loaderman.util.JdbcUtil; import java.sql.Connection; import java.sql.DriverManager; impor ...
- 深入学习重点分析java基础---第一章:深入理解jvm(java虚拟机) 第一节 java内存模型及gc策略
身为一个java程序员如果只会使用而不知原理称其为初级java程序员,知晓原理而升中级.融会贯通则为高级 作为有一个有技术追求的人,应当利用业余时间及零碎时间了解原理 近期在看深入理解java虚拟机 ...
- SQLite 版本引发的 Python 程序调用问题
问题 在跑 OpenStack functional 功能测试的时候有两个用例过不去. nova.tests.functional.db.test_resource_provider.Resource ...