一、题目回顾

题目链接:Alyona and a tree

Examples
Input
5
2 5 1 4 6
1 7
1 1
3 5
3 6
Output
1 0 1 0 0
 
Input
5
9 7 8 6 5
1 1
2 1
3 1
4 1
Output
4 3 2 1 0
 
Note

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 1 controls the vertex 5).

题意:对于两个点,如果dis(u,v)<=val(v)且v是u的子孙,则称u掌控v,现在求每个结点掌控点的个数。

二、解题思路

  • dfs+二分

dis[x]表示根节点到x节点上的边权和,则x这个节点是被u节点控制的,当且仅当 dis[x]-dis[u]<=a[x],即dis[x]-a[x] <= dis[u]; 而dis这个数组如果在dfs序上,肯定是单调递增的。所以维护一下dfs序、维护一下dis数组。 在处理x的出度的时候找出dis数组中第一个满足dis[x]-a[x]<=dis[u] 的u节点, 则u--->x的路径上的所有点都能够控制x节点,而u节点以上的节点都不能控制x节点。设u节点的dfs序的上一个节点为y,则让ans[y]-,然后在dfs的时候累加答案即可。设当前节点为x,出度节点为y,则ans[x]+=ans[y],当遇到那些ans被减过的节点(即执行过ans[x]–的节点x), 则在算的时候就会把那个不属于它的节点给扣掉。而以上的节点相应的也会受“ans[x]–”的也不会算那些不属于它们的节点了。一开始让ans[x]都等于1,最后再减去1即可。

三、代码

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define all(x) x.begin(),x.end() typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 2e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n;
LL a[MAXN];
LL ans[MAXN],dis[MAXN];
vector <LL> w[MAXN];
vector <int> G[MAXN];
vector < pair<LL,int> > temp; void dfs(int x,int fa)
{
ans[x] = 1;
LL t = dis[x]-a[x];
int pos = lower_bound(all(temp),mp(t,0))-temp.begin();
pos--;
if (pos >= 0)
ans[temp[pos].se]--;
temp.pb(mp(dis[x],x));
int len = G[x].size();
rep1(i,0,len-1)
{
int y = G[x][i];
if (y==fa) continue;
dis[y] = dis[x] + w[x][i];
dfs(y,x);
ans[x] += ans[y];
}
temp.pop_back();
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rel(a[i]);
rep1(i,2,n)
{
int fa;LL cost;
rei(fa);rel(cost);
G[fa].pb(i);
w[fa].pb(cost);
G[i].pb(fa);
w[i].pb(cost);
}
dfs(1,-1);
rep1(i,1,n)
{
printf("%I64d",ans[i]-1);
if (i==n)
puts("");
else
putchar(' ');
}
return 0;
}

DFS——CodeForces740DAlyona and a tree的更多相关文章

  1. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  2. (二叉树 递归 DFS) leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  3. BFS广度优先 vs DFS深度优先 for Binary Tree

    https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/ What are BFS and DFS for Binary Tree? A Tree i ...

  4. Path of Equal Weight (DFS)

    Path of Equal Weight (DFS)   Given a non-empty tree with root R, and with weight Wi assigned to each ...

  5. 蒟蒻浅谈树链剖分之一——两个dfs操作

    树链剖分,顾名思义就是将树形的结构剖分成链,我们以此便于在链上操作 首先我们需要明白在树链剖分中的一些概念 重儿子:某节点所有儿子中子树最多的儿子 重链:有重儿子构成的链 dfs序:按重儿子优先遍历时 ...

  6. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  7. [LeetCode] questions conclustion_BFS, DFS

    BFS, DFS 的题目总结. Directed graph: Directed Graph Loop detection and if not have, path to print all pat ...

  8. UVA - 12424 Answering Queries on a Tree(十棵线段树的树链剖分)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N and have colors C1, C2,. ...

  9. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

随机推荐

  1. 使用classList来实现两个按钮样式的切换

    classList属性的方法:add();remove();toggle(); 描述,在一些页面我们需要使用两个按钮来回切换,如图: 我们要使用到add()和remove()方法 html部分: &l ...

  2. 用JS实现一个时钟的效果

    (效果图) 分两步进行的. 第一步:  要得到现在的 时 分 秒 但是这里面有一个小玄机 . 比如现在是 9点整      时针指向 9 是没错的 但是如果现在是 9点半   时针应该指向的是 9到1 ...

  3. Spring知识点总结(五)Spring整合JDBC

     1. 回顾JDBC        a. java操作关系型数据的API.导入相关数据库的驱动包后可以通过JDBC提供的接口来操作数据库.        b. 实现JDBC的六个步骤          ...

  4. 零基础Python知识点回顾(一)

    如果你是小白,建议只要安装官网的python-3.7.0-amd64.exe 然后在电脑cmd命令提示符  输入检查是否已经安装pip,一般安装了python都会有的. >pip         ...

  5. Oracle PL/SQL中异常高级特性

    在OraclePL/SQL语句块中exception的异常处理部分是非常重要的组成部分,它决定了在PL/SQL语句块内部可执行部分在发生异常错误时,程序是友好地提示:程序遇到某些错误而无法执行,还是抛 ...

  6. 统计iOS产品不同渠道的下载量

    如何统计iOS产品不同渠道的下载量? 一.前言 在开发过程中,Android可能会打出来很多的包,用于标识不同的商店下载量.原来觉得苹果只有一个商店:AppStore,如何做出不同来源的统计呢?本篇文 ...

  7. java中的==、equals()源码分析

    浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...

  8. 如何在linux中创建虚拟环境

    安装虚拟环境的命令 : sudo pip install virtualenv sudo pip install virtualenvwrapper 安装完虚拟环境后,如果提示找不到mkvirtual ...

  9. PHP学习day1

    PHP 变量规则: 变量以 $ 符号开头,其后是变量的名称 变量名称必须以字母或下划线开头 变量名称不能以数字开头 变量名称只能包含字母数字字符和下划线(A-z.0-9 以及 _) 变量名称对大小写敏 ...

  10. Apache Maven(三):POM

    什么是 POM? POM (Project Object Model) 项目对象模型.它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置细节的信息.它包含大多数项目的默认值.例如,构建 ...