-
Notifications
You must be signed in to change notification settings - Fork 4
/
PT07Z.cpp
62 lines (59 loc) · 1.18 KB
/
PT07Z.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
//BISMILLAHIRRAHMANIRRAHIM
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#define pb push_back
#define MAX 10005
using namespace std;
int dist[MAX],mx;
bool vis[MAX];
vector <int> adj[MAX];
void dfs(int node)
{
int i;
vis[node]=1;
for(i=0;i<adj[node].size();i++)
{
if(!vis[adj[node][i]])
{
dist[adj[node][i]]=dist[node]+1;
dfs(adj[node][i]);
}
}
}
void solve()
{
int n,i,nd,mx=0;
cin>>n;
for(i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
adj[x].pb(y);
adj[y].pb(x);
}
memset(dist,0,sizeof(dist));
memset(vis,0,sizeof(vis));
dfs(1);
for(i=1;i<=n;i++)
{
if(mx<dist[i])
{
mx=dist[i];
nd=i;
}
}
memset(dist,0,sizeof(dist));
memset(vis,0,sizeof(vis));
dfs(nd);mx=0;
for(i=1;i<=n;i++)
if(mx<dist[i])
mx=dist[i];
cout<<mx<<endl;
}
int main()
{
solve();
}