1 条题解
-
0
C++ :
#include <bits/stdc++.h> using namespace std; struct nod{ int l, r, w; }; nod tree[1000010], tree1[1000010]; int n, cnt[1000010], ans = 0; void read(int &x){ int f=1;x=0;char s=getchar(); while(s<'0'||s>'9'){if (s=='-')f=-1;s=getchar();} while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();} x*=f; } void readp(){ read(n); for (int i = 1; i <= n; ++i){ read(tree[i].w); tree1[i].w = tree[i].w; } for (int i = 1; i <= n; ++i){ read(tree[i].l); read(tree[i].r); tree1[i].r = tree[i].l; tree1[i].l = tree[i].r; } } int dfs(int root){ int s = 1; if (tree[root].l != -1) s += dfs(tree[root].l); if (tree[root].r != -1) s += dfs(tree[root].r); return cnt[root] = s; } bool check(int r1, int r2){ if (tree[r1].w != tree1[r2].w) return false; bool f1, f2; if (tree[r1].l == -1 && tree1[r2].l == -1) f1 = true; else if (tree[r1].l != -1 && tree1[r2].l != -1) f1 = check(tree[r1].l, tree1[r2].l); else f1 = false; if (tree[r1].r == -1 && tree1[r2].r == -1) f2 = true; else if (tree[r1].r != -1 && tree1[r2].r != -1) f2 = check(tree[r1].r, tree1[r2].r); else f2 = false; return f1 && f2; } int main(){ readp(); memset(cnt, -1, sizeof(cnt)); dfs(1); for (int i = 1; i <= n; ++i){ if (cnt[i] <= ans) continue; if (check(i, i)) ans = max(ans, cnt[i]); } printf("%d\n", ans); return 0; }
信息
- ID
- 3636
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者