forked from sunildinday/my-algo-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-diameter.cpp
65 lines (64 loc) · 1.39 KB
/
tree-diameter.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
/*---------------------------Dynamic Programming------------------------------
Diameter of tree
for each node of the tree we will find the height of each child then if the diameter passes through
a node then it will be the sum of the height of the two child which have maximum height
*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
std::vector<int> adj[100];
int diameter;//diameter of the tree
int height[101];
void def(int V,int pV)
{
std::vector<int> child;//used to get height of all the children's
for(auto v:adj[V])//dfs for child of V
{
if(v==pV)//if v is its parent then ignore it
continue;
dfs(v,V);
child.push_back(height[v]);//pushing the height of each child of V
}
height[V]=1;//intialising height of node V
if(adj[V].size()!=0)
height[V]=1+*max_element(child.begin(), child.end());//height of current node will be the maximum height of child
if(adj[V].size()>=2)
{
//utility func for finding two maximum element in child vector
int mx1,mx2;
int mx1=child[0],mx2=child[1];
if(mx1<mx2)
swap(mx1,mx2);
for(int i=2;i<child.size();i++)
{
if(child[i]<mx2)
continue;
if(child[i]>=mx1)
{
mx2=mx1;
mx1=val[i];
}
else
{
mx2=val[i];
}
}
diameter=max(1+mx1+mx2,diameter);
}
}
int main()
{
int n;
cin>>n;
int x,y;
for(int i=0;i<n;i++)
{
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1,0);
cout<<diameter<<endl;
return 0;
}