-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlargest_distance_between_nodes_of_a_tree.cpp
68 lines (68 loc) · 1.66 KB
/
largest_distance_between_nodes_of_a_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
pair <int,int> bfs(int x, unordered_map <int, vector <int>> mp,int n)
{
vector <int> dist(n,-1); //initialize dist from node x to all nodes = -1
queue <int> q;
q.push(x);
dist[x] = 0; //distance from x to x
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v: mp[u])
{
//v is vertex reachable from node u
if(dist[v]==-1)
{
q.push(v); // push unvisited nodes in queue
dist[v] = dist[u] + 1; // increment distance of node v
}
}
}
int max_dist = INT_MIN;
int max_idx = 0;
for(int i=0; i<dist.size();i++)
{
if(dist[i]>max_dist)
{
max_idx = i;
max_dist = dist[i];
}
}
return make_pair(max_idx, max_dist);
// return make_pair(1,1);
}
int solve(vector<int> &A) {
unordered_map <int, vector <int>> mp;
int start;
int n = A.size();
for(int i=0; i<A.size();i++)
{
if(A[i]==-1)
{
start = i;
continue;
}
mp[A[i]].push_back(i);
mp[i].push_back(A[i]);
}
// for(int x:mp[3])
// {
// cout<<x<<" ";
// }
pair <int,int> p1,p2;
p1 = bfs(start,mp,n); //longest distance from root. p1 will contain that node x, maxdist
cout<<p1.first<<" "<<p1.second<<endl;
p2 = bfs(p1.first,mp,n); //max distance will be max distance from node x
cout<<p2.first<<" "<<p2.second;
return p2.second;
// return 1;
}
int main()
{
vector <int> A{-1, 0, 0, 0, 3};
int ans = solve(A);
cout<<ans<<endl;
return 0;
}