BFS40分求助!
查看原帖
BFS40分求助!
883706
zxc2014722928楼主2024/4/4 20:04
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int ans = 0;
int n;
struct node{
    int l,r;
}tree[N];


void bfs(){
    queue<node> q;
    q.push(tree[1]);
    while(q.size()){
        ans++;
        for(int i = 0;i<q.size();i++){
            auto temp = q.front();
            q.pop();
            if(temp.l!=0) q.push(tree[temp.l]);
            if(temp.r!=0) q.push(tree[temp.r]);
        }
    }
}
int main(){
    cin>>n;
    for(int i = 1;i<=n;i++){
        int l,r;
        cin>>l>>r;
        tree[i].l = l;
        tree[i].r = r;
    }
    bfs();
    cout<<ans;
}

第一个和第五个对了 其他都错了

2024/4/4 20:04
加载中...