Pagini recente » Cod sursa (job #2137135) | Cod sursa (job #1119647) | Cod sursa (job #1705845) | Cod sursa (job #557036) | Cod sursa (job #2570271)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin ("darb.in");
ofstream fout ("darb.out");
const int nmax=100005;
int n,grad[nmax],dist[nmax],rez;
bool viz[nmax];
vector <int> muchii[nmax];
void dfs(int nod)
{
viz[nod]=true;
for (int i=0;i<muchii[nod].size();i++)
{
int vecin=muchii[nod][i];
if (!viz[vecin])
{
dist[vecin]=dist[nod]+1;
dfs(vecin);
}
}
}
int main()
{
fin >> n;
for (int i=1;i<n;i++)
{
int x,y;
fin >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
grad[x]++;
}
dist[3]=1;
dfs(3);
int maxx=0,fr=1;
for (int i=1;i<=n;i++)
{
if (dist[i]>maxx)
{
fr=i;
maxx=dist[i];
}
}
maxx=0;
for (int i=1;i<=n;i++)
{
viz[i]=false;
dist[i]=0;
}
dist[fr]=1;
dfs(fr);
for (int i=1;i<=n;i++)
{
maxx=max(dist[i],maxx);
}
fout << maxx;
}