# 4 - 09. 笛卡尔树（25）

4 - 09. 笛卡尔树（25）题目地址

400 ms

65536 kB

8000 B

Standard

1
6
8 27 5 1
9 40 -1 -1
10 20 0 3
12 21 -1 4
15 22 -1 -1
5 35 -1 -1
YES
2
6
8 27 5 1
9 40 -1 -1
10 20 0 3
12 11 -1 4
15 22 -1 -1
50 35 -1 -1
NO
3
7
8 27 5 1
9 40 -1 -1
10 20 0 3
12 22 -1 4
15 21 6 -1
5 35 -1 -1
13 23 -1 -1
NO
4
6
8 27 5 1
9 40 -1 -1
10 20 0 3
12 21 -1 4
11 22 -1 -1
5 35 -1 -1
NO
5
9
11 5 3 -1
15 3 4 7
5 2 6 0
6 8 -1 -1
9 6 -1 8
10 1 2 1
2 4 -1 -1
20 7 -1 -1
12 9 -1 -1
NO
6
1
1 1 -1 -1
YES

• 用到了并查集，建立二叉树，二叉树中根遍历，使用队列queue<>判断小堆树
• 保证40分钟内完成，需要经常敲(我花了将近50分钟敲下面的代码直接敲)
/*
4 - 09. 笛卡尔树（25）
http://www.patest.cn/contests/ds/4-09
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <queue>

using namespace std;

#define N 1001

int n;

struct mydata{
int k1;
int k2;
int left;
int right;
mydata(int _k1=0, int _k2=0, int _left=-1, int _right=-1) :k1(_k1), k2(_k2), left(_left), right(_right){}
};

typedef struct node{
int k1, k2;
struct node* left;
struct node* right;
node(int _k1 = 0, int _k2 = 0) :k1(_k1), k2(_k2), left(NULL), right(NULL){}
}BNode;

int father[N];
mydata datas[N];

int find(int x)
{
if (x == father[x])
return x;
return father[x] = find(father[x]);
}

void merg(int x, int y) // 并查集合并
{
father[find(y)] = find(x);
}

BNode* root;

BNode* createTreeK1(BNode* root, int i)
{
if (root == NULL)
{
root = new node(datas[i].k1,datas[i].k2);
if (datas[i].left != -1)
{
root->left = createTreeK1(root->left, datas[i].left);
}
if (datas[i].right != -1)
{
root->right = createTreeK1(root->right, datas[i].right);
}
}
return root;
}

// 中根遍历
vector<int> vin;
void inorder(BNode* root)
{
if (root != NULL)
{
inorder(root->left);
vin.push_back(root->k1);
inorder(root->right);
}
}

bool isXiaodui(BNode* root) // 小根堆判断
{
queue<BNode*> que;
while (!que.empty())
{
que.pop();
}
que.push(root);
while (!que.empty())
{
BNode* rt = que.front();
que.pop();

int data = rt->k2;
if (rt->left != NULL)
{
if (rt->left->k2 <= data)
return false;
que.push(rt->left);
}
if (rt->right != NULL)
{
if (rt->right->k2 <= data)
return false;
que.push(rt->right);
}
}
return true;
}

int main()
{
//freopen("in", "r", stdin);
while (scanf("%d", &n) != EOF)
{
int i;
for (i = 0; i < n; i++)
{
father[i] = i;
}
for (i = 0; i < n; i++)
{
scanf("%d%d%d%d", &datas[i].k1, &datas[i].k2, &datas[i].left, &datas[i].right);
if (datas[i].left != -1)
{
if (find(i) != find(datas[i].left))
merg(i, datas[i].left);
}
if (datas[i].right != -1)
{
if (find(i) != find(datas[i].right))
merg(i, datas[i].right);
}
}

int rootNum = find(0);
root = NULL;
root = createTreeK1(root, rootNum);

vin.clear();
inorder(root);
bool flag1 = true;
for (i = 1; i < n; i++)
{
if (vin[i] <= vin[i - 1]) // 后面的 不大于 前面的就不是了
{
flag1 = false;
break;
}
}
if (flag1)
{
bool flag2 = isXiaodui(root);
if (flag2)
{
printf("YES");
}
else{
printf("NO");
}
}
else{
printf("NO");
}
printf("\n");
}
return 0;
}