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\n",max1);
56         print(k);
57         printf("\n");
58     }
59     return 0;
60 }
View Code

 

转载于:https://www.cnblogs.com/fanminghui/p/3927968.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值