Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- iOS--基础控件总结一
UIWindow窗口 UIView视图 UIButten按钮 UILabel文本显示 UITextField输入框 UI TextView多行输入框 UISwitch开关 UISegmentedCon ...
- 快速上手RaphaelJS-Instant RaphaelJS Starter翻译(一)
(目前发现一些文章被盗用的情况,我们将在每篇文章前面添加原文地址,本文源地址:http://www.cnblogs.com/idealer3d/p/Instant_RaphaelJS_Start ...
- ubuntu安装rpm包
ubuntu下的rpm包的安装方法 一般是不能够直接安装的,我们需要一个工具叫alien,先install它吧.然后按照下面择所需. 1.直接安装: alien -i -c filename.rpm ...
- Lua小技巧
来公司以后,业务逻辑都用lua写.写了好长时间了,到最近才觉得有点掌握了Lua的灵活.最近用Lua写了个类似集合一样的东西,如果两次向集合里放入同一个元素,就会报错,方便检查配置.代码如下: -- k ...
- C++学习之路——1.linux下环境搭建
学习之路,可说各有各的看法和方法.对于我来说,完全是兴趣一下子来了,脑壳发热吧.就想有个干净的环境,只想着与程序有关的东西练一练. 目前想学习C++,可是打开VC++6,再安了VS2010.VS201 ...
- STM32中的PWM的频率和占空比的设置
转于http://blog.csdn.net/liming0931/article/details/8491468 下面的这个是stm32的定时器逻辑图,上来有助于理解: TIM3的ARR寄存器和 ...
- SYSLOG审记日志的配置。
前言 syslog是UNIX系统中提供的一种日志记录方法(RFC3164),syslog本身是一个服务器,程序中凡是使用syslog记录的信息都会发送到该服务器,服务器根据配置决定此信息是否记录,是记 ...
- 数据解析(XML和JSON数据结构)
一 解析 二 XML数据结构 三 JSON 数据结构 一 解析 1 定义: 从事先规定好的格式中提取数据 解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照 ...
- aes rsa加密
aes在加密时,若加密字符串的长度不是16,则会在后面加0x00补足16位,所以在解密后还应该去除0x00 小于16字节的原文会得到16字节长度的密文,小于32字节的原文会得到32字节长度的密文,大于 ...
- 学习 Local Sensitive Hash
1. 最近邻法的应用 1.1 Jaccard 相似集 如何定义相似:即相关属性交集的大小,越大则越相似.我们给相似一个数学上的定义:Jaccard 相似集. 集合 \(S\) 与集合 \(T\) 的 ...