Skip to content

Instantly share code, notes, and snippets.

@deadkff01
Created February 20, 2016 18:49
Show Gist options
  • Select an option

  • Save deadkff01/8b48067f857bab46ad21 to your computer and use it in GitHub Desktop.

Select an option

Save deadkff01/8b48067f857bab46ad21 to your computer and use it in GitHub Desktop.
Teads Sponsored Contest - Solution
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: [] };
}
graph[xi].edges.push(yi);
if (!graph[yi]) {
graph[yi] = { data: yi, edges: [] };
}
graph[yi].edges.push(xi);
//printErr(xi+' <---> '+yi);
}
var max_depth = 0;
function deeepth() {
graph.map(function(x){
if(x.depth > max_depth) {
max_depth = x.depth;
}
if (x.depth == max_depth) {
xi = x.data;
}
});
return max_depth;
}
function explore_graph() {
graph.map(function(x){
x.explored = false;
x.pred = -1;
x.depth = Number.MAX_VALUE;
});
}
function bfs(n) {
explore_graph();
var q = [];
graph[n].explored = true;
graph[n].depth = 0;
q.push(n);
var x = null;
while(q.length !== 0) {
x = q.splice(0,1)[0];
for(var i = 0; i < graph[x].edges.length; i++) {
if (graph[graph[x].edges[i]].explored === false) {
graph[graph[x].edges[i]].explored = true;
graph[graph[x].edges[i]].pred = x;
graph[graph[x].edges[i]].depth = graph[x].depth + 1;
q.push(graph[x].edges[i]);
}
}
}
deeepth();
}
bfs(xi);
bfs(xi);
print(Math.ceil(max_depth / 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment