• L2-031 深入虎穴


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

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

    输入格式:

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

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

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

    输出格式:

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

    输入样例:

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

    输出样例:

    12
    1. #include <iostream>
    2. #include <vector>
    3. #include <set>
    4. #include <map>
    5. #include <string>
    6. #include <algorithm>
    7. #include <cstdlib>
    8. using namespace std;
    9. #define M 100000
    10. vector<int> arr[M + 5];
    11. int ans = 0, deep = 0;
    12. int n;
    13. set<int> s;
    14. void dfs(int i, int d) {
    15. if (d > deep) {
    16. deep = d;
    17. ans = i;
    18. }
    19. if (arr[i].empty()) return;
    20. for (auto x : arr[i]) {
    21. dfs(x, d + 1);
    22. }
    23. return;
    24. }
    25. int main() {
    26. cin >> n;
    27. for (int i = 1, a; i <= n; i++) {
    28. cin >> a;
    29. for (int j = 0, b; j < a; j++) {
    30. cin >> b;
    31. arr[i].push_back(b);
    32. s.insert(b);
    33. }
    34. }
    35. int root;
    36. for (int i = 1; i <= n; i++) {
    37. if (!s.count(i)) {
    38. root = i;
    39. break;
    40. }
    41. }
    42. dfs(root, 1);
    43. cout << ans;
    44. return 0;
    45. }

     

  • 相关阅读:
    vue 浏览器记住密码后,自动填充账号密码错位
    『力扣每日一题14』:消失的数字
    学生党高性价比蓝牙耳机有哪些?高性价比学生党蓝牙耳机推荐
    双指针算法
    PyQt5开发相关
    PB:自动卷滚条
    Spring Statement 状态机应用实例
    Stream强化
    TiDB 集群故障诊断
    Kafka常见问题解析
  • 原文地址:https://blog.csdn.net/Mz_yuner/article/details/133916762