Pagini recente » Cod sursa (job #2714028) | Cod sursa (job #2089591) | Cod sursa (job #760249) | Cod sursa (job #551027) | Cod sursa (job #2246042)
#include <fstream>
#include <queue>
#include <stack>
#include <tuple>
#include <list>
#include <algorithm>
//#include <iostream>
std::ifstream fin("darb.in");
std::ofstream fout("darb.out");
std::pair<unsigned long, unsigned long> findDiameter(std::list<unsigned long> tree[], unsigned long nodeId)
{
auto max1 = 0UL;
auto max2 = 0UL;
std::for_each(tree[nodeId].cbegin(), tree[nodeId].cend(),[&tree, &max1, &max2](auto id){
const auto p = findDiameter(tree, id);
if(p.first > max1)
{
max2 = max1;
max1 = p.first;
}
else
{
if(p.first > max2)
{
max2 = p.first;
}
}
});
const auto diameter = max1 + max2 + 1;
//std::cout << "max1:" << max1 << ' ' << "max2:" << max2 << "diameter:" << diameter <<'\n';
return std::make_pair(max1+1, diameter);
}
int main()
{
auto n = 0UL;
auto x = 0UL;
auto y = 0UL;
fin>>n;
std::list<unsigned long> tree[n+1];
while(true)
{
fin >> x;
fin >> y;
if( fin.eof() ) break;
tree[x].push_back(y);
}
const auto p = findDiameter(tree, 1);
fout << p.second <<'\n';
return 0;
}