1004 Counting Leaves (30分)

今天在热心网友的督促下完成了第一道PAT编程题。
太久没有保持训练了,整个人都很懵。

解题方法:
1.读懂题意
2.分析重点
3.确定算法
4.代码实现

该题需要计算每层的叶子节点个数,所以选用BFS
还有一个关键问题是 如何记录一层的开始和结束
另外,对于新手来说,图的存储也是一个知识点

容易忽略特殊取值情况下的答案:
当非叶节点个数为0,只有根节点一个点,所以直接输出1
而其他情况下,第一层叶子节点数为0

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, maxN = 100 + 24;
int N, M;
int ans[maxN], d[maxN];
vector< vector<int> >  E(maxN);

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d%d", &N, &M);
    for(int i = 0; i < M; i++) {
        int u, v, k; scanf("%d%d", &u, &k);
        d[u] = k;
        for(int j = 0; j < k; j++) {
            scanf("%d", &v);
            E[u].push_back(v);
        }
    }
    memset(ans, 0, sizeof ans);
    queue<int> Q, W;
    Q.push(1);
    if(!M) { printf("1"); return 0; }
    printf("0");

    int h = 1;
    while(!Q.empty()) {
        int x = Q.front(), e = d[x], count = 0;
        Q.pop();
        for(auto u : E[x]) {
            if(d[u] > 0) { W.push(u); count++; }
        }
        ans[h] += e - count;

        if(Q.empty()) {
            Q = W;
            while(W.size()) W.pop();
            h++;
        }
    }
    for(int i = 1; i < h; i++) printf(" %d", ans[i]);

    return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/

原文地址:https://www.cnblogs.com/000what/p/12189665.html

时间: 2024-08-01 15:01:25

1004 Counting Leaves (30分)的相关文章

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)

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

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) 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

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

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

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