-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPT07Z.cpp
More file actions
55 lines (55 loc) · 1005 Bytes
/
PT07Z.cpp
File metadata and controls
55 lines (55 loc) · 1005 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
50
51
52
53
54
55
#include<bits/stdc++.h>
using namespace std;
vector<long int>graph[10000];
long int n,xx;
long int maxx=INT_MIN;
void Do_DFS(long int countt,long int src,bool visited[])
{
visited[src]=true;
countt++;
vector < long int > ::iterator it;
for(it=graph[src].begin();it!=graph[src].end();it++)
{
if(!visited[*it])
{
if(countt>=maxx)
{
maxx=countt;
xx=*it;
}
Do_DFS(countt,*it,visited);
}
}
}
void DFS(long int src)
{
bool visited[n];
for(long int i=0;i<n;i++) visited[i]=false;
long int count=0;
Do_DFS(count,src,visited);
}
void find_diameter()
{
DFS(0);
DFS(xx);
}
void addedge(long int x,long int y)
{
graph[x].push_back(y);
graph[y].push_back(x);
}
int main()
{
//long int n;
cin>>n;
long int i;
long int x,y;
for(i=0;i<n-1;i++)
{
cin>>x>>y;
x--,y--;
addedge(x,y);
}
find_diameter();
cout<<maxx;
}