1004. Counting Leaves(30)—PAT 甲級

来源:https://www.cnblogs.com/Endlessp162096/archive/2018/04/29/8971705.html
-Advertisement-
Play Games

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 contain ...


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, and M (< N), the number of non-leaf nodes. Then M 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.

Output

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

題目大意:計算樹的每一層上有多少個葉子節點並按層輸出
分析:使用深度有限搜索(DFS)遞歸遍歷樹上每一個節點的孩子節點,如果這個節點沒有孩子節點,就逐層返回。child[i]集合記錄每個節點的孩子節點,leaf[i]數組記錄每一層上的葉子節點,max_h記錄最大層數,層數從1開始。也可以使用廣度優先搜索(BFS),不同之處是DFS使用集合,BFS使用隊列,且BFS不用使用遞歸,只需要第一個節點入隊列後判斷隊列非空即可。

//DFS求葉子節點
#include <iostream>
#include <vector>
using namespace std;
int leaf[100],max_h=1;
vector<int> child[100];
void DFS(int id_num,int h)
{
    if(max_h<h) max_h=h;
    int k=child[id_num].size();
    if(k==0){
        leaf[h]+=1;
        return;
    }
    for(int i=0;i<k;i++)
    {
        DFS(child[id_num][i],h+1);
    }
}
int main() {
    int n,m;
    scanf("%d %d",&n,&m);
    int id_num,k,id;
    for(int i=0;i<m;i++){
        scanf("%d %d",&id_num,&k);
        for(int j=0;j<k;j++){
            scanf("%d",&id);
            child[id_num].push_back(id);
        }
    }
    DFS(1,1);
    for(int i=1;i<=max_h;i++){
        if(i!=1) printf(" ");
        printf("%d",leaf[i]);
    }
    printf("\n");
    return 0;
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 重覆提交原因 從提交頁面到成功頁面的跳轉一般採用視圖定位,由於視圖定位是在服務端跳轉的,如果用戶在點擊提交之後再次刷新頁面,會導致重覆提交,資料庫的數據會有重覆。 採用令牌措施 1、在轉賬展示頁面生成一個隨機的令牌號碼,然後放入session和傳參中。 2、跳轉到轉賬的trans.jsp文件,註意傳 ...
  • 簡介 __setitem__:當屬性被以索引方式賦值的時候會調用該方法 __getitem__:一般如果想使用索引訪問元素時,就可以在類中定義這個方法 __delitem__:當使用索引刪除屬性時調用該方法 實例 ~~~~ __Author__ = "Lance " coding = utf 8 c ...
  • 會話跟蹤是一種靈活的機制,雖然HTTP是一種無狀態協議,但會話跟蹤技術使Web上的狀態編程成為可能,目前普遍存在四種會話跟蹤技術:URL重寫、隱藏表單域、Cookie、Session。 1 隱藏表單域 特點 (參數存放)參數是存放在請求實體里的,因此沒有長度限制,但是不支持 GET 請求方法,因為 ...
  • ArrayList 實現原理:由數組實現的。元素有序,允許重覆。 //其中增長長度的方法,可以看到是創建一個新數組,傳入舊數組和新的數組長度。 private void grow(int minCapacity) { // overflow-conscious code int oldCapacit ...
  • At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door ...
  • Given a non negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specificat ...
  • 傳遞多個參數的四種方式: 順序傳參:public User selectUser(String name,int deptId); <select id="selectUser" resultType="user">select * from user where user_name=#{0} an ...
  • 原文鏈接:https://www.codemore.top/cates/Backend/post/2018-04-21/spring-mvc-handler-methods 由註解@RequestMapping註解修飾的處理請求的函數的簽名非常的靈活,可以使用controller函數支持的一系列參數 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...