zoukankan      html  css  js  c++  java
  • pat 1004

    我觉得海星,直接看代码吧

     1 #include <cstring>
     2 #include <iostream>
     3 #include <vector>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 int fa[105];//对应节点的父亲
     9 bool has_cd[105];//是否有孩子
    10 int cnt[105];//对应深度的无孩子节点总数
    11 int d[105];//对应节点的深度
    12 
    13 int depth(int i)
    14 {
    15     int &r = d[i];
    16     if (r >= 0)return r;//已经处理过直接返回
    17     if (i == 1)return 0;//root
    18     r = depth(fa[i]) + 1;//计算对应深度,别急着返回
    19     if (has_cd[i] == 0)//no_cd
    20         ++cnt[r];//直接在对应深度的计数器+1,root即01已经特殊处理
    21     return r;
    22 }
    23 
    24 int main(void)
    25 {
    26     memset(has_cd, 0, sizeof(has_cd));//没有孩子
    27     memset(cnt, 0, sizeof(cnt));
    28     memset(d, -1, sizeof(d));
    29     
    30     int n, m;
    31     cin >> n >> m;
    32     for (int i = 0;i < m;i++)
    33     {
    34         int f, k;
    35         cin >> f >> k;
    36         has_cd[f] = true;//有了(滑稽
    37         for (int j = 0;j < k;j++)
    38         {
    39             int cd;
    40             cin >> cd;
    41             fa[cd] = f;//认父亲
    42         }
    43     }
    44 
    45     for (int i = 0;i < n;i++)//注意是个循环
    46         depth(i + 1);
    47     int max_dep = -1;//下面计算最大深度
    48     for (int i = 0;i < n;i++)
    49         if (d[i + 1] > max_dep)max_dep = d[i + 1];
    50 
    51     if (n == 1 && m == 0)cout << "1" << endl;//单节点,虽然题目限定m<n应该不会有这种情况
    52     else {
    53         for (int i = 0;i < max_dep;i++)
    54             cout << cnt[i] << " ";
    55         cout << cnt[max_dep] << endl;
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    相对定位和绝对定位
    一切重新开始
    Oracle Profile 使用
    使用javamail发送邮件错误:550 5.7.1 Unable to relay
    gvim 备份文件去除 配置
    解决Maven中OutOfMemory错误
    sqlplus启动后的环境SQLPATH的设置
    ORA-30004 错误处理
    oracle 锁表查询及解决、表字段查询
    如何进行软件架构设计
  • 原文地址:https://www.cnblogs.com/schsb/p/8795903.html
Copyright © 2011-2022 走看看