Pagini recente » Cod sursa (job #1422150) | Cod sursa (job #1703446) | Cod sursa (job #1077676) | Cod sursa (job #2789649) | Cod sursa (job #2788870)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define VMAX 100000
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector <int> adj[VMAX];
int d[VMAX];
bool visited[VMAX];
int V, x, y;
pair <int, int> furthestNode(int starting_point)
{
int u, last = starting_point;
queue <int> q;
for (int i = 0; i < V; ++i) d[i] = 0, visited[i] = false;
q.push(starting_point);
d[starting_point] = 1;
visited[starting_point] = true;
while (!q.empty())
{
u = q.front(), q.pop();
for (auto w:adj[u])
if (visited[w] == false)
{
visited[w] = true;
d[w] = d[u] + 1;
last = w;
q.push(w);
}
}
return {last, d[last]};
}
int main()
{
fin >> V;
for (int i = 0; i < V - 1; ++i)
{
fin >> x >> y;
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
auto furthest_from_origin = furthestNode(0);
fout << furthestNode(furthest_from_origin.first).second;
fin.close();
fout.close();
return 0;
}