Luo Gu P5018 symmetrical binary tree --hash

To single-handedly link https://www.luogu.com.cn/problem/P5018 

This question is actually a hash of water in the past, we have to maintain two hash

A child is to the left and right sub-tree after tree h1

H2 is a left sub-tree first and then the right subtree

If a point, h2 h1 == right subtree his left subtree

So to this point as the root of the tree is a binary tree symmetry

TIPS: order of hash is critical!

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#define LL long long
using namespace std;
const int M=2e6+7,mod=19260817;
int read(){
    int ans=0,f=1,c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
    return ans*f;
}
int n years = 1 ;
int l [M] r [M] h [M];
LL v1[M],v2[M];
void dfs(int x){
    if(l[x]!=-1){
        dfs(l[x]); h[x]+=h[l[x]];
        v1[x]=(v1[x]*2333+v1[l[x]]*233)%mod;
    }
    if(r[x]!=-1){
        dfs(r[x]); h[x]+=h[r[x]]; 
        v1[x]=(v1[x]*2333+v1[r[x]]*1007)%mod;
        v2[x]=(v2[x]*2333+v2[r[x]]*233)%mod;
    }
    if(l[x]!=-1) v2[x]=(v2[x]*2333+v2[l[x]]*1007)%mod;
    if(l[x]!=-1&&r[x]!=-1&&v1[l[x]]==v2[r[x]]) ans=max(ans,h[x]);
}
int main(){
    //freopen("1.txt","r",stdin);
    n=read();
    for(int i=1;i<=n;i++) h[i]=1,v1[i]=read(),v2[i]=v1[i];
    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read();
    dfs(1); 
    printf("%d\n",ans);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/yourinA/p/12000315.html