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;
int Deep;

void dfs(int x,int deep)
{
    Deep=max(Deep,deep);
    if(g[x].size()==0)
    {
        ans[deep]++;
        return ;
    }
    for(int i=0;i<g[x].size();i++)
        dfs(g[x][i],deep+1);
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<=n;i++) g[i].clear();
    for(int i=1;i<=m;i++)
    {
        int id; scanf("%d",&id);
        int k; scanf("%d",&k);
        while(k--)
        {
            int id2; scanf("%d",&id2);
            g[id].push_back(id2);
        }
    }
    memset(ans,0,sizeof ans);
    root=1; Deep=1;
    dfs(root,1);
    for(int i=1;i<=Deep;i++)
    {
        printf("%d",ans[i]);
        if(i<Deep) printf(" ");
        else printf("\n");
    }
    return 0;
}
时间: 2024-12-24 03:40:47

PAT (Advanced Level) 1004. Counting Leaves (30)的相关文章

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甲题题解-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]; //统计每层的叶子节点

PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; struct Node {

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

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 contains one test case. Each case start