PJ T4 不能用哈希吗QAQ?

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

80分代码 17~21点权为1时被卡了 ```cpp #include<bits/stdc++.h> using namespace std; int n,d[1000005],point[1000005],lch[1000005],rch[1000005],maxn=1,bj; vector<int>vec; vector<int>::iterator it; void INIT(int x) { if(lch[x]!=-1){ INIT(lch[x]); point[x]+=point[lch[x]]; } if(rch[x]!=-1){ INIT(rch[x]); point[x]+=point[rch[x]]; } point[x]++; } void check(int x){ if(lch[x]==-1||rch[x]==-1)return; check(lch[x]); check(rch[x]); if(d[lch[x]]!=d[rch[x]])bj=0; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&d[i]); for(int i=1;i<=n;i++)scanf("%d%d",&lch[i],&rch[i]); INIT(1); for(int i=1;i<=n;i++) if(lch[i]!=-1&&rch[i]!=-1) if(point[lch[i]]==point[rch[i]])vec.push_back(i); for(it=vec.begin();it!=vec.end();it++){ bj=1; check(*it); if(bj)maxn=max(maxn,point[*it]); } cout<<maxn<<endl; return 0; } ``` 但理论上这份代码是错的,~~由此可见民间数据有多水~~
by Soledad_S @ 2018-11-11 09:53:35


```pascal begin write('1'); end. ``` 我的打表40分。。。
by xujian @ 2018-11-11 10:00:26


更正,是24分
by xujian @ 2018-11-11 10:00:47


唉,我~~太弱了~~
by xujian @ 2018-11-11 10:01:03


@[longlongzhu123](/space/show?uid=57525) 可以 ``` #include<iostream> #include<cstdio> using namespace std; #define ll long long struct twree{ ll left,right,num; }; twree a[2300003]; ll n; ll check; ll ans=1; ll sum=1; ll read() { ll x=0,f=1; char ch=getchar() ; while(ch<'0'||ch>'9') { if(ch=='-') f=-f; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } void dfs(ll x,ll y) { if(a[x].num==-1&&a[y].num==-1) { return ; } else if(a[x].num==a[y].num) { sum+=2; } else if(a[x].num!=-1||a[y].num!=-1) { sum=1; check=1; return ; } else if(a[x].right!=-1||a[y].left!=-1||a[y].right!=-1||a[x].left!=-1) { //cout<<sum<<endl; sum=1; check=1; return ; } //cout<<a[x].left<<" "<<a[y].right<<" "<<sum<<endl; if(a[x].right==-1&&a[y].left!=-1) { sum=1; check=1; return ; } if(a[x].left==-1&&a[y].right!=-1) { sum=1; check=1; return ; } if(a[y].left==-1&&a[x].right!=-1) { //cout<<a[x].left<<" "<<a[y].right<<" "<<sum<<endl; sum=1; check=1; return ; } if(a[y].right==-1&&a[x].left!=-1) { sum=1; check=1; return ; } if(a[x].right!=-1&&a[y].left!=-1) { //cout<<x<<" "<<y<<" "<<a[x].right<<" "<<a[y].left<<endl; dfs(a[x].right,a[y].left); } if(a[y].right!=-1&&a[x].left!=-1) { //cout<<x<<" "<<y<<" "<<a[x].left<<" "<<a[y].right<<endl; dfs(a[x].left,a[y].right); } if(a[x].left==-1&&a[x].right==-1) return ; if(a[y].left==-1&&a[y].right==-1) return ; } int main() { n=read(); /*if(n==1) { cout<<n; return 0; }*/ for(ll i=1;i<=n;i++) { a[i].num=read(); } for(ll i=1;i<=n;i++) { a[i].left=read(); a[i].right=read(); } for(ll i=1;i<=n;i++) { if(a[i].left==-1||a[i].right==-1) continue; if(a[a[i].left].num!=a[a[i].right].num) continue; dfs(a[i].left,a[i].right); if(check==1) { check=0; continue; } ans=max(ans,sum); sum=1; } cout<<ans<<endl; return 0; } ```
by 任弈凡 @ 2018-11-27 13:38:21


纯暴力
by 任弈凡 @ 2018-11-27 13:38:36


|