L2-031 深入虎穴 (25 分)

本文介绍了一种通过广度优先搜索(BFS)算法来找到树状结构中距离根节点最远的节点的方法。该算法首先确定根节点,然后进行一层层的遍历直到找到最底层的节点。

题目链接

题目大意与分析:先寻找根结底,然后广度优先搜索,寻找层次最大的结点。

提交代码:

#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;

int main(){
    int n,root,door;
    scanf("%d",&n);
    vector<vector<int> > v(n+2);
    vector<int>a(n+2,0);
    for(int i=1;i<=n;i++){
        int k,t;
        scanf("%d",&k);
        for(int j=1;j<=k;j++){
            scanf("%d",&t);
            v[i].push_back(t);
            a[t]=1;
        }
    }
    for(int i=1;i<=n;i++){
        if(a[i]==0){
            root=i;
            break;
        }
    }
    queue<int>q;
    q.push(root);
    door=root;
    while(!q.empty()){
        int t=q.front();
        q.pop();
        door=t;
        for(int i=0;i<v[t].size();i++){
            q.push(v[t][i]);
        }
    }
    printf("%d",door);
    return 0;
}

带注释代码:

#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;

int main(){
    int n,root,door;
    scanf("%d",&n);
    vector<vector<int> > v(n+2);
    vector<int>a(n+2,0);
    for(int i=1;i<=n;i++){
        int k,t;
        scanf("%d",&k);
        for(int j=1;j<=k;j++){
            scanf("%d",&t);
            v[i].push_back(t);
            a[t]=1;
        }
    }
    for(int i=1;i<=n;i++){ //寻找根结点
        if(a[i]==0){
            root=i;
            break;
        }
    }
    queue<int>q;
    q.push(root);
    door=root;
    while(!q.empty()){  //BFS
        int t=q.front();
        q.pop();
        door=t;
        for(int i=0;i<v[t].size();i++){
            q.push(v[t][i]);
        }
    }
    printf("%d",door);
    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]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值