【LG5018】[NOIP2018pj]对称的二叉树

题面

洛谷

题解

看到这一题全都是用\(O(nlogn)\)的算法过的

考场上写\(O(n)\)算法的我很不开心

然后就发了此篇题解。。。

首先我们可以像树上莫队一样按照 左-右-根 的顺序将这棵树的欧拉序跑下来,

记下开始访问点\(x\)的\(dfs\)序\(L[x]\),和回溯时的\(dfs\)序\(R[x]\)

再将记录欧拉序的数组记为\(P\)

void dfs(int x) {
P[L[x] = ++cnt] = x;
if (t[x].ch[0]) dfs(t[x].ch[0]);
if (t[x].ch[1]) dfs(t[x].ch[1]);
P[R[x] = ++cnt] = x;
t[x].size = t[t[x].ch[0]].size + t[t[x].ch[1]].size + 1;
}

统计出数组\(P\)的两个哈希值,一个是记录点权(\(hs1[0][x]\)),

另一个是记录当前点是左儿子还是右儿子(\(hs2[0][x]\))

for (int i = 1; i <= cnt; i++) hs1[0][i] = hs1[0][i - 1] * base + t[P[i]].v;
for (int i = 1; i <= cnt; i++) hs2[0][i] = hs2[0][i - 1] * base + get(P[i]);

再将这棵树按照 右-左-根 的顺序将这棵树的另一个欧拉序跑下来(记得清空),

记下开始访问点\(x\)的\(dfs\)序\(rL[x]\),和回溯时的\(dfs\)序\(rR[x]\)

void rdfs(int x) {
P[rL[x] = ++cnt] = x;
if (t[x].ch[1]) rdfs(t[x].ch[1]);
if (t[x].ch[0]) rdfs(t[x].ch[0]);
P[rR[x] = ++cnt] = x;
}

再记录一次统计出数组\(P\)的两个哈希值,一个是记录点权(\(hs1[1][x]\)),

另一个是记录当前点是左儿子还是右儿子(\(hs2[1][x]\))(这时候要取异或一下)

    for (int i = 1; i <= cnt; i++) hs1[1][i] = hs1[1][i - 1] * base + t[P[i]].v;
for (int i = 1; i <= cnt; i++) hs2[1][i] = hs2[1][i - 1] * base + (get(P[i]) ^ 1);

其中\(get\)函数:

inline int get(int x) { return t[t[x].fa].ch[1] == x; }

然后我们要怎么判断呢?

先判断左右儿子\(ls\)和\(rs\)的\(size\)是否相等

然后再判断第一遍\(dfs\)左儿子所覆盖的欧拉序内和

第二遍\(dfs\)右儿子所覆盖的欧拉序内两个哈希值相不相等即可

if (getHash(hs1[0], L[ls], R[ls]) != getHash(hs1[1], rL[rs], rR[rs])) continue;
if (getHash(hs2[0], L[ls], R[ls]) != getHash(hs2[1], rL[rs], rR[rs])) continue;

然而常数过大,速度被nlogn吊打

完整代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
namespace IO {
const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
inline char gc() {
if (is == it) it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return *is++;
}
}
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = IO::gc();
if (ch == '-') w = -1 , ch = IO::gc();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = IO::gc();
return w * data;
}
#define MAX_N 1000005
struct Node { int ch[2], fa, size, v; } t[MAX_N];
inline int get(int x) { return t[t[x].fa].ch[1] == x; }
typedef unsigned long long ull;
const ull base = 100007;
ull pw[MAX_N << 1];
ull hs1[2][MAX_N << 1], hs2[2][MAX_N << 1];
ull getHash(ull *hs, int l, int r) { return hs[r] - hs[l - 1] * pw[r - l + 1]; }
int N, L[MAX_N], R[MAX_N], rL[MAX_N], rR[MAX_N], P[MAX_N << 1], cnt;
void dfs(int x) {
P[L[x] = ++cnt] = x;
if (t[x].ch[0]) dfs(t[x].ch[0]);
if (t[x].ch[1]) dfs(t[x].ch[1]);
P[R[x] = ++cnt] = x;
t[x].size = t[t[x].ch[0]].size + t[t[x].ch[1]].size + 1;
}
void rdfs(int x) {
P[rL[x] = ++cnt] = x;
if (t[x].ch[1]) rdfs(t[x].ch[1]);
if (t[x].ch[0]) rdfs(t[x].ch[0]);
P[rR[x] = ++cnt] = x;
}
int main () {
N = gi(); pw[0] = 1;
for (int i = 1; i <= 2 * N; i++) pw[i] = pw[i - 1] * base;
for (int i = 1; i <= N; i++) t[i].v = gi();
for (int x = 1; x <= N; x++) {
int ls = gi(), rs = gi();
if (ls != -1) t[x].ch[0] = ls, t[ls].fa = x;
if (rs != -1) t[x].ch[1] = rs, t[rs].fa = x;
}
dfs(1);
for (int i = 1; i <= cnt; i++) hs1[0][i] = hs1[0][i - 1] * base + t[P[i]].v;
for (int i = 1; i <= cnt; i++) hs2[0][i] = hs2[0][i - 1] * base + get(P[i]);
cnt = 0; rdfs(1);
for (int i = 1; i <= cnt; i++) hs1[1][i] = hs1[1][i - 1] * base + t[P[i]].v;
for (int i = 1; i <= cnt; i++) hs2[1][i] = hs2[1][i - 1] * base + (get(P[i]) ^ 1);
int ans = 1;
for (int x = 1; x <= N; x++) {
int ls = t[x].ch[0], rs = t[x].ch[1];
if (t[ls].size != t[rs].size) continue;
if (getHash(hs1[0], L[ls], R[ls]) != getHash(hs1[1], rL[rs], rR[rs])) continue;
if (getHash(hs2[0], L[ls], R[ls]) != getHash(hs2[1], rL[rs], rR[rs])) continue;
ans = max(ans, t[x].size);
}
printf("%d\n", ans);
return 0;
}

