D. Alyona and a tree

Problem Description:

Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v controls u.

Input:

The first line contains single integer n (1 ≤ n ≤ 2·105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).

It is guaranteed that the given graph is a tree.

Output:

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Sample Input:

5

2 5 1 4 6

1 7

1 1

3 5

3 6

Sample Output:

1 0 1 0 0

这题是后补的,花了2 3天,最后问了黄菊苣,又看了前几的代码,才有点懂了 但也不错嘛,感受到了D题的难度

【题目链接】D. Alyona and a tree

【题目类型】图论+二分+dfs

&题意:

一颗树1~n,以1为节点,每个节点i都有一个对应的a[i]值,问每个节点可以控制多少个子节点 并输出

控制的定义:dis(v,u)<=a[u]

dis(v,u)的定义:从v到u的边上的权值之和

&题解:

首先要存图,下面是前几的大神代码,他用的是g存的图。

存完图,dfs扫一遍,depth[]存的是权值前缀和,前缀和存在单调性,这时就可以想到用二分了。(构造前缀和,以便用二分)

--1---

-2-3--

--4-5-

上面是样例的树,假如我dfs到了第4个节点,那么我就二分4 3 1这个路径的前缀和,找到能控制4的最大区间,更新这个区间,之后继续dfs第5个节点,这时修改前缀和的数组,也就是path数组,把4 pop_back掉,把5 push进去,这就可以二分5 3 1这条路了。

主算法就这样,今天比较晚了,明天写一下试试。

【时间复杂度】O(\(nlogn\))

&代码:

#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
typedef long long ll;
typedef pair<int, int> pii;
#define fast_io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define freopen(x) freopen(x".in", "r", stdin), freopen (x".out", "w", stdout);
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
const int N = (int) 1e6;
vector<pair<ll, int> > path;
vector<vector<pair<int, ll> > > g(N);
ll ans[N], a[N], depth[N]; void dfs(int v, int p = -1) {
ans[v] = 1;
int idx = lower_bound(all(path), mp(depth[v] - a[v], -1)) - path.begin();
--idx;
if (idx >= 0) ans[path[idx].se]--;
path.pb(mp(depth[v], v));
for (auto x : g[v]) {
int to = x.fi;
if (to == p) continue;
ll len = x.se;
depth[to] = depth[v] + len;
dfs(to, v);
ans[v] += ans[to];
}
path.pop_back();
}
int main() {
fast_io;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i < n; i++) {
int p;
ll w;
cin >> p >> w;
g[i + 1].pb(mp(p, w));
g[p].pb(mp(i + 1, w));
}
dfs(1);
for (int i = 1; i <= n; i++)
cout << ans[i] - 1 << " ";
return 0;
}

Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)的更多相关文章

  1. 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 ...

  2. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  3. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

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

  4. Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题

    C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...

  5. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #358 (Div. 2) C. Alyona and the Tree

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

  9. Codeforces Round #381 (Div. 2)C. Alyona and mex(思维)

    C. Alyona and mex Problem Description: Alyona's mother wants to present an array of n non-negative i ...

随机推荐

  1. iOS 企业包碰到的问题

    在这里 就不讲 iOS 企业包 怎么申请了 网上链接很多 也简单  真找不到靠谱的, 就用这个链接 教程 http://www.cnblogs.com/xiaoc1314/p/5595312.html ...

  2. Python常用函数、方法、模块记录

    常用函数: 1.pow():乘方 2.abs():绝对值 3.round():四舍五入 4.int():转换为整数 5.input():键盘输入(会根据用户的输入来做类型的转换) raw_input( ...

  3. 【JS】FOR循环通关只循环一次length提高性能

    问题来源于jqueryAPI 原文: Iteration An array has a length property that is useful for iteration: for ( var ...

  4. downsampling and upsampling【转】

    orig: http://www.eetimes.com/document.asp?doc_id=1275556 downsampling The process of reducing a samp ...

  5. 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法

    问题描述:在scott用户下创建视图的时候,报错:权限不足.(其他用户以此类推)解决方法: 以dba用户登录 sqlplus / as sysdba 赋予scott用户创建VIEW的权限 grant  ...

  6. Swing图形用户界面

    package test; import java.awt.event.ActionEvent;import java.awt.event.ActionListener; import javax.s ...

  7. svn:cleanup failed previous operation has not finished; run cleanup if it was interrupted

    svn:cleanup failed previous operation has not finished; run cleanup if it was interrupted 今天 大脑一时短路 ...

  8. 第二章:UNIX标准化及实现

    本章节介绍个UNIX编程环境的标准化的进展,对ISO C,POSIX和Single UNIX Specification三个主要标准进行了说明 本章后面部分介绍了限制的具体实例. 我学习本章的心得是: ...

  9. C++ 中静态成员函数访问非静态成员变量的方法

    最近在 VS2010 里开发出厂烧写工具,遇到一个问题: 我创建了一个线程,在这个线程里要访问非静态成员,而这个线程函数是静态的.最后找到的办法是用对象指针来做. sourcecode: #test. ...

  10. MongoDB学习笔记九:分片

    分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程.有事也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机既可以存储更多的数据 ...