Pagini recente » Cod sursa (job #327794) | Cod sursa (job #3202467) | Cod sursa (job #2739807) | Cod sursa (job #2975996) | Cod sursa (job #1984323)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
const int nMax = 100005;
int n;
vector<int> vecini[nMax];
bool viz[nMax];
int maxLength, startNode;
void citire()
{
ifstream in("darb.in");
in >> n;
int x, y;
for(int i = 1; i < n; ++i)
{
in >> x >> y;
vecini[x].push_back(y);
vecini[y].push_back(x);
}
}
void Dfs(int current, int length)
{
if(length > maxLength)
{
maxLength = length;
startNode = current;
}
viz[current] = true;
for(auto v:vecini[current])
{
if(viz[v] == false)
Dfs(v, length + 1);
}
}
void rezolvare()
{
Dfs(1, 1);
memset(viz, 0, sizeof(viz));
Dfs(startNode, 1);
ofstream out("darb.out");
out << maxLength;
out.close();
}
int main()
{
citire();
rezolvare();
return 0;
}