跳到正文

个人记录

1-解

cutecat1约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月2日
收录
2026年3月13日
更新
2026年11月19日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C++
(虽然但是,颠,抽象,ooc 致歉,私设注意避雷,含唐量极高注意,含 C 语言注意,秦腔注意,触雷致歉)
()内为动作或旁白
【】内为事件
{} 内为内心想法
—————————————————————————————— 我是优雅的分割线 ——————————————————————————————
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=5005;
struct Node{int l,r;}nd[MAXN];
bool w[MAXN],m[MAXN];
int ans=0;
int dfs(int rt){
    if(rt==0)return 0;
    int lh=dfs(nd[rt].l);
    int rh=dfs(nd[rt].r);
    if(lh==0&&rh==0)w[rt]=m[rt]=1;
    else if(lh==1&&rh==0)w[rt]=1,m[rt]=0;
    else if(lh==0&&rh==1)w[rt]=m[rt]=0;
    else if(m[nd[rt].l]&&m[nd[rt].r]&&lh==rh)w[rt]=m[rt]=1;
    else if(m[nd[rt].l]&&w[nd[rt].r]&&lh==rh)w[rt]=1,m[rt]=0;
    else if(w[nd[rt].l]&&m[nd[rt].r]&&lh==rh+1)w[rt]=1,m[rt]=0;
    else w[rt]=m[rt]=0;
    if(w[rt])ans++;
    return max(lh,rh)+1;
}
int main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++)cin>>nd[i].l>>nd[i].r;
    dfs(1);cout<<ans<<endl;
    return 0;
}
原文