L2-031 深入虎穴

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

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

输入格式:

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

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

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

输出格式:

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

输入样例:

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

输出样例:

12

比赛的时候没看出问题来,第一次以1点为起点得了17然后让每个点跑了次dfs得了20,当时好像是加了dist数组记录的长度,所以都跑多次也没全对(应该是)

题是很普通的一个dfs,但是坑在两个地方

一:没有告诉你起点,不能也没想就直接走的1,因为序号连续,所以算一下前N项和,减去每次到过的门,剩下的就是起始点

二:不是坑点,而是做题时自己不严谨的点,估计测试数据是

1
1 0

如果是像我这样在循环时直接判断最大值,那么最大值初始一定要为-1,反正不能为0

#include <vector>
#include <iostream>
using namespace std;
static const auto io_sync_off = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return nullptr;
}();

const int maxn = 100005;
vector<int> ver[maxn];
int N, ans, maxd = -1;//赋值负的

void dfs(int cur, int step)
{
    if (ver[cur].size() == 0)//或者维护一个dist数组记录距离
    {
        if (step > maxd)
        {
            maxd = step;
            ans = cur;
        }
        return;
    }
    for (int i = 0; i < ver[cur].size(); ++i)
        dfs(ver[cur][i], step + 1);
}
int main()
{
    int a, k;
    cin >> N;
    int root = (N + 1) * N / 2;//所有门的总和
    for (int i = 1; i <= N; ++i)
    {
        cin >> k;
        while (k--)
        {
            cin >> a;
            root -= a;			//减去出现过的门
            ver[i].push_back(a);
        }
    }
    dfs(root, 0);
    cout << ans;
    return 0;
}
### L2-031 深入虎穴 Java 解决方案 为了实现这个问题,可以采用深度优先搜索(DFS)来遍历整个图结构并记录每个节点的最大深度。以下是详细的解决方案: #### 数据结构定义 使用 `ArrayList<Integer>[]` 来表示邻接表形式的图,其中每一个索引代表一个门,而对应的列表则保存可以从该门到达的所有其他门。 ```java import java.util.*; public class Main { static ArrayList<Integer>[] adjList; static boolean[] visited; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = Integer.parseInt(scanner.nextLine().trim()); // 初始化邻接表和访问标记数组 adjList = new ArrayList[N + 1]; for (int i = 1; i <= N; ++i) { adjList[i] = new ArrayList<>(); } visited = new boolean[N + 1]; String[] parts; for (int i = 1; i <= N; ++i) { parts = scanner.nextLine().split("\\s+"); int count = Integer.parseInt(parts[0]); if (count != 0) { for (int j = 1; j <= count; ++j) { adjList[i].add(Integer.parseInt(parts[j])); } } } // 寻找根节点 Set<Integer> roots = findRoot(N); // 执行 DFS 并计算最大深度 dfs(roots.iterator().next(), 0, 0); System.out.println(maxDepth + " " + farthestNode); } private static Set<Integer> findRoot(int N) { BitSet isNotRoot = new BitSet(); for (int i = 1; i <= N; ++i) { for (Integer neighbor : adjList[i]) { isNotRoot.set(neighbor); } } Set<Integer> result = new HashSet<>(); for (int i = 1; i <= N; ++i) { if (!isNotRoot.get(i)) { result.add(i); } } return result; } static int maxDepth = -1; static int farthestNode = -1; private static void dfs(int node, int depth, int parent) { visited[node] = true; if (depth > maxDepth) { maxDepth = depth; farthestNode = node; } for (Integer next : adjList[node]) { if (next == parent || visited[next]) continue; dfs(next, depth + 1, node); } } } ``` 此程序首先读取输入数据构建图形结构,并查找唯一的根节点[^1]。接着利用递归方式执行深度优先搜索以探索所有可能路径,在过程中更新最深位置及其对应的距离值[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值