关闭

DFS-PATA-1004. Counting Leaves (30)

221人阅读 评论(0) 收藏 举报
分类:

题意:

给出N,M:   节点总个数,非叶节点个数

M行:根节点  根节点后的个数  节点编号

思路:

思路一:可以建树查询

思路二:用数组记录每个编号节点后有几个根节点,如果为空则层数+1;

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
vector <int >adjlist[105];
int record [101]={0};
void dfs(int id ,int level)
{
    if(adjlist[id].empty())
    {
        record[level]++;
        return ;
    }
    for(vector<int>::iterator i=adjlist[id].begin();i!=adjlist[id].end();++i)
    {
        dfs(*i,level+1);
    }
}
int main()
{
    int n, k, x , y;
    cin>>n>>k;
    int cle=n-k; //记录下多少空点
    for(int i=1;i<=k; i++)
    {
        int m;
        cin>>x>>m;
        while(m--)
            {
                cin>>y;
                adjlist[x].push_back(y);
            }
    }
    dfs(1,0);
printf("%d",record[0]);
    int    cnt = record[0];
for(int i=1;cnt<cle;++i){
printf(" %d",record[i]);
cnt += record[i]; //当前遍历到的以节点为根的空节点个数
}
printf("\n");
return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
vector <int >adjlist[105];
int record [101]={0};
void dfs(int id ,int level)
{
    if(adjlist[id].empty())
    {
        record[level]++;
        return ;
    }


    for(vector<int>::iterator i=adjlist[id].begin();i!=adjlist[id].end();++i)
    {
        dfs(*i,level+1);
    }
}
int main()
{
    int n, k, x , y;
    cin>>n>>k;
    int cle=n-k; //记录下多少空点
    for(int i=1;i<=k; i++)
    {
        int m;
        cin>>x>>m;
        while(m--)
            {
                cin>>y;
                adjlist[x].push_back(y);
            }
    }
    dfs(1,0);
	printf("%d",record[0]);
    int    cnt = record[0];
	for(int i=1;cnt<cle;++i){
		printf(" %d",record[i]);
		cnt += record[i]; //当前遍历到的以节点为根的空节点个数
	}
	printf("\n");
	return 0;
}


0
0
查看评论

PAT甲级 1004.Counting Leaves (30) 题目翻译与答案

1004.计算叶子个数 一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。 输入 每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0<N<100),M指非叶结点的个数。然后下面有M行,每行的格式如下:
  • qq278672818
  • qq278672818
  • 2017-02-07 20:29
  • 598

pat1004Counting Leaves (30)

题意分析: (1)给出树中节点的总数以及非叶子节点的总数,并给出各非叶子节点的编号、其孩子节点的个数、其孩子节点的编号,求出每一层叶子节点的个数 (2)题意是求出每一层的叶子结点数,其次给出的节点的父子关系很明确,应使用层序遍历的方式 (3)层序遍历过程中存储节点的队列中在front和rear指针之...
  • LostStephon
  • LostStephon
  • 2015-10-02 14:32
  • 511

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 ...
  • xianyun2009
  • xianyun2009
  • 2014-09-23 17:15
  • 762

浙大PAT 1004 Counting Leaves (简单深搜)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually ...
  • IAccepted
  • IAccepted
  • 2014-03-15 16:32
  • 5008

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 con...
  • hyf20144055065
  • hyf20144055065
  • 2016-07-19 16:17
  • 328

PAT 1004. Counting Leaves (30)(Java一个测试点未通过 and C++)

1004. Counting Leaves (30) Java代码:diwu
  • u010388829
  • u010388829
  • 2014-09-10 09:14
  • 817

PAT-A 1004. Counting Leaves (30) (树)

题目链接:https://www.patest.cn/contests/pat-a-practise/1004 题意: 样例 输入: 2 1 01 1 02 输出: 0 1 第一行一共n个节点,其中非叶子节点有m个,下面m行,每行第一个数id是当前结点编号,第二个...
  • Strokess
  • Strokess
  • 2016-07-29 21:42
  • 329

Pat(Advanced Level)Practice--1004(Counting Leaves)

Pat1004代码 题目描述: A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. ...
  • u012736084
  • u012736084
  • 2014-02-09 22:08
  • 1083

Counting Leaves

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Specification: The in...
  • xmzyjr123
  • xmzyjr123
  • 2016-07-19 21:27
  • 203

a1004. 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 ...
  • a530877647
  • a530877647
  • 2016-03-21 20:55
  • 102
    个人资料
    • 访问:103735次
    • 积分:7089
    • 等级:
    • 排名:第3841名
    • 原创:629篇
    • 转载:14篇
    • 译文:0篇
    • 评论:9条
    博客专栏
    openGL

    文章:15篇

    阅读:5865
    acm

    文章:493篇

    阅读:76000
    最新评论