牛牛想知道,他是否能从
号格子出发回到
号格子。
若能回到 号格子则返回Yes,否则返回No。
若能回到 号格子则返回Yes,否则返回No。
[4, 4],[(1,2), (2, 3), (3,4),(4,1)]
"Yes"
m对 u, v 互不相同
/*
* function Point(a, b){
* this.x = a || 0;
* this.y = b || 0;
* }
*/
/**
* 能回到1号点返回 Yes,否则返回 No
* @param param int整型一维数组 param[0] 为 n,param[1] 为 m
* @param edge Point类一维数组 Point.x , Point.y 分别为一条边的两个点
* @return string字符串
*/
var visited = {};
var flag = true;
function solve( param , edge ) {
// write code here
let map = {};
let reachAble = false;
edge.forEach((el)=>{
if(map[el.x] === undefined){
map[el.x] = [el.y];
}else{
map[el.x].push(el.y);
}
if(!reachAble&&el.y===1){
reachAble = true;
}
})
if(!reachAble){
return "No";
}
if(map[1]){
dfs(1,map);
}else{
return "No";
}
if(!flag){
return "Yes";
}
return "No";
}
function dfs(postion,map){
if(postion === 1){
flag = false;
}
if(!flag){
return;
}
let paths = map[postion];
if(!paths){
return;
}
for(let i = 0; i<paths.length;i++){
let po = paths[i];
if(!visited[po]){
visited[po] = true;
dfs(po,map);
}
}
}
module.exports = {
solve : solve
};