-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(Tree) Tree Diameter
61 lines (55 loc) · 1.12 KB
/
(Tree) Tree Diameter
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
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const long long mod =1e9+7;
vector<vector<int> > adjList;
vector<bool> vis;
vector<int> parents;
int lastNode=1;
void bfs(int node){
vis[node]=true;
queue<int> q;
q.push(node);
while(!q.empty()){
int curr=q.front();
q.pop();
for(int neighbor: adjList[curr]){
if(!vis[neighbor]){
vis[neighbor]=true;
q.push(neighbor);
parents[neighbor]=curr;
lastNode=neighbor;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
adjList.resize(n+1);
vis.resize(n+1, false);
parents.resize(n+1, 0);
for(int i=1; i<n; i++){
int a, b;
cin>>a>>b;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
bfs(1);
int lN=lastNode;
for(int i=0; i<=n; i++){
vis[i]=false;
}
bfs(lN);
int fN=lastNode;
int x=fN;
int ans=0;
while(x!=lN){
x=parents[x];
ans++;
}
cout<<ans<<endl;
return 0;
}