Pagini recente » Cod sursa (job #2336281) | Cod sursa (job #173595) | Cod sursa (job #146674) | Cod sursa (job #3256575) | Cod sursa (job #1630304)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
/*#include <stdlib.h>
#include <time.h>*/
#define maxN 100001
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
int n, dist[maxN];
vector <int> Tree[maxN];
void readTree()
{
int a,b;
fin>>n;
while (fin>>a>>b)
{
Tree[a].push_back(b);
Tree[b].push_back(a);
}
}
void BFS(int &node)
{
queue <int> Q;
Q.push(node);
dist[node]=1;
while (!Q.empty())
{
node=Q.front();
Q.pop();
for (int i=0; i<Tree[node].size(); ++i)
{
int nb=Tree[node][i];
if (!dist[nb])
{
dist[nb]=dist[node]+1;
Q.push(nb);
}
}
}
}
int main()
{
readTree();
//srand(time(NULL));
int Node = 1;//rand() % n + 1;
BFS(Node);
fill(dist+1, dist+n+1, 0);
BFS(Node);
fout<<dist[Node];
return 0;
}