-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdiameter_of_a_tree.cpp
More file actions
49 lines (42 loc) · 840 Bytes
/
diameter_of_a_tree.cpp
File metadata and controls
49 lines (42 loc) · 840 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define SIZE 100001
vector< vector<int> >arr(SIZE, vector<int>());
vector< int >depth(SIZE);
vector< int >diameter_path;
void dfs(int from, int to)
{
depth[to]=depth[from]+1;
for(auto x:arr[to])
{
if(x!=from)
dfs(to,x);
}
}
void add_path(int from, int to, int last, vector<int>& v)
{
if(diameter_path.size() > 0) return;
v.push_back(to);
if(to == last)
{
diameter_path.resize(v.size());
for(int i=0;i<v.size();i++) diameter_path[i]=v[i];
return;
}
for(auto x:arr[to])
{
if(x!=from)
add_path(to,x,last,v);
}
v.pop_back();
}
void get_diameter()
{
int leaf1,leaf2;
dfs(0,1);
leaf1=max_element(depth.begin(),depth.end())-depth.begin();
dfs(0,leaf1);
leaf2=max_element(depth.begin(),depth.end())-depth.begin();
vector<int>v;
add_path(0, leaf1, leaf2, v);
}