P5018?[NOIP2018 普及组] 对称二叉树https://www.luogu.com.cn/problem/P5018
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <cctype>
#include <cstdlib>
#include <deque>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
struct node {
int l, r, val;
} tree[MAXN];
bool same(int now1, int now2) {
if (now1 == -1 && now2 == -1) {
return true;
}
if (now1 == -1 || now2 == -1) {
return false;
}
if (tree[now1].val != tree[now2].val) {
return false;
}
return same(tree[now1].l, tree[now2].r) && same(tree[now1].r, tree[now2].l);
}
int count(int now) {
if (now == -1) {
return 0;
}
return count(tree[now].l) + count(tree[now].r) + 1;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &tree[i].val);
}
for (int i = 1; i <= n; i++) {
scanf("%d %d", &tree[i].l, &tree[i].r);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (same(i, i)) {
ans = max(ans, count(i));
}
}
printf("%d", ans);
return 0;
}
|