2021.08.09 P5018 对称二叉树(树形结构)
2021.08.09 P5018 对称二叉树(树形结构)
[P5018 NOIP2018 普及组] 对称二叉树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题意:
求一棵子树,关于该子树根节点轴对称的节点数最大。
分析:
暴力。
代码如下:
40tps
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6+10;
int n,val[N],ans;
struct node{
int l,r,flag,sum,val,size;
}a[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
s=s*10+ch-'0';
ch=getchar();
}
return s*w;
}
void dfs(int x,int flag){
//fa[x]=f;dep[x]=dep[fa[x]]+1;
if(!x)return ;
//a[x].flag=flag;a[x].val=val[x];
//a[x].maxn=dep[x];
a[x].flag=flag;
a[x].sum=a[x].val=flag*a[x].val;
//a[x].val直接读入
a[x].size=1;
dfs(a[x].l,1);dfs(a[x].r,-1);
a[x].sum+=a[a[x].l].sum+a[a[x].r].sum;
//a[x].maxn=max(a[x].maxn,max(a[a[x].l].maxn,a[a[x].r].maxn));
a[x].size+=(a[a[x].l].size+a[a[x].r].size);
if(a[x].sum==a[x].val&&a[a[x].l].size==a[a[x].r].size&&a[a[x].l].sum==-a[a[x].r].sum)ans=max(ans,a[x].size);
}
int main(){
n=read();
for(int i=1;i<=n;i++)a[i].val=read();
for(int i=1;i<=n;i++){
a[i].l=read();a[i].r=read();
if(a[i].l==-1)a[i].l=0;
if(a[i].r==-1)a[i].r=0;
//cout<<i<<" "<<a[i].l<<" "<<a[i].r<<endl;
}
dfs(1,1);
//for(int i=1;i<=n;i++)
//cout<<i<<" l "<<a[i].l<<" r "<<a[i].r<<" flag "<<a[i].flag<<" val "<<a[i].val<<" size "<<a[i].size<<" sum "<<a[i].sum<<endl;
cout<<ans;
return 0;
}
100tps
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6+10;
int n;
struct node{
int l,r,size,val;
}a[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
s=s*10+ch-'0';
ch=getchar();
}
return s*w;
}
void dfs(int x){
a[x].size=1;
if(a[x].l)dfs(a[x].l),a[x].size+=a[a[x].l].size;
if(a[x].r)dfs(a[x].r),a[x].size+=a[a[x].r].size;
}
bool check(int x,int y){
if(!x&&!y)return true;
if(x&&y&&a[x].val==a[y].val&&check(a[x].l,a[y].r)&&check(a[x].r,a[y].l))
return true;
return false;
}
int main(){
n=read();
for(int i=1;i<=n;i++)a[i].val=read();
for(int i=1;i<=n;i++){
a[i].l=read();a[i].r=read();
if(a[i].l==-1)a[i].l=0;
if(a[i].r==-1)a[i].r=0;
}
dfs(1);
int ans=0;
for(int i=1;i<=n;i++)if(check(a[i].l,a[i].r))ans=max(ans,a[i].size);
cout<<ans;
return 0;
}
2021.08.09 P5018 对称二叉树(树形结构)的更多相关文章
- 2021.08.06 P3478 STA-Station(树形结构)
2021.08.06 P3478 STA-Station(树形结构) [P3478 POI2008]STA-Station - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 给 ...
- 2021.08.09 P5658 括号树(树形结构)
2021.08.09 P5658 括号树(树形结构) [P5658 CSP-S2019] 括号树 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 太长,在链接中. 分析及代码 ...
- 2021.08.09 P7238 迷失森林(树的直径)
2021.08.09 P7238 迷失森林(树的直径) P7238 「DCOI」迷失森林 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.树的直径两种求法:两次dfs.树 ...
- 2021.08.09 P4868 Preprefix sum(树状数组)
2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...
- 2021.08.09 P6037 Ryoku的探索(基环树)
2021.08.09 P6037 Ryoku的探索(基环树) P6037 Ryoku 的探索 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.树的性质 2.基环树的性质 ...
- 2021.08.09 P6225 抑或橙子(树状数组)
2021.08.09 P6225 抑或橙子(树状数组) 重点: 1.异或用法 题意: Janez 喜欢橙子!他制造了一个橙子扫描仪,但是这个扫描仪对于扫描的每个橙子的图像只能输出一个 3232 位整数 ...
- Luogu P5018 对称二叉树 瞎搞树&哈希
我的天..普及组这么$hard$... 然后好像没有人用我的垃圾做法,,,好像是$O(n)$,但十分的慢,并且极其暴力$qwq$ 具体来说,就是直接$dfs$求出树高,然后想像出把原来的树补成满二叉树 ...
- 洛谷P5018 对称二叉树——hash
给一手链接 https://www.luogu.com.cn/problem/P5018 这道题其实就是用hash水过去的,我们维护两个hash 一个是先左子树后右子树的h1 一个是先右子树后左子树的 ...
- P5018 对称二叉树题解
题目内容链接: 那么根据题意,上图不是对称二叉树,只有节点7的子树是: 通俗来说,对称二叉树就是已一个节点x为根的子树有穿过x点的对称轴并且对称轴两边的对称点的大小也必须相等,那么这棵树就是对称二叉树 ...
随机推荐
- 4月2日 python学习总结
昨天内容回顾: 1.迭代器 可迭代对象: 只要内置有__iter__方法的都是可迭代的对象 既有__iter__,又有__next__方法 调用__iter__方法==>得到内置的迭代器对象 调 ...
- sql注入之简要注入
#mysql简要注入流程 mysql注入简要流程如下图: 由于还没学习完先结合sqlilabs的第一关讲解信息收集的一部分和数据注入的information_schema的有据查询. #sqli-la ...
- jdr挖的大坑
计数问题小结 很多计数问题在直接拆分计算贡献时都会出现不容易直接表示的情况.在解决这些问题时,往往需要解决一些子问题方案数的递推, 再套用组合数或者分块计算来降低难度或时间复杂度,这里给出几种递推方法 ...
- python编程笔记--字符编码
ASCII码.Unicode.utf-8 ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电 ...
- 【vue】中英文切换(使用 vue-i18n )
一.准备工作 1.vue-i18n 1.仓库地址 2.兼容性:支持 Vue.js 2.x 以上版本 1-1.安装依赖vue-i18n (c)npm install vue-i18n 1-2.使用 在 ...
- Error running 'App': Command line is too long. Shorten command line for App or also for Spring Boot default configuration.
找到标签 <component name="PropertiesComponent">.在标签里加一行 : <property name="dynam ...
- Kafka 新旧消费者的区别?
旧的 Kafka 消费者 API 主要包括:SimpleConsumer(简单消费者) 和 ZookeeperConsumerConnectir(高级消费者).SimpleConsumer 名字看起来 ...
- spring-boot-learning-监控相关
springboot提供了对项目的监控功能,首先我们需要引入需要的jar包: <!--监控包--> <!-- https://mvnrepository.com/artifact/o ...
- php安装扩展的两种方法
方法一:使用yum命令安装 1.yum install libevent-devel 2.pecl install channel://pecl.php.net/libevent-0.1.0 3.ec ...
- git提交错误 git config --global user.email “you@example.com“ git config --global user.name “Your Name
1 Commit failed - exit code 128 received, with output: '*** Please tell me who you are. 2 3 Run 4 5 ...