XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)
2 seconds
256 megabytes
standard input
standard output
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 vcontrols u.
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.
Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
5
2 5 1 4 6
1 7
1 1
3 5
3 6
1 0 1 0 0
5
9 7 8 6 5
1 1
2 1
3 1
4 1
4 3 2 1 0
In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).
题意:
给你一棵树,n个点, 以1为根,每个点有一个值,每条边有边权
我们规定假如u的子树中存在一个点v, 使得dist(u,v)<=a[v], dist(u,v)表示u 到 v的路径上的边权之和, a[v] 是v的点权
那么我们称u控制v
现在需要你算出每个点各控制了几个点
题解:
很显然被想到倍增统计路径和,然后你可以找到最远的那个被控制的点,然后该点到控制点的路径上全部加一,树上差分一下就行了
但是你A不了XJOI的,毕竟人家卡内存,所以还是去学所谓的树上二分吧,搬原题然后只改内存,这种素质我是第一次见。
代码如下:
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pii pair<int,int>
#define mp make_pair
#define int long long
using namespace std; int n,a[],fa[][],dis[][],d[],ans[];
vector<pii> g[]; void dfs(int now,int f,int dist)
{
fa[][now]=f;
dis[][now]=dist;
for(int i=;i<=;i++)
{
fa[i][now]=fa[i-][fa[i-][now]];
}
for(int i=;i<=;i++)
{
dis[i][now]=dis[i-][now]+dis[i-][fa[i-][now]];
}
for(int i=;i<g[now].size();i++)
{
if(g[now][i].first==f) continue;
dfs(g[now][i].first,now,g[now][i].second);
}
} int find(int x,int val)
{
int pos=x,nowd=;
for(int i=;i>=;i--)
{
if(dis[i][pos]+nowd<=val&&fa[i][pos])
{
nowd+=dis[i][pos];
pos=fa[i][pos];
}
}
return pos;
} void dfs2(int now,int f)
{
int pos=find(now,a[now]);
d[fa[][pos]]--;
d[fa[][now]]++;
for(int i=;i<g[now].size();i++)
{
if(g[now][i].first==f) continue;
dfs2(g[now][i].first,now);
}
} void dfs3(int now,int f)
{
ans[now]=d[now];
for(int i=;i<g[now].size();i++)
{
if(f==g[now][i].first) continue;
dfs3(g[now][i].first,now);
ans[now]+=ans[g[now][i].first];
}
} signed main()
{
scanf("%lld",&n);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
}
for(int i=;i<=n;i++)
{
int to,cost;
scanf("%lld%lld",&to,&cost);
g[to].push_back(mp(i,cost));
g[i].push_back(mp(to,cost));
}
dfs(,,);
dfs2(,);
dfs3(,);
for(int i=;i<=n;i++)
{
printf("%lld ",ans[i]);
}
}
XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)的更多相关文章
- Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...
- CodeForces 739B Alyona and a tree (二分+树上差分)
<题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...
- 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 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- 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 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
随机推荐
- 【转】java与.net比较学习系列(3) 基本数据类型和类型转换
原文地址:https://www.cnblogs.com/mcgrady/p/3397874.html 阅读目录 一,整数类型 二,浮点数类型 三,字符类型 四,布尔类型 五,类型转换之自动转换 六, ...
- VMware虚拟机如何设置从U盘启动
要给虚拟机重新安装win7系统,想使用U盘重装系统的方式,就需要让虚拟机从U盘启动,以下内容就是虚拟机从U盘启动的全操作过程. 前期准备: 1.u盘启动盘 2.VMware虚拟机 具体操作步骤: 1. ...
- Zabbix 监控页面中文乱码
问题描述: 如题,我相信大多数人都遇到过这个问题,Zabbix 监控图 中文乱码. 解决这个问题也很简单:( Zabbix 网页目录中缺少字体 ) 1.打开 Windows 的 C:\Windows\ ...
- C语言之单链表的使用
#include <stdio.h> #include <stdlib.h> typedef struct LNode{ int data; struct LNode *nex ...
- VS2008调用VS2012的WCF服务的方式和遇到的问题
1 用添加服务引用的懒方式 2 用http请求方式 3 客户端自己定义一个Contract,跟服务端的一样(可以只写要使用的方法,不用全部写完). 由于规范要求,前两种都不能用,后面根据同事的描述,产 ...
- Centos7 配置
参考文章: http://www.hksilicon.com/kb/articles/594621/CentOS-7 1. 查看时区是否正确timedatectl,若不正确则设置时区 timedate ...
- cdn path b 问题
主节点内存和磁盘最好大点,许多默认东西都放主节点了 mysql 配置文件修改后server-id = 1 1.hive 启动不起来 去配置里关掉 严格的 Hive Metastore 架构验证 hiv ...
- SSH(安全协议外壳)介绍及Linux SSH免密登录
SSH(安全外壳协议) SSH 为 Secure Shell 的缩写,是一种网络安全协议,专为远程登录会话和其他网络服务提供安全性的协议.通过使用 SSH,可以把传输的数据进行加密,有效防止远程管理过 ...
- 利用Fiddler对Android模拟器网络请求进行抓包
安装使用Fiddler 下载安装Fiddler的方法这里就略过了,一路Next就行了.装好之后运行软件,正常情况这个时候我们已经可以对电脑的网络请求进行抓包了.Fiddler默认的代理地址是127.0 ...
- Unity strip engine code 遇到執行不能之問題與解決
遊戲發布在 WebGL 平台發現檔案還是太大,因此在 IL2CPP 的環境下,開啟 Strip engine code 編譯功能,嘗試看看能不能減少一些檔案容量. 但由於我們另外有載入 Scene s ...