-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1043.cpp
More file actions
executable file
·87 lines (80 loc) · 1.81 KB
/
Copy path1043.cpp
File metadata and controls
executable file
·87 lines (80 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* 完全二叉树判断
* 凭直觉写
* 草死了二叉树鲨我
**/
#include <stdio.h>
#include <iostream>
using namespace std;
int N;
struct node{
int self;
int fa;
node *left, *right;
node():fa(-1), left(NULL), right(NULL) {}
};
node no[1000233];
int fa[1000233];
void build() {
for(int i = 0;i <= N;++ i) {
no[i].self = i;
no[i].fa = fa[i];
}
for(int i = 0;i < N;++ i) {
node *fano = &(no[fa[i]]);
node *le = fano->left;
if(le) {
if(le->self > i) {
fano->right = le;
fano->left = &(no[i]);
} else {
fano->right = &(no[i]);
}
} else {
if(i) fano->left = &(no[i]);
}
}
}
bool check(node *rt) {
if(rt == NULL) {
return 0;
}
node* che[1000233];
int head = 0, rear = 0;
che[rear ++] = rt;
while(head != rear) {
node *top = che[head];
if(top->left && top->right) {
++ head;
che[rear ++] = top->left;
che[rear ++] = top->right;
} else
if(top->left == NULL && top->right) {
return 0;
} else
if((top->left && top->right == NULL) ||
(top->left == NULL && top->right == NULL)) {
++ head;
while(head != rear) {
top = che[head];
if(top->left == NULL && top->right == NULL) {
++ head;
} else {
return 0;
}
}
return 1;
}
}
return 1;
}
int main() {
scanf("%d", &N);
for(int i = 1;i < N;++ i) {
scanf("%d", &fa[i]);
}
build();
if(check(&(no[0]))) cout << "true" << endl;
else cout << "false" << endl;
return 0;
}