-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diameter_Of_Tree.cpp
82 lines (72 loc) · 1.67 KB
/
Diameter_Of_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Algo :
Step 1: select a random node A
Step 2: run bfs/dfs from A and find the farthest node say S
Step 3: run bfs/dfs from S and find the farthest node say D
Step 4: d(S, D) is the diameter of the tree
Refer : http://codeforces.com/blog/entry/22276 too
*/
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string.h>
#define MAX 100001
typedef long long int ll;
using namespace std
bool visited[MAX];
ll dist[MAX];
struct node {
ll dest;
ll weight;
};
vector<node> array1[MAX];
ll bfs(ll start,ll v) {
memset(visited,false,sizeof(visited));
memset(dist,0,sizeof(dist));
queue<ll> q;
q.push(start);
visited[start]=true;
dist[start]=0;
while(!q.empty()) {
ll s=q.front();
q.pop();
for(ll j=0;j<array1[s].size();j++) {
if(!visited[array1[s][j].dest]) {
visited[array1[s][j].dest]=true;
q.push(array1[s][j].dest);
dist[array1[s][j].dest]=array1[s][j].weight+dist[s];
}
}
}
return max_element(dist+1,dist+v+1)-dist;
}
int main(){
ll t;
cin>>t;
while(t--) {
ll v;
cin>>v;
for(int i=1;i<=v;i++)
array1[i].clear(); // this is an array that is compoed of vector units that compries of nodes.
for(ll i=1;i<v;i++) {// array[i] -> is a vector of nodes corresponding to ith node
ll a,b,w; // each node has to be pushed back onto the vector(i)
cin>>a>>b>>w;
node temp;
temp.dest=b;
temp.weight=w;
array1[a].push_back(temp);
temp.dest=a;
array1[b].push_back(temp);
}
/*for(int i=1;i<=v;i++)
{ for(int j=0;j<array[i].size();j++)
{ cout<<i<<" "<<array[i][j].dest<<" "<<array[i][j].weight<<endl;
}
}*/
ll index=bfs(1,v);
ll index2=bfs(index,v);
ll ans=dist[index2];
cout<<ans<<endl;
}
}