Pagini recente » Borderou de evaluare (job #1409087) | Borderou de evaluare (job #2178749) | Cod sursa (job #1970479) | Cod sursa (job #915561) | Cod sursa (job #2243523)
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int LIM = 100000;
bool beenThere[LIM+1];
int theDistance[LIM+1];
vector < int > myVector[LIM+1];
int theFirstDFSMaxDistance;
int theSecondDFSMaxDistance;
int theFirstNode;
int N,M;
void DFS(int theNode , int theDistance)
{
if(theDistance > theFirstDFSMaxDistance)
{
theFirstDFSMaxDistance = theDistance;
theFirstNode = theNode;
}
beenThere[theNode] = true;
for(auto V : myVector[theNode])
{
if(beenThere[V] == false)
{
DFS(V,theDistance+1);
}
}
}
int main()
{
in >> N;
for ( int i = 1; i < N ; ++i)
{
int x,y;
in >> x >> y;
myVector[x].push_back(y);
myVector[y].push_back(x);
}
DFS(1,0);
memset(beenThere,false,sizeof(beenThere));
theFirstDFSMaxDistance = 0;
DFS(theFirstNode,0);
out << theFirstDFSMaxDistance + 1;
}