当前位置:网站首页>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;
}
边栏推荐
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
- npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
- IDEA入门看这一篇就够了
- C语言——分支语句和循环语句
- B. Difference Array--Codeforces Round #808 (Div. 1)
- 文件查询匹配神器 【glob.js】 实用教程
- Calculate the angle of a line defined by two points
- vscode hide menu bar
- perspectiveTransform warpPerspective getPerspectiveTransform findHomography
猜你喜欢

Prufer sequence

【SeaTunnel】从一个数据集成组件演化成企业级的服务

Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile

JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method

C#大型互联网平台管理框架源码:基于ASP.NET MVC+EF6+Bootstrap开发,支持多数据库

【数据分析03】

From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program

Go 微服务开发框架DMicro的设计思路

drf生成序列化类代码

ROS2初级知识(8):Launching启动多节点
随机推荐
数据库表设计规则
牛客多校4 A.Task Computing 思维
选择合适的 DevOps 工具,从理解 DevOps 开始
Is TCP reliable?Why?
6133. Maximum number of packets
excel change cell size
避免使用 <b>、<i>、<s> 和 <u> 标签
qt-faststart installation and use
excel split text into different rows
03. GO language variable definition, function
添加大量元素时使用 DocumentFragments
检查点是否在矩形内
如何使用pywinauto和pyautogui将动漫小姐姐链接请回家
npm npm
【数据分析03】
用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
Three, mysql storage engine - building database and table operation
B. Difference Array--Codeforces Round #808 (Div. 1)
Deep learning Course2 first week Practical aspects of Deep Learning exercises
【参营经历贴】2022网安夏令营