zoukankan      html  css  js  c++  java
  • L2-2 小字辈 (25 分)

    本题给定一个庞大家族的家谱,要请你给出最小一辈的名单。

    输入格式:

    输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号。随后第二行给出 N 个编号,其中第 i 个编号对应第 i 位成员的父/母。家谱中辈分最高的老祖宗对应的父/母编号为 -1。一行中的数字间以空格分隔。

    输出格式:

    首先输出最小的辈分(老祖宗的辈分为 1,以下逐级递增)。然后在第二行按递增顺序输出辈分最小的成员的编号。编号间以一个空格分隔,行首尾不得有多余空格。

    输入样例:

    9
    2 6 5 5 -1 5 6 4 7
    

    输出样例:

    4
    1 9

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #define ll long long
     5 using namespace std;
     6 const int amn=1e5+5;
     7 int a[amn];
     8 struct mem
     9 {
    10     int fa,rank;
    11 }m[amn];
    12 int z;
    13 int fi(int x)
    14 {
    15     if(m[x].rank)return m[x].rank;
    16     return m[x].rank=fi(m[x].fa)+1;
    17 }
    18 int main()
    19 {
    20     int n,in;
    21     while(cin>>n)
    22     {
    23         for(int i=1;i<=n;i++)
    24         {
    25             m[i].rank=m[i].fa=0;
    26         }
    27         for(int i=1;i<=n;i++)
    28         {
    29             cin>>in;
    30             m[i].fa=in;
    31             if(in==-1)
    32             {
    33                 z=in;
    34                 m[i].rank=1;
    35             }
    36         }
    37         int maxn=0;
    38         for(int i=1;i<=n;i++)
    39         {
    40             if(i!=z)
    41             fi(i);
    42             if(m[i].rank>maxn)maxn=m[i].rank;
    43         }
    44         int top=0;
    45         for(int i=1;i<=n;i++)
    46         {
    47             if(m[i].rank==maxn)a[top++]=i;
    48         }
    49         sort(a,a+top);
    50         cout<<maxn<<endl;
    51         for(int i=0;i<top;i++)
    52         {
    53             cout<<a[i];
    54             if(i<top-1)cout<<" ";
    55             else cout<<endl;
    56         }
    57     }
    58 }
  • 相关阅读:
    Dynamics 365 CRM large instance copy
    Dynamics CRM Plug-in
    Dynamics CRM Publisher
    Dynamics 365 CRM Free up storage 清理Dynamics 365 CRM的空间
    账户和联系人 Accounts and Contacts 译
    Dynamics CRM Instances
    Dynamics CRM Solution
    微软Azure通知中心 (Azure Notification Hubs)
    CLR(Common Language Runtime) 公共语言运行库
    构建Apache Web服务器
  • 原文地址:https://www.cnblogs.com/Railgun000/p/10561149.html
Copyright © 2011-2022 走看看