zoukankan      html  css  js  c++  java
  • $P5018 对称二叉树$

    problem

    一直忘记给这个题写题解了。

    这题挺水的吧。
    挺后悔当时没写出来。

    #ifdef Dubug
    
    #endif
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL ;
    inline LL In() { LL res(0),f(1); register char c ;
    	while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
    	while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
    	return res * f ;
    }
    
    int n ;
    const int N = 1e6 + 5 ;
    int a[N] ;
    int Left[N] , Right[N] ;
    int sum = 1 ;
    
    inline bool dfs(int x,int y) {//搜索能否有对称。
    	if(x == -1 and y == -1) return 1 ;
    	if(x == -1 or y == -1 or a[x] != a[y]) return 0 ;
    	sum += 2 ;
    	return (dfs(Left[x],Right[y]) and dfs(Left[y],Right[x])) ;
    }
    
    signed main() {
    	n = In() ;
    	for(register int i=1;i<=n;i++) a[i] = In() ;
    	for(register int i=1;i<=n;i++) Left[i] = In() , Right[i] = In() ;
    	int ans = -0x7f ;
    	for(register int i=1;i<=n;i++) {//每个枚举一遍。n个节点。
    		sum = 1 ;//初始化
    		if(dfs(Left[i],Right[i])) ans = max(ans,sum) ;//如果有对称 那么更新ans
    	}
    	cout << ans << endl ;
    	return 0 ;
    }
    
    不存在十全十美的文章 如同不存在彻头彻尾的绝望
  • 相关阅读:
    final
    职场语句
    故事
    三个关键字
    关于重读字母去掉的代码
    Java书
    docker私库harbor的搭建
    配置允许匿名用户登录访问vsftpd服务,进行文档的上传下载、文档的新建删除等操作
    docker容器内外相互拷贝数据
    docker
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10611652.html
Copyright © 2011-2022 走看看