D. Broken BST

D. Broken BST
time limit per test
1 second
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.

真的是自己智商弱啊23333,做了老半天没懂题目意思,看了别人代码才懂。唉

DFS

代码如下:

//
//  main.cpp
//  D. Broken BST
//
//  Created by 徐智豪 on 2017/4/27.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <set>
using namespace std;
int father[100010],leftson[100010],rightson[100010],leave[100010];
bool vis[100010];
set<int> ans;
int z=0;
void dfs(int cur,int left,int right)
{
   if(leave[cur]>=left&&leave[cur]<=right)
       ans.insert(leave[cur]);
    if(leftson[cur]!=-1)dfs(leftson[cur],left,min(leave[cur]-1,right));
    if(rightson[cur]!=-1)dfs(rightson[cur],max(leave[cur]+1,left),right);
    
}
int main(int argc, const char * argv[]) {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",leave+i,leftson+i,rightson+i);
        if(leftson[i]!=-1)vis[leftson[i]]=1;
        if(rightson[i]!=-1)vis[rightson[i]]=1;
    }
    for(int i=1;i<=n;i++)if(!vis[i])dfs(i,0,10000000000);
    for(int i=1;i<=n;i++)if(ans.find(leave[i])==ans.end())z++;
    cout<<z<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值