蜜汁WA

P5018 [NOIP2018 普及组] 对称二叉树

80分有大佬帮我看看吗```cpp #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; long long n,v[20000006],l[20000006],r[20000006]; long long ans=0; inline bool find(long long x,long long y) { if(l[x]==-1&&r[x]==-1&&l[y]==-1&&r[y]==-1&&v[x]==v[y]) return true; else { if(l[x]==-1&&r[y]==-1&&r[x]!=-1&&l[y]!=-1) { if(find(r[x],l[y])) return true; else return false; } if(r[x]==-1&&l[y]==-1&&l[x]!=-1&&r[y]!=-1) { if(find(l[x],r[y])) return true; else return false; } if(l[x]!=-1&&r[y]!=-1&&r[x]!=-1&&l[y]!=-1) { if(find(r[x],l[y])&&find(l[x],r[y])) return true; else return false; } return false; } } inline long long num(int x) { if(l[x]!=-1&&r[x]==-1) return (num(l[x])+1); if(r[x]!=-1&&l[x]==-1) return (num(r[x])+1); if(l[x]!=-1&&r[x]!=-1) return (num(l[x])+num(r[x])+1); if(l[x]==-1&&r[x]==-1) return 1; } inline void dfs(long long x) { if(l[x]==-1&&r[x]==-1) if(ans<1) ans=1; if(l[x]==-1&&r[x]!=-1) dfs(r[x]); if(r[x]==-1&&l[x]!=-1) dfs(l[x]); if(l[x]!=-1&&r[x]!=-1) { if(find(l[x],r[x])) ans=max(ans,num(x)); dfs(l[x]); dfs(r[x]); } } int main() { cin>>n; for(int i=1;i<=n;i++) cin>>v[i]; for(int i=1;i<=n;i++) cin>>l[i]>>r[i]; dfs(1); cout<<ans; return 0; } ``` noip代码
by 地表最强男人 @ 2019-04-26 16:29:55


```cpp #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; long long n,v[20000006],l[20000006],r[20000006]; long long ans=0; inline bool find(long long x,long long y) { if(l[x]==-1&&r[x]==-1&&l[y]==-1&&r[y]==-1&&v[x]==v[y]) return true; else { if(l[x]==-1&&r[y]==-1&&r[x]!=-1&&l[y]!=-1) { if(find(r[x],l[y])) return true; else return false; } if(r[x]==-1&&l[y]==-1&&l[x]!=-1&&r[y]!=-1) { if(find(l[x],r[y])) return true; else return false; } if(l[x]!=-1&&r[y]!=-1&&r[x]!=-1&&l[y]!=-1) { if(find(r[x],l[y])&&find(l[x],r[y])) return true; else return false; } return false; } } inline long long num(int x) { if(l[x]!=-1&&r[x]==-1) return (num(l[x])+1); if(r[x]!=-1&&l[x]==-1) return (num(r[x])+1); if(l[x]!=-1&&r[x]!=-1) return (num(l[x])+num(r[x])+1); if(l[x]==-1&&r[x]==-1) return 1; } inline void dfs(long long x) { if(l[x]==-1&&r[x]==-1) if(ans<1) ans=1; if(l[x]==-1&&r[x]!=-1) dfs(r[x]); if(r[x]==-1&&l[x]!=-1) dfs(l[x]); if(l[x]!=-1&&r[x]!=-1) { if(find(l[x],r[x])) ans=max(ans,num(x)); dfs(l[x]); dfs(r[x]); } } int main() { cin>>n; for(int i=1;i<=n;i++) cin>>v[i]; for(int i=1;i<=n;i++) cin>>l[i]>>r[i]; dfs(1); cout<<ans; return 0; } ```
by 地表最强男人 @ 2019-04-26 16:30:04


|