题目链接 Alyona and a tree

比较考验我思维的一道好题。

首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$。$t[i][j]$表示从第$i$个节点到离他第$2^{j}$近的祖先,$d[i][j]$表示从$i$开始到$t[i][j]$的路径上的路径权值总和。

在第一次DFS的同时,对节点$x$进行定位(结果为$dist(x, y)<=a(y)$)的离$x$最远的$x$的某个祖先,然后进行$O(1)$的差分。

第一次DFS完成后,做第二次DFS统计答案(统计差分后的结果)

时间复杂度$O(NlogN)$

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n)		for(int i(0); i <  (n); ++i)
#define rep(i, a, b) for(int i(a); i <= (b); ++i)
#define dec(i, a, b) for(int i(a); i >= (b); --i)
#define LL long long
#define sz(x) (int)x.size() const int N = 200000 + 10;
const int A = 30 + 1; vector <int> v[N], c[N];
LL a[N], deep[N];
LL x, y;
int n, cnt;
LL t[N][A], d[N][A];
LL g[N], value[N];
LL s[N];
LL ans[N]; void dfs(int x, int fa){ if (g[x]){
t[x][0] = g[x];
d[x][0] = value[x];
for (int i = 0; t[t[x][i]][i]; ++i){
t[x][i + 1] = t[t[x][i]][i];
d[x][i + 1] = d[t[x][i]][i] + d[x][i];
}
int now = x, noww = 0;
bool flag = false;
dec(i, 20, 0){
if (t[now][i] && d[now][i] + noww <= a[x]){
noww += d[now][i];
now = t[now][i];
flag = true;
}
}
if (flag){
--s[g[now]]; ++s[g[x]];
}
} REP(i, sz(v[x])){
int u = v[x][i];
deep[u] = deep[x] + 1;
dfs(u, x);
}
} void dfs2(int x){
ans[x] += s[x];
REP(i, sz(v[x])){
dfs2(v[x][i]);
ans[x] += ans[v[x][i]];
}
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%lld", a + i);
rep(i, 2, n){
scanf("%lld%lld", &x, &y);
g[i] = x; value[i] = y;
v[x].push_back(i), c[x].push_back(y);
} memset(s, 0, sizeof s);
cnt = 0;
deep[1] = 0;
dfs(1, 0);
memset(ans, 0, sizeof ans);
dfs2(1);
rep(i, 1, n - 1) printf("%lld ", ans[i]);
printf("%lld\n", ans[n]);
return 0; }
												

Codeforces 739B Alyona and a tree(树上路径倍增及差分)的更多相关文章

  1. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  2. CodeForces 739B Alyona and a tree (二分+树上差分)

    <题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...

  3. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  5. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  6. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  7. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  8. XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...

  9. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

随机推荐

  1. POJ3436------ACM Computer Factory

    题目链接 ACM Computer Factory Description As you know, all the computers used for ACM contests must be i ...

  2. Kubernetes master服务定制编译docker镜像

    前言 之前部署了Kubernetes 1.13.0,发现master服务的启动方式与1.10.4版本有所区别,kube-apiserver.kube-controller-manager和kube-s ...

  3. linux下编译openjdk8

    一.准备工作                                                           1.0 工作环境 Ubuntu 12.04,32位机 1.1.安装JD ...

  4. UVa 1649 Binomial coefficients 数学

    题意: \(C(n, k) = m(2 \leq m \leq 10^{15})\),给出\(m\)求所有可能的\(n\)和\(k\). 分析: 设\(minK = min(k, n - k)\),容 ...

  5. nginx 同一 iP 多域名配置方法(多文件)

    一.Nginx 配置文件(nginx version: nginx/1.12.2) 路径:/usr/local/nginx/conf/nginx.conf 操作:在 http 模块增加(子配置文件的路 ...

  6. loj2537 「PKUWC 2018」Minimax

    pkusc 快到了--做点题涨涨 rp. 初见时 yy 了一个类似于归并的东西,\(O(n^2)\),50 分. 50 分 yy 做法 对于一个点,枚举他能到达的权值(假设这个权值在左子树,在右子树是 ...

  7. Python+Selenium练习篇之7-利用name定位元素

    本文介绍如何通过节点中name的值来定位这个web元素.还是来看百度首页搜索输入框,通过name的值来定位. 相关脚本代码: # coding=utf-8 from selenium import w ...

  8. c语言入门-02-第一个c程序开始

    我们来开我们第一个c代码 #include<stdio.h> int main(){ // print num int num; num = 1; printf("%d\n&qu ...

  9. Python数据分析基础——读写CSV文件

    1.基础python代码: #!/usr/bin/env python3 # 可以使脚本在不同的操作系统之间具有可移植性 import sys # 导入python的内置sys模块,使得在命令行中向脚 ...

  10. jdk -version could not open jvm.cfg 的解决办法

    java 时出现 could not open jvm.cfg 的解决办法     问题描述: 重装JDK并更变目录后,出现JAVA -VERSION  出现could not open jvm.cf ...