当前位置:网站首页>6134. 找到离给定两个节点最近的节点-力扣双百代码
6134. 找到离给定两个节点最近的节点-力扣双百代码
2022-08-01 23:09: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;
}
边栏推荐
- visual studio code multiple editing
- 基于JAX的激活函数、softmax函数和交叉熵函数
- When using DocumentFragments add a large number of elements
- 避免使用 <b>、<i>、<s> 和 <u> 标签
- Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
- qt-faststart installation and use
- Postman batch test interface detailed tutorial
- 解决端口占用
猜你喜欢

隔离和降级

关于ETL的两种架构(ETL架构和ELT架构)

Solve the port to take up

测试岗月薪5-9k,如何实现涨薪到25k?

What is CICD excuse me

PHP算法之电话号码的字母组合

(Translation) How the contrasting color of the button guides the user's actions

文件查询匹配神器 【glob.js】 实用教程

解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”

从0到1:图文投票小程序设计与研发笔记
随机推荐
欧拉路径与欧拉回路
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
域名重定向工具 —— SwitchHosts 实用教程
PDF转Word有那么难吗?做一个文件转换器,都解决了
ROS2初级知识(8):Launching启动多节点
Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
隔离和降级
excel remove all carriage return from a cell
SRv6 L3VPN的工作原理
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
杭电多校3 1012. Two Permutations dp*
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
牛客多校4 A.Task Computing 思维
联邦学习的框架搭建
请问什么是 CICD
系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
Deep learning Course2 first week Practical aspects of Deep Learning exercises
excel clear format
Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"
2022/7/31