当前位置:网站首页>6134. Find the closest node to the given two nodes - force double hundred code
6134. Find the closest node to the given two nodes - force double hundred code
2022-08-01 23:10:00 【Mr Gao】
6134. 找到离给定两个节点最近的节点
给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,每个节点 至多 有一条出边.
有向图用大小为 n 下标从 0 开始的数组 edges 表示,表示节点 i 有一条有向边指向 edges[i] .如果节点 i 没有出边,那么 edges[i] == -1 .
同时给你两个节点 node1 和 node2 .
请你返回一个从 node1 和 node2 都能到达节点的编号,使节点 node1 和节点 node2 到这个节点的距离 较大值最小化.如果有多个答案,请返回 最小 的节点编号.如果答案不存在,返回 -1 .
注意 edges 可能包含环.
示例 1:
输入:edges = [2,2,3,-1], node1 = 0, node2 = 1
输出:2
解释:从节点 0 到节点 2 的距离为 1 ,从节点 1 到节点 2 的距离为 1 .
两个距离的较大值为 1 .我们无法得到一个比 1 更小的较大值,所以我们返回节点 2 .
示例 2:
输入:edges = [1,2,-1], node1 = 0, node2 = 2
输出:2
解释:节点 0 到节点 2 的距离为 2 ,节点 2 到它自己的距离为 0 .
两个距离的较大值为 2 .我们无法得到一个比 2 更小的较大值,所以我们返回节点 2 .
解题代码如下:
void dfs( int* edges, int edgesSize, int node,int *dnode,int d,int *r){
r[node]++;
if(d<dnode[node]&&r[node]<=1){
dnode[node]=d;
}
if(edges[node]!=-1&&r[node]<2){
dfs(edges,edgesSize,edges[node],dnode,d+1,r);
}
}
void dfs2( int* edges, int edgesSize, int node,int *dnode,int d,int *r){
r[node]++;
// printf("ndoe %d ",node);
if(d>dnode[node]&&r[node]<=1){
dnode[node]=d;
}
// printf("edges[node] r[node] %d %d ",edges[node],r[node]);
if(edges[node]!=-1&&r[node]<2){
// printf("dfasfsd");
dfs2(edges,edgesSize,edges[node],dnode,d+1,r);
// printf(" sdfasfsd");
}
}
int closestMeetingNode(int* edges, int edgesSize, int node1, int node2){
int *dnode=(int *)malloc(sizeof(int )*edgesSize);
int *r=(int *)malloc(sizeof(int )*edgesSize);
int *r2=(int *)malloc(sizeof(int )*edgesSize);
int i;
for(i=0;i<edgesSize;i++){
dnode[i]=1000000;
r[i]=0;
r2[i]=0;
}
dfs(edges,edgesSize,node1,dnode,0,r);
dfs2(edges,edgesSize,node2,dnode,0,r2);
int min=10000000;
for(i=0;i<edgesSize;i++){
if(dnode[i]<min&&r[i]>=1&&r2[i]>=1){
min=dnode[i];
}
}
//printf("min %d ",min);
for(i=0;i<edgesSize;i++){
if(dnode[i]==min&&r[i]>=1&&r2[i]>=1){
return i;
}
}
return -1;
}
边栏推荐
- 如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
- Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- 简单3D渲染器的制作
- 移动端人脸风格化技术的应用
- 复现gallerycms字符长度限制短域名绕过
- A. Doremy‘s IQ-- Codeforces Round #808 (Div. 1)
- 1391D. 505 状压dp
- SQL Server(设计数据库--存储过程--触发器)
- Go 微服务开发框架DMicro的设计思路
猜你喜欢
Quarantine and downgrade
Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
【SeaTunnel】从一个数据集成组件演化成企业级的服务
Prufer sequence
简单3D渲染器的制作
10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
华为无线设备配置双链路冷备份(AP指定配置方式)
Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
(Translation) How the contrasting color of the button guides the user's actions
随机推荐
从0到1:图文投票小程序设计与研发笔记
Chapter 11 Working with Dates and Times
PDF转Word有那么难吗?做一个文件转换器,都解决了
excel vertical to horizontal
The monthly salary of the test post is 5-9k, how to increase the salary to 25k?
分享10套开源免费的高品质源码,免费源码下载平台
从0到100:招生报名小程序开发笔记
chrome copies the base64 data of an image
对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
(Translation) How the contrasting color of the button guides the user's actions
数据库表设计规则
别看了,这就是你的题呀
PAM Palindromic Automata
excel remove all carriage return from a cell
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
Use Jenkins for continuous integration, this knowledge point must be mastered
APP special test: traffic test
vscode hide menu bar
What is CICD excuse me
计算两点之间的距离