PAT (甲级)1004 Counting Leaves

题目

题目链接

思路

先把每个节点放到一个vector中,二维数组vector,每一行代表一个节点,下标代表这个节点的id,把此节点的孩子id作为这行的数据
DFS: dfs其实就是要用一个栈来把之前经历过得节点存起来,目的是为了递归结束的时候知道下一个该处理的元素;但这个题,因为你已经知道下一个要处理的元素,所以没有必要;核心就是先判断递归结束的条件:没有孩子就return;然后分别dfs 它的孩子就可以
BFS: bfs要用队列,等处理完兄弟节点后,在依次处理他们各自的孩子,正好符合队列的性质;同样的,这个题,你已经知道处理的顺序,所以直接写就好,不需要把元素放到队列中;核心就是先判断递归结束的条件,如果有孩子则依次看孩子是不是满足要求,然后依次递归就好

代码
BFS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> tree(105);
vector<int> depthVec(100,0);//用来存放每一层的子节点
int maxDepth = -1;//最大深度
//用来遍历这个节点下的所有孩子,并记录depth
void dfs(int nodeId, int depth){
    if(tree[nodeId].size() == 0){
        depthVec[depth]++;
        maxDepth = max(maxDepth,depth);
        return ;
    }
    else{
        for(int i=0; i<tree[nodeId].size(); ++i){
            dfs(tree[nodeId][i], depth+1);
        }
    }
}
int main(){
    //先把每个节点用vector存起来
    int total, noLeafTotal;
    cin >> total >>noLeafTotal;
    for(int i=0; i<noLeafTotal; ++i){
        int nodeId,cnt;
        cin >> nodeId >> cnt;
        for(int j=0; j<cnt; ++j){
            int childId;
            cin >> childId;
            tree[nodeId].push_back(childId);
        }
    }
    dfs(1,0);
    for(int i=0; i<=maxDepth; ++i){
        cout << depthVec[i];
        if(i != maxDepth) cout << " ";
    }
    return 0;
}
BFS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<vector<int>> tree(105);
vector<int> depthVec(100,0);//用来存放每一层的子节点
int maxDepth = -1;//最大深度
//用来遍历这个节点下的所有孩子,并记录depth
void bfs(int nodeId, int depth){
    if(tree[nodeId].size() == 0){
        maxDepth = max(maxDepth,depth);
        return ;
    }
    else{
        for(int i=0; i<tree[nodeId].size(); ++i){
            if(tree[tree[nodeId][i]].size() == 0) depthVec[depth+1]++;
        }
        for(int i=0; i<tree[nodeId].size(); ++i)
            bfs(tree[nodeId][i],depth+1);
    }
}
int main(){
    //先把每个节点用vector存起来
    int total, noLeafTotal;
    cin >> total >>noLeafTotal;
    for(int i=0; i<noLeafTotal; ++i){
        int nodeId,cnt;
        cin >> nodeId >> cnt;
        for(int j=0; j<cnt; ++j){
            int childId;
            cin >> childId;
            tree[nodeId].push_back(childId);
        }
    }
    bfs(1,0);
    for(int i=0; i<=maxDepth; ++i){
        cout << depthVec[i];
        if(i != maxDepth) cout << " ";
    }
    return 0;
}
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <string>
#include <math.h>
using namespace std;
const int maxn = 105;
struct node{
     vector<int> children;
}Node[maxn];

vector<int> num(maxn);
int n, m, level = 0;

void BFS(int id){
     queue<int> Q;
     Q.push(id);
     int top;
     while(!Q.empty()){
          int k = Q.size();
          for(int i = 0; i < k; i ++){
               top = Q.front();
               Q.pop();
               //如果是叶子节点,节点数加一
               if(Node[top].children.size() == 0) num[level]++;
               //如果不是叶子结点就把孩子节点入队
               for(int j = 0; j < Node[top].children.size(); j ++)
                    Q.push(Node[top].children[j]);
          }
          level++;//层数加一
     }
     return ;
}
int main()
{
     scanf("%d%d", &n, &m);
     int id, k, t;
     //保存树
     for(int i = 0; i < m; i ++){
          scanf("%d%d", &id, &k);
          for(int j = 0; j < k; j ++){
               scanf("%d", &t);
               Node[id].children.push_back(t);
          }
     }
     BFS(1);
     for(int i = 0; i < level; i ++){
          printf("%d", num[i]);
          if(i != level - 1) printf(" ");
     }
     system("pause");
     return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值