当前位置:网站首页>[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
2022-08-01 23:20:00 【wow kaka negative negative positive positive】
LeetCode304周赛
https://leetcode.cn/contest/weekly-contest-304/
Record two unwritten questions
Reference explanation:Y总
这是一个基环树问题,Just a bunch of trees hanging from a ring.
LeetCode 6134. 找到离给定两个节点最近的节点
https://leetcode.cn/problems/find-closest-node-to-given-two-nodes/
解决思路:分别从 node1
和 node2
开始,Record each node to node1
和 node2
的距离,If there is a loop or it ends at the end.然后遍历节点,If you want the smallest max(d1[i], d2[i])
的节点 i
.
class Solution {
public:
int closestMeetingNode(vector<int>& p, int x, int y) {
int n = p.size(); // 记录点数
vector<int> d1(n, -1); // Record every arrival x 的距离
vector<int> d2(n, -1); // Record every arrival y 的距离
d1[x] = d2[y] = 0;
while (p[x] != -1) {
if (d1[p[x]] != -1) {
// 已经遍历过,跳出循环
break;
}
d1[p[x]] = d1[x] + 1; // 节点距离 + 1
x = p[x];
}
while (p[y] != -1) {
if (d2[p[y]] != -1) {
// 已经遍历过,跳出循环
break;
}
d2[p[y]] = d2[y] + 1; // 节点距离 + 1
y = p[y];
}
int ans = -1, id = -1;
for (int i = 0; i < n; i++) {
int a = d1[i], b = d2[i];
if (a != -1 && b != -1) {
if (ans == -1 || max(a, b) < ans) {
// If a node distance x 和 y 更近,记录为 ans
ans = max(a, b);
id = i;
}
}
}
return id;
}
};
LeetCode 6135. 图中的最长环
https://leetcode.cn/problems/longest-cycle-in-a-graph/
解决思路:Number of loop nodes n
次,每次从第 i
个节点开始,If there is a loop or it ends at the end,Record the length you can walk.最终结果就是取 n
The longest route length that a node can travel from the beginning.
class Solution {
public:
vector<int> p;
vector<bool> st; // Stores whether each point has been searched
vector<int> in_stk; // is on the stack,深度为多少
int ans = -1;
void dfs(int x, int depth){
st[x] = true; // 表示被遍历过
in_stk[x] = depth; // 加入栈,Marked as the depth of the stack
int next = p[x]; // 搜索 x 的下一个节点
if (next != -1) {
if (!st[next]) {
// not searched
dfs(j, depth + 1);
} else if (in_stk[next]) {
//被搜索过,在栈中,有环
ans = max(ans, depth + 1 - in_stk[next]); // 深度更新
}
}
in_stk[x] = 0; // 出栈,标记清空
}
int longestCycle(vector<int>& p) {
this->p = p;
int n = p.size(); // 节点个数
st = vector<bool>(n);
in_stk = vector<int>(n);
for (int i = 0; i < n; i++) {
// 遍历所有点
if (!st[i]) {
// If the current point has not been searched
dfs(i, 1);
}
}
return ans;
}
};
边栏推荐
- excel change cell size
- 2022/7/31
- 文件查询匹配神器 【glob.js】 实用教程
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
- excel vertical to horizontal
- System availability: 3 9s, 4 9s in SRE's mouth... What is it?
- 如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
- 解决端口占用
- Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D Solution
- Oracle 数据库设置为只读及读写
猜你喜欢
随机推荐
域名重定向工具 —— SwitchHosts 实用教程
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
Avoid hidden text when loading fonts
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
复现gallerycms字符长度限制短域名绕过
PostgreSQL 基础--常用命令
Check if point is inside rectangle
Solve the port to take up
解决端口占用
D - Linear Probing- 并查集
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
vscode hide menu bar
A. Doremy‘s IQ-- Codeforces Round #808 (Div. 1)
[C language advanced] file operation (2)
部门项目源码分享
【参营经历贴】2022网安夏令营
Access the selected node in the console
Chapter 12 End-User Task As Shell Scripts
Always use "noopener" or "noreferrer" for links that open in a new tab
Nacos配置中心之加载配置