D. Broken BST
memory limit per test

256 megabytes

input

standard input

output

standard output

Let T be arbitrary binary tree — tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent — it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree T:

  1. Set pointer to the root of a tree.
  2. Return success if the value in the current vertex is equal to the number you are looking for
  3. Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for
  4. Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for
  5. Return fail if you try to go to the vertex that doesn't exist

Here is the pseudo-code of the described algorithm:

bool find(TreeNode t, int x) {
if (t == null)
return false;
if (t.value == x)
return true;
if (x < t.value)
return find(t.left, x);
else
return find(t.right, x);
}
find(root, x);

The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree.

Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree.

If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately.

Input

First line contains integer number n (1 ≤ n ≤ 105) — number of vertices in the tree.

Each of the next n lines contains 3 numbers vlr (0 ≤ v ≤ 109) — value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number  - 1 is set instead. Note that different vertices of the tree may contain the same values.

Output

Print number of times when search algorithm will fail.

Examples
input
3
15 -1 -1
10 1 3
5 -1 -1
output
2
input
8
6 2 3
3 4 5
12 6 7
1 -1 8
4 -1 -1
5 -1 -1
14 -1 -1
2 -1 -1
output
1
Note

In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.

题意:给出一颗二叉搜索树,不保证这棵树是正确的二叉搜索树,

那么按照二叉搜索树的搜索法则(小则往左,大则往右),就有可能找不到树上的某些节点

问找不到的节点有多少个

二叉搜索树,若根节点权值为10,那么左子树的权值范围为[-inf,9],右子树范围为[11,inf]

也就是说每个点的子节点可能的区间范围是确定的

若10有一个权值为a的左孩子,那么只有a的权值在[-inf,9]范围内,才能找到a

否则就找不到

右孩子同理

所以我们可以dfs统计满足要求的点的个数,

即若根节点权值为k,若左孩子权值在[l,k-1]之间,左孩子可以找到

若右孩子的权值在[k+1,r]之间,右孩子可以找到

最后 点的总数 减去 能找到的点的个数

注意:

1、累加可以找到的点时,累加这个权值的出现次数,

因为所有权值为这个的点,可能位于树的不同位置,但只要找到一个,所有权值为他的都可以找这一个

2、dfs(l[now],lk,min(v[now]-1,rk));    dfs(r[now],max(v[now]+1,lk),rk);

即注意dfs左子树时,区间右端点取大,dfs右子树时,区间左端点取小

为什么?因为点二叉搜索树是不正确的,到达位置错误的点时,要按错误的点来

为什么不直接累加不合法的?

累加合法的可以不用累加后清0,

累加不合法的可能是一个位置的不合法,但另一个位置的合法,

对于同一个权值,只要有一个位置合法,就能找到

#include<map>
#include<cstdio>
#include<algorithm>
#define N 100001
using namespace std;
int n,root,ans;
int v[N],l[N],r[N];
bool h[N];
map<int,int>sum;
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>'') { if(c=='-') f=-;c=getchar(); }
while(c>=''&&c<='') { x=x*+c-''; c=getchar(); }
return x;
}
void dfs(int now,int lk,int rk)
{
if(v[now]>=lk&&v[now]<=rk) ans+=sum[v[now]];
if(l[now]>) dfs(l[now],lk,min(v[now]-,rk));
if(r[now]>) dfs(r[now],max(v[now]+,lk),rk);
}
int main()
{
n=read();
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&v[i],&l[i],&r[i]);
sum[v[i]]++;
if(l[i]!=-) h[l[i]]=true;
if(r[i]!=-) h[r[i]]=true;
}
for(int i=;i<=n;i++)
if(!h[i])
{
root=i; break;
}
dfs(root,,1e9);
printf("%d",n-ans);
}

Codeforces 797 D. Broken BST的更多相关文章

  1. 【codeforces 797D】Broken BST

    [题目链接]:http://codeforces.com/contest/797/problem/D [题意] 给你一个二叉树; 然后问你,对于二叉树中每个节点的权值; 如果尝试用BST的方法去找; ...

  2. Broken BST CodeForces - 797D

    Broken BST CodeForces - 797D 题意:给定一棵任意的树,对树上所有结点的权值运行给定的算法(二叉查找树的查找算法)(treenode指根结点),问对于多少个权值这个算法会返回 ...

  3. AC日记——Broken BST codeforces 797d

    D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...

  4. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  5. CodeForces 797D Broken BST

    $dfs$,线段树. 通过观察可以发现,某位置要能被找到,和他到根这条路上的每个节点的权值存在密切的联系,且是父节点的左儿子还是右儿子也有联系. 可以从根开始$dfs$,边走边更新线段树,如果遍历左儿 ...

  6. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  7. Codeforces 1025 D - Recovering BST

    D - Recovering BST 思路:区间dp dp[l][r][0]表示l到r之间的数字可以构成一个二叉搜索树,并且以r+1为根节点 dp[l][r][0]表示l到r之间的数字可以构成一个二叉 ...

  8. 【26.34%】【codeforces 722A】Broken Clock

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)

    pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...

随机推荐

  1. “Hello World!”团队第六周的第二次会议

    今天是我们团队“Hello World!”团队第六周召开的第二次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...

  2. Alpha发布—文案+美工展示

    目录 团队简介 项目进展 组内分工 队员总结 后期计划 一.团队简介 二.项目进展 从选题发布到今天的Alpha发布,我们团队经历了许许多多的磨难.我们最终设计了如下的功能:首页.班级.个人.更多.打 ...

  3. emmmmmm

    211606342杨艺勇 211606379王熙航 单元测试 对每一个代码块进行测试,返回测试结果并和预期结果进行比对 对源代码进行相应的重构,以适应测试代码的调用,且不影响源代码的正常运行 通过与构 ...

  4. 【第八周】beta阶段事后诸葛亮会议

    本文由宫成荣,武志远共同编写 组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块NEO 会议时间:2016.11.15 18:00~18:40 会议地点: ...

  5. jstack笔记

    遇到java程序跑不动怎么办,jstack是比较容易想到的一个工具,利用jstack来dump出一个线程堆栈快照,然后具体分析. 一般的堆栈大概是由下面的部分组成的: "resin-2212 ...

  6. Spring Cloud 架构 五大神兽的功能

    什么是微服务 微服务的概念源于2014年3月Martin Fowler所写的一篇文章“Microservices”. 微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调 ...

  7. LoadRunner录制用户操作

    先说明一点,使用录制的手段拿到的测试脚本和工程师自己编写的测试脚本其实是一样的,不要觉得录制的方式low,而自己编写脚本就显得高大上,这是不对的.除非工程师本身对开发们写的代码逻辑很熟,对业务上的各个 ...

  8. bzoj5039:[Jsoi2014]序列维护

    做做bzoj上的新题(不存在的) 同bzoj1798: [Ahoi2009]维护序列,样例都一样的...我能想象到的唯一的新的考察意义就是模数是2e9不是1e9,于是加法的时候需要转long long ...

  9. hdu6021[BestCoder #93] MG loves string

    这场BC实在是有趣啊,T2是个没有什么算法但是细节坑的贪心+分类讨论乱搞,T3反而码起来很顺. 然后出现了T2过的人没有T3多的现象(T2:20人,T3:30人),而且T2的AC率是惨烈的不到3% ( ...

  10. element-ui中单独引入Message组件的问题

    import Message from './src/main.js'; export default Message; 由于Message组件并没有install 方法供Vue来操作的,是直接返回的 ...