• cf B. Resort


    http://codeforces.com/contest/350/problem/B

    从旅馆开始倒着找到一个点它的出度>1的位置为止,比较长度大小,找到一个长度最大的即可。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 100010
     5 using namespace std;
     6 
     7 int n;
     8 int a[maxn],b[maxn];
     9 int out[maxn];
    10 int k;
    11 
    12 void print(int x)
    13 {
    14     if(b[x]&&out[b[x]]==1)
    15     {
    16         print(b[x]);
    17         printf(" %d",x);
    18     }
    19     else
    20     {
    21         printf("%d",x);
    22         return ;
    23     }
    24 }
    25 int main()
    26 {
    27     while(scanf("%d",&n)!=EOF)
    28     {
    29         for(int i=1; i<=n; i++)
    30         {
    31             scanf("%d",&a[i]);
    32         }
    33         for(int j=1; j<=n; j++)
    34         {
    35             scanf("%d",&b[j]);
    36             out[b[j]]++;
    37         }
    38         int max1=0;
    39         for(int i=1; i<=n; i++)
    40         {
    41             if(a[i])
    42             {
    43                 int t1=1;
    44                 for(int j=i; b[j]!=0&&out[b[j]]==1; j=b[j])
    45                 {
    46                     t1++;
    47                 }
    48                 if(max1<t1)
    49                 {
    50                     max1=t1;
    51                     k=i;
    52                 }
    53             }
    54         }
    55         printf("%d
    ",max1);
    56         print(k);
    57         printf("
    ");
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    POJ2182Lost Cows
    BZOJ4003: [JLOI2015]城池攻占
    POJ1635Subway tree systems
    BZOJ1005: [HNOI2008]明明的烦恼
    POJ1182 NOI2001 食物链
    栈的链式实现
    栈的数组实现
    链表ADT的实现
    #ifndef的用法
    using namespace std
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3927968.html
Copyright © 2020-2023  润新知