L2-031 深入虎穴 (25 分) Java题解 (树的最大深度dfs,bfs)
输入样例:
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
解题思路:
找最大深度的编号,由于答案唯一,所以宽搜到的最后一个结点就是bfs扩展最远的点;也可以深搜,通过递归左右儿子更新最大深度时更新结点编号,但dfs有两个点没过,找不出原因。
Java代码:
import java.io.*;
import java.util.*;public class Main {static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));public static int ini() throws IOException {st.nextToken();return (int)st.nval;}static int N = 100005, n, idx, maxd, maxid;static int[]e = new int[N];static int[]ne = new int[N];static int[]h = new int[N];static boolean[]vis = new boolean[N];public static void main(String[] args) throws IOException {n = ini();Arrays.fill(h, -1);for(int i = 1; i <= n; i++) {int m = ini();for(int j = 0; j < m; j++) {int t = ini();vis[t] = true;add(i, t);}}for(int i = 1; i <= n; i++) if(!vis[i])
// bfs(i); // 调用宽搜dfs(i, 1); // 调用深搜System.out.println(maxid);}public static void dfs(int u, int deep) {if(deep > maxd) {maxd = deep;maxid = u;}for(int i = h[u]; i != -1; i = ne[i]) {int j = e[i];dfs(j, deep + 1);}}public static void bfs(int root) {Queue<Integer> qu = new LinkedList<>();qu.add(root);int t = 0;while(!qu.isEmpty()) {t = qu.poll();for(int i = h[t]; i != -1; i = ne[i]) {int j = e[i];qu.add(j);}}System.out.println(t);}public static void add(int a, int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx++;}
}
dfs:
bfs
L2-031 深入虎穴 (25 分) Java题解 (树的最大深度dfs,bfs)相关推荐
- 1080 MOOC期终成绩 (25 分) java 题解
题目描述: 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作 ...
- 1007 Maximum Subsequence Sum (25 分) java 题解
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { N ...
- 1063 Set Similarity (25 分) java 题解
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt×100%, where Nc is t ...
- 1074 Reversing Linked List (25 分) java 题解
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- 【CCCC】L2-031 深入虎穴 (25分),,求多叉树最深的节点编号,大水题!!!
problem L2-031 深入虎穴 (25分) 著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一 ...
- 7-121 深入虎穴 (25 分)(dfs,bfs)
7-121 深入虎穴 (25 分) 著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一 ...
- 题目:L2-031 深入虎穴 (25 分)
题目:题目详情 - L2-031 深入虎穴 (25 分) (pintia.cn) 大意 已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一个房间,或 ...
- 天梯赛L2-3 深入虎穴 (25 分)
PTA L2-3 深入虎穴 (25 分) 著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或 ...
- L2-031 深入虎穴 (25 分)(DFS 代码有详细解析)
L2-031 深入虎穴 (25 分) 著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是 ...
最新文章
- 成功解决‘nvidia-smi‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
- java获得当前文件路径
- 《BI那点儿事》Microsoft 线性回归算法
- 在Hadoop集群上,搭建HBase集群
- C#正则表达式开源工具
- WIN7 系统破解LoadRunner 11
- 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草 区间dp
- cycleGAN有matlab代码吗,CycleGAN的代码组成
- 一道组合数学题-马拦过河卒,很精彩
- Foreach In(C#)
- topcon gps 软件下载
- Ubuntu16.04中好用的软件(持续更新)
- 【深度相机系列二】深度相机原理揭秘--飞行时间(TOF)
- 谈谈时间管理--陶哲轩
- 高品质摄影作图台式计算机推荐,摄影后期做图用什么电脑
- 高数_第3章重积分_三重积分之2__先一后二和先二后一
- ye我们胜利了的shooow
- 杭州旭航集团,申请纳斯达克IPO上市,募资9800万美元
- Python生成九宫格图片
- PHP:安装fileinfo扩展