Created
February 25, 2016 21:23
-
-
Save deadkff01/d45a6d7530dcb6f3089c to your computer and use it in GitHub Desktop.
Dwarfs standing on the shoulders of giants - Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Auto-generated code below aims at helping you parse | |
* the standard input according to the problem statement. | |
**/ | |
var graph = []; | |
var n = parseInt(readline()); // the number of adjacency relations | |
for (var i = 0; i < n; i++) { | |
var inputs = readline().split(' '); | |
var xi = parseInt(inputs[0]); // the ID of a person which is adjacent to yi | |
var yi = parseInt(inputs[1]); // the ID of a person which is adjacent to xi | |
if (!graph[xi]) { | |
graph[xi] = { data: xi, edges: [], depth: 1}; | |
} | |
if (!graph[yi]) { | |
graph[yi] = { data: yi, edges: [], depth: 1}; | |
} | |
printErr(xi+'---'+yi); | |
graph[xi].edges.push(graph[yi]) | |
} | |
/*//non-optimized solution | |
function explore_graph(node){ | |
cur_node = graph[node]; | |
for(var i in cur_node.edges){ | |
if(cur_node.depth < (graph[cur_node.edges[i]].depth + 1)){ | |
graph[node].depth +=1; | |
explore_graph(node); | |
} | |
} | |
} | |
*/ | |
function explore(n, d) { | |
if (!n.edges.length) { | |
return d; | |
} else { | |
var max = 0; | |
n.edges.map(function(w) { | |
var result = explore(w, d + 1); | |
graph[w.data].depth = result; | |
if (result > max) { | |
max = result; | |
} | |
}); | |
return max; | |
} | |
} | |
graph.map(function(w) { | |
explore(w, 1); | |
}); | |
// Write an action using print() | |
// To debug: printErr('Debug messages...'); | |
print(Math.max(...graph.map(w => w.depth).filter(n => true))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment