zoukankan      html  css  js  c++  java
  • [PTA]L2-031 深入虎穴 (25 分)

    L2-031 深入虎穴 (25 分)

    Description

    著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报。已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门。每一扇门背后或者是一个房间,或者又有很多条路,同样是每条路通向一扇门…… 他的手里有一张表格,是其他间谍帮他收集到的情报,他们记下了每扇门的编号,以及这扇门背后的每一条通路所到达的门的编号。007 发现不存在两条路通向同一扇门。

    内线告诉他,情报就藏在迷宫的最深处。但是这个迷宫太大了,他需要你的帮助 —— 请编程帮他找出距离入口最远的那扇门。

    Input

    输入首先在一行中给出正整数 N(<),是门的数量。最后 N 行,第 i 行(1)按以下格式描述编号为 i 的那扇门背后能通向的门:

    K D[1] D[2] ... D[K]
    

    其中 K 是通道的数量,其后是每扇门的编号。

    output

    在一行中输出距离入口最远的那扇门的编号。题目保证这样的结果是唯一的。

    Examples

    Input

    13
    3 2 3 4
    2 5 6
    1 7
    1 8
    1 9
    0
    2 11 10
    1 13
    0
    0
    1 12
    0
    0

    Output

    12

    注意:

    不知道入口是哪一个!

    正确解法:

    找入口

    按照bfs用邻接表遍历一边,然后找最远的那一个点。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cstring>
     5 #include<map>
     6 #include<set>
     7 #include<vector>
     8 #include<queue>
     9 #include<algorithm>
    10 #include<cmath>
    11 using namespace std;
    12 typedef long long ll;
    13 const int inf=0x7fffffff;
    14 const int N=100000+100;
    15 const int M=9999999;
    16 const ll mod=1000000000+7;
    17 int bok[N],n,k,que[N],x,cnt[N];
    18 int Link[N],len=0;
    19 struct node
    20 {
    21     int y,v,next;
    22 }e[N];
    23 void insert(int xx,int yy,int vv)
    24 {
    25     e[++len].next=Link[xx];
    26     Link[xx]=len;
    27     e[len].y=yy;
    28     e[len].v=vv;
    29 }
    30 void init()
    31 {
    32     scanf("%d",&n);
    33     for(int i=1;i<=n;i++)
    34     {
    35         scanf("%d",&k);
    36         while(k--)
    37         {
    38             scanf("%d",&x);
    39             bok[x]=1;
    40             insert(i,x,1);
    41         }
    42     }
    43 }
    44 void bfs(int x)
    45 {
    46     memset(bok,0,sizeof(bok));
    47     int head=1,tail=2;
    48     que[1]=x;
    49     bok[x]=1;
    50     cnt[x]=0;
    51     while(head<tail)
    52     {
    53         for(int i=Link[que[head]];i;i=e[i].next)
    54         {
    55             if(bok[e[i].y]==0)
    56             {
    57                 bok[e[i].y]=1;
    58                 que[tail++]=e[i].y;
    59                 cnt[e[i].y]=cnt[que[head]]+1;
    60             }
    61         }
    62         head++;
    63     }
    64     cout<<que[tail-1]<<endl;
    65 }
    66 int main()
    67 {
    68     init();
    69     for(int i=1;i<=n;i++)
    70         if(bok[i]==0)
    71         {
    72             //cout<<i<<endl;
    73             bfs(i);
    74             break;
    75         }
    76 
    77     return 0;
    78 }
    View Code
    No matter how you feel, get up , dress up , show up ,and never give up.
  • 相关阅读:
    在CentOS7上搭建MySQL主从复制与读写分离
    数据库 引擎
    数据库 事务
    数据库 索引
    MYSQL
    基于mysqld_multi实现MySQL 5.7.24多实例多进程配置
    09 引导过程与故障修复
    chapter 8.3
    作业 8.1
    Chapter 04 作业
  • 原文地址:https://www.cnblogs.com/Kaike/p/10661547.html
Copyright © 2011-2022 走看看