PAT:1004. Counting Leaves (30) AC

#include<stdio.h>
#include<vector>
const int MAX=510;
using namespace std;

int n,m,le=0;          //节点数,非叶子节点数,最深层叶层数
vector<int> child[MAX];    //存储孩子情况
int number[MAX];      //每一层叶子数

void DFS(int s,int l)
{
  if(child[s].size()==0)
  {
    ++number[l];
    if(le<l)
      le=l;
    return;
  }
  for(int i=0 ; i<child[s].size() ; ++i)
  {
    DFS(child[s][i],l+1);
  }
}

int main()
{
  scanf("%d%d",&n,&m);
  for(int i=0 ; i<m ; ++i)
  {
    int node,k,c;
    scanf("%d%d",&node,&k);
    for(int j=0 ; j<k ; ++j)
    {
      scanf("%d",&c);
      child[node].push_back(c);
    }
  }

  DFS(1,1);      //从节点1开始,1是第一层

  for(int i=1; i<=le ; ++i)    //输出每层叶子数
  {
    printf("%d",number[i]);
    if(i!=le)
      printf(" ");
  }

  return 0;
}
时间: 2024-10-10 14:59:09

PAT:1004. Counting Leaves (30) AC的相关文章

1004. Counting Leaves (30)——PAT (Advanced Level) Practise

题目信息: 1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contai

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

PTA 1004 Counting Leaves (30)(30 分)(建树dfs或者bfs,未AC 段错误)

1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 10

1004 Counting Leaves (30)(30 分)

1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 10

PAT 1004. Counting Leaves (30) C#实现

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; /* 统计每层的叶子节点个数 建树,然后dfs即可 */ const int maxn=105; int n,m; int layer[maxn]; //统计每层的叶子节点

1004 Counting Leaves (30分)

今天在热心网友的督促下完成了第一道PAT编程题. 太久没有保持训练了,整个人都很懵. 解题方法: 1.读懂题意 2.分析重点 3.确定算法 4.代码实现 该题需要计算每层的叶子节点个数,所以选用BFS 还有一个关键问题是 如何记录一层的开始和结束 另外,对于新手来说,图的存储也是一个知识点 容易忽略特殊取值情况下的答案: 当非叶节点个数为0,只有根节点一个点,所以直接输出1 而其他情况下,第一层叶子节点数为0 /**/ #include <cstdio> #include <cstrin

PAT:1004. 成绩排名 (20) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; typedef struct STU { char mname[15]; char mID[15]; int mscore; }STU; bool cmp(STU a,STU b) //[skill]使用sort函数自己构造比较 { return a.mscore&g