Pagini recente » Cod sursa (job #2354882) | Cod sursa (job #2700478) | Cod sursa (job #2675075) | Cod sursa (job #1861532) | Cod sursa (job #1440107)
#include <iostream>
#include <fstream>
#include <vector>
#define NMax 100001
using namespace std;
vector<int> Graf[NMax];
bool uz[NMax];
int d[NMax],x,y,Max,N,last;
void Citire()
{
ifstream g("darb.in");
g>>N;
for(int i=1;i<=N;i++)
{
g>>x>>y;
x--;
y--;
Graf[x].push_back(y);
Graf[y].push_back(x);
}
}
void DFS(int nod)
{
for(vector<int>::iterator it=Graf[nod].begin();it!=Graf[nod].end();it++)
{
if(uz[*it]==false)
{
uz[*it]=true;
d[*it]=d[nod]+1;
if(d[*it]>Max)
{
Max=d[*it];
last=*it;
}
DFS(*it);
}
}
}
int main()
{
Citire();
uz[x]=true;
d[x]=1;
DFS(x);
uz[last]=true;
d[last]=1;
DFS(last);
ofstream f("darb.out");
f<<Max;
}