题目链接 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. Win7系统桌面便签怎么添加?

    参考:http://jingyan.baidu.com/article/ab69b270c207432ca7189f99.html Win7系统桌面便签怎么添加?有时候工作.学习忙起来就会忘记要办的事 ...

  2. linux系统下单节点hadoop2的配置

    Jdk安装: jdk-7u45-linux-x64.gz cp jdk-7u45-linux-x64.gz /usr/java/ cd /usr/java/ tar -zxvf jdk-7u45-li ...

  3. 2190: [SDOI2008]仪仗队(欧拉函数)

    2190: [SDOI2008]仪仗队 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3235  Solved: 2089 Description 作 ...

  4. vrpie在Visio Studio 中无法调试的问题

    最近这这几天一直在研究vrpie,之前不能调试,一调试就出问题,打开那个生成的htm文件是没问题的,最初的解决方法是不通过调试来打可那个htm页面,但是这样比较麻烦,因为经常需要和服务器交互,就只能用 ...

  5. loj2061 「HAOI2016」放棋子

    答案就是错排数 n = int(input()) f = [0] * 205 f[0] = 1 for i in range(2, n+1): f[i] = (i-1) * (f[i-1] + f[i ...

  6. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  7. leetcode 【 Trapping Rain Water 】python 实现

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  8. Python基础-week06 面向对象编程进阶

    一.反射 1.定义:指的是通过字符串来操作类或者对象的属性 2.为什么用反射? 减少冗余代码,提升代码质量. 3.如何用反射? class People: country='China' def __ ...

  9. Leetcode 498.对角线遍历

    对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. 示例: 输入: [ [ 1, 2, 3 ], [ 4, 5, ...

  10. sqlserver2008透明书库加密

    /*Title:TDE加密Author:浪客Environment:Windows Server 2008 Enterprise + SQL Server 2008 EnterpriseDescrip ...