PAT -- 甲级1004(1004 Counting Leaves)

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 Specification:

Each input file contains one test case. Each case starts with a line containing 0<N<1000<N<1000<N<100, the number of nodes in a tree, and MMM (<N<N<N), the number of non-leaf nodes. Then MMM lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with NNN being 0. That case must NOT be processed.

Output Specification:

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02

Sample Output:

0 1

给你n个结点,当中有m个父亲节点,接下来又m行,每行的分别给父亲节点father,其k个儿子数,接下来k个数代表father的儿子节点,求该棵以1为根节点的树每层的叶子节点数。

思路:DFS遍历这颗树,注意m为0的时候相当于有n个根节点,所以特判一下输出N。

Code

#include <bits/stdc++.h>

using namespace std;
int N,M;
vector<int> Tree[105];
map<int, int > vis;

void DFS (int idx, int k) {
  if (Tree[idx].size() == 0) {
    vis[k]++;
    return ;
  }
  for (int i = 0; i < Tree[idx].size(); i++) {
    DFS(Tree[idx][i], k + 1);
  }
}

int main() {
  int father, son, k;
  cin >> N >>M;
  while (M--) { //build
    cin >> father >> k;
    while (k--) {
      cin >> son;
      Tree[father].push_back(son);
    }
  }
  if (M == 0) {
    cout << N << endl;
  } else {
    DFS(1, 1);
    cout << vis[1];
    for (int i = 2; i <= vis.size(); i++) {
      cout << " " << vis[i];
    }
    cout << endl;
  }
  return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 17:45
本人简历上&nbsp;1&nbsp;个&nbsp;RAG&nbsp;项目&nbsp;+&nbsp;1&nbsp;个&nbsp;Agent&nbsp;demo;这次面的是AI岗一面前我以为:背完八股&nbsp;+&nbsp;把项目讲清楚,应该能稳过。0-5&nbsp;min:自我介绍&nbsp;+&nbsp;项目背景-&nbsp;顺利。讲清楚了我的&nbsp;RAG&nbsp;是给法律咨询场景做的,痛点是大模型不懂行业术语。5-20&nbsp;min:项目深挖(开始崩)-&nbsp;Q1:你的法律文档总共多少?切了多少个&nbsp;chunk?-&nbsp;我:约&nbsp;500&nbsp;份&nbsp;PDF,5&nbsp;万个&nbsp;chunk-&nbsp;Q2:500&nbsp;份&nbsp;PDF&nbsp;加起来才&nbsp;5&nbsp;万&nbsp;chunk?平均每份&nbsp;100&nbsp;个&nbsp;chunk,你切片粒度是多少?-&nbsp;我:512&nbsp;token-&nbsp;Q3:法律文档里"第三条第二款"和"第三条之二"是不同含义,你的切片会不会把它切散?-&nbsp;我:(沉默&nbsp;5&nbsp;秒)……应该会-&nbsp;Q4:那你怎么解决?-&nbsp;我:我可以加一个&nbsp;metadata……(开始编)❌&nbsp;第一次崩:切片粒度没考虑业务语义。20-35&nbsp;min:评测体系(继续崩)-&nbsp;Q:你怎么知道你的&nbsp;RAG&nbsp;有效?-&nbsp;我:我用&nbsp;Recall@5……-&nbsp;Q:评测集多少条?怎么构造的?-&nbsp;我:100&nbsp;条,我手工标注的-&nbsp;Q:100&nbsp;条够吗?分布怎么样?-&nbsp;我:分布……我没分-&nbsp;Q:那你的&nbsp;Recall@5&nbsp;是&nbsp;0.81,你怎么知道这个数字是好是坏?baseline&nbsp;是什么?-&nbsp;我:(沉默&nbsp;10&nbsp;秒)❌&nbsp;第二次崩:没有&nbsp;baseline,没分布分析,纯靠"看起来还行"。35-55&nbsp;min:Agent&nbsp;部分(彻底崩)-&nbsp;Q:你的&nbsp;Agent&nbsp;demo&nbsp;用了几个工具?-&nbsp;我:3&nbsp;个,搜索、计算器、文档查询-&nbsp;Q:当用户问一个问题,你的&nbsp;Agent&nbsp;怎么决定调哪个工具?-&nbsp;我:用&nbsp;ReAct,让模型自己决定-&nbsp;Q:模型决策错了怎么办?-&nbsp;我:我加了个&nbsp;reflection……-&nbsp;Q:reflection&nbsp;失败&nbsp;3&nbsp;次后怎么处理?-&nbsp;我:(沉默&nbsp;15&nbsp;秒)……我没想过❌&nbsp;第三次崩:异常路径完全没设计。55-65&nbsp;min:业务理解&nbsp;+&nbsp;反问-&nbsp;Q:你觉得字节做&nbsp;AI&nbsp;应用最大的瓶颈是什么?-&nbsp;我:算力?数据?-&nbsp;Q:你看过哪些字节最近发的&nbsp;AI&nbsp;产品?-&nbsp;我:豆包、扣子……-&nbsp;Q:扣子是&nbsp;Agent&nbsp;平台还是工作流平台?-&nbsp;我:(再次沉默)❌&nbsp;第四次崩:对面试公司业务一无所知。
面试官拷打AI项目都会问...
点赞 评论 收藏
分享
xiaowl:1. 技能堆叠没有意义,精简下,而且里面的精通、熟练等内容,其实经不起推敲,这里可以简单写清楚你在前端、后端等领域,有哪些你自己比较经验丰富熟练的技能,以及哪些有过一定涉猎,做一定区分度 2. 项目方案有些单薄,但是这个项目本身还是有很多挑战点的,你应该思考下对于里面有难题的挑战点,你是怎么解决的,避免泛泛而谈。比如,多人编辑是一个老大难问题,包括了互斥、协作等,这里可以详细讲一讲你怎么设计解决问题的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务