【LG5018】[NOIP2018pj]对称的二叉树的更多相关文章

  1. [NOIP2018PJ]对称二叉树

    [NOIP2018PJ]对称二叉树 这个题正常人看到题面难道不是哈希? 乱写了个树哈希... #include<bits/stdc++.h> using namespace std; co ...

  2. 《剑指offer》第二十八题(对称的二叉树)

    // 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...

  3. (剑指Offer)面试题59:对称的二叉树

    题目: 请实现一个函数,用来判断一颗二叉树是不是对称的. 注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 思路: 对于一棵二叉树,从根结点开始遍历, 如果左右子结点有一个为NULL,那 ...

  4. 【剑指offer】面试题 28. 对称的二叉树

    面试题 28. 对称的二叉树 题目描述 题目:请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 解答过程 给定一个二叉树,检查它是否是镜像 ...

  5. 第28题:leetcode101:Symmetric Tree对称的二叉树

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  6. 剑指Offer:对称的二叉树【28】

    剑指Offer:对称的二叉树[28] 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 题目分析 Java题解 /* publi ...

  7. 【Offer】[28] 【对称的二叉树】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请实现一个函数,用来判断一-棵二叉树是不是对称的.如果一棵二叉树和它的镜像一样,那么它是对称的.  牛客网刷题地址 思路分析 利用前序 ...

  8. php算法题---对称的二叉树

    php算法题---对称的二叉树 一.总结 一句话总结: 可以在isSymmetrical()的基础上再加一个函数comRoot,函数comRoot来做树的递归判断 /*思路:首先根节点以及其左右子树, ...

  9. Leetcode:面试题28. 对称的二叉树

    Leetcode:面试题28. 对称的二叉树 Leetcode:面试题28. 对称的二叉树 Talk is cheap . Show me the code . /** * Definition fo ...

随机推荐

  1. ethereumjs-vm/examples/run-transactions-simple

    https://github.com/ethereumjs/ethereumjs-vm/tree/master/examples/run-transactions-simple prerequisit ...

  2. Level/levelup-1-简介

    https://github.com/Level/levelup A node.js wrapper for abstract-leveldown compliant stores 一个为实现抽象le ...

  3. 将form表单元素的值序列化成对象

    /**jQuery * 将form表单元素的值序列化成对象 * @returns object */ var serializeObject = function(form) { var o = {} ...

  4. 无法加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项

    未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配. (异常来自 HRESULT:0x80131040). 有时候我们创建了一个类库,我们 ...

  5. a、button、input点击获取焦点时出现蓝色边框,如何去掉

    a,button,input{ -webkit-tap-highlight-color: rgba(0, 0, 0, 0); -webkit-user-modify: read-write-plain ...

  6. ios的framework合并

    # 运行此脚本前 # 先编译一遍工程 确保正常运行 没有报错 # 作为Xcode Aggregate运行 # file-->new target-->cross-platform--> ...

  7. 使用XWAF框架(4)——LunarCalendar日历组件

    XWAF提供了管理日历的com.xwaf.date.LunarCalendar静态类,可以直接使用,非常方便.该类包括六个主要静态方法: 4.1  isLeapYear(int year) 判断公历年 ...

  8. File、Paths和Files类的使用详解

    Paths:通过get()方法返回一个Path对象,Path用于表示文件路径和文件. Files:提供了大量处理文件的方法,例如文件复制.读取.写入,获取文件属性.快捷遍历文件目录等..... Fil ...

  9. Centos6.5中如何用sqlite3命令打开’.db’后缀的数据库执行sql

      1. 简单sql语句使用: 在任意目录下新建一个数据库,比如student . 命令: sqlite3 student.db 出现如下提示: 输入sql语句create table user(us ...

  10. (1-1)入门—最简单的树(使用json数据)

    1.<!DOCTYPE html>是必须的. 2.zTree 的容器 className 别忘了设置为 "ztree". 使用ztree创建树,首先要引用ztree相关 ...