当前位置:网站首页>L2-031 深入虎穴 (25 分)
L2-031 深入虎穴 (25 分)
2022-06-29 09:23:00 【陌陌623】
本次刷题为了2021年天梯
L2-031 深入虎穴 (25 分)
著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报。已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门。每一扇门背后或者是一个房间,或者又有很多条路,同样是每条路通向一扇门…… 他的手里有一张表格,是其他间谍帮他收集到的情报,他们记下了每扇门的编号,以及这扇门背后的每一条通路所到达的门的编号。007 发现不存在两条路通向同一扇门。
内线告诉他,情报就藏在迷宫的最深处。但是这个迷宫太大了,他需要你的帮助 —— 请编程帮他找出距离入口最远的那扇门。
输入格式:
输入首先在一行中给出正整数 N(<105),是门的数量。最后 N 行,第 i 行(1≤i≤N)按以下格式描述编号为 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
题解
发现自己的代码比之前变得优美
写的也更流畅了
确实比去年天梯的时候有很大进步
加油 崇文
Code
BFS
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int n = 1e5 + 10;
int dp[n];
vector<int> v[n];
void bfs(int i)
{
queue<int> q;
q.push(i);
int ans = 1;
while (!q.empty())
{
auto x = q.front();
q.pop();
ans = x;
for(auto &e : v[x])
q.push(e);
}
cout << ans;
}
int main()
{
int N, M, x;
cin >> N;
for (int i = 1; i <= N; i++)
{
cin >> M;
while (M--)
cin >> x, v[i].push_back(x), dp[x]++;
}
for (int i = 1; i <= N; i++)
if (!dp[i])
bfs(i);
return 0;
}
DFS
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int n = 1e5 + 10;
int dp[n];
vector<int> v[n];
int ans = 1;
int maxt = 0;
void dfs(int k, int level)
{
if (maxt < level)
ans = k, maxt = level;
for (auto &e : v[k])
dfs(e, level + 1);
}
int main()
{
int N, M, x;
cin >> N;
for (int i = 1; i <= N; i++)
{
cin >> M;
while (M--)
cin >> x, v[i].push_back(x), dp[x]++;
}
for (int i = 1; i <= N; i++)
if (!dp[i])
dfs(i, 0);
cout << ans;
return 0;
}
边栏推荐
- JVM之虚拟机栈之动态链接
- Pointer functions and function pointers
- Codeforces Round #641 Div2
- Install and configure redis in the Linux environment, and set the boot auto start
- Symphony tutorial
- C语言中通过sprintf()函数构造sql语句
- Shanke's C language 2018 exercise (Telecom)
- RecyclerView刷新闪烁与删除Item时崩溃问题
- C语言实现一种创建易管理易维护线程的方法
- EDA and VHDL question bank
猜你喜欢

EDA and VHDL question bank

Application of decorator mode, packaging ServletRequest and adding addparameter method

完美二叉树、完全二叉树、完满二叉树

nacos注册中心集群

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

力扣94二叉树的中序遍历

KDevelop new project

力扣85题最大矩形

自定义控件之侧滑关闭 Activity 控件

Flutter 基础组件之 GridView
随机推荐
leetcode MYSQL数据库题目181
manacher
CodeForces - 1151B 思维
Leetcode MySQL database topic 178
Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
C语言中通过sprintf()函数构造sql语句
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
内网穿透工具frp使用入门
自定义控件之下载控件1(DownloadView1)
Minorgc, majorgc, fullgc
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
单片机集成开发环境Keil5的使用
Codeforces - 1151b thinking
XML布局中Button总是在最上层显示
共用体Union
Middle order traversal of Li Kou 94 binary tree
SeaweedFS安全配置(Security Configuration)
JVM之方法的绑定机制
leetcode MYSQL数据库题目178
2020-09-21 referer字符串切分 boost gateway代码组织层次