Pagini recente » Cod sursa (job #248812) | Clasament | Cod sursa (job #63463) | Cod sursa (job #1682043) | Cod sursa (job #1435401)
#include <fstream>
#include <vector>
#include <queue>
#define vect vector< vector<int> >
using namespace std;
void read(vect &v)
{
ifstream f("darb.in");
int n, x, y;
f >> n;
v.resize(n+1);
for (int i = 1; i < n; ++i)
{
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
f.close();
}
int pathLongest(vect &v, vector<int> &dist, int start)
{
queue<int> Q;
int node;
fill(dist.begin(), dist.end(), -1);
dist[start] = 1;
Q.push(start);
while (!Q.empty())
{
node = Q.front();
Q.pop();
for (vector<int>::const_iterator it = v[node].begin(); it != v[node].end(); ++it)
{
if (dist[*it] == -1)
{
Q.push(*it);
dist[*it] = dist[node] + 1;
}
}
}
return node;
}
void solve(vect &v)
{
ofstream f("darb.out");
vector<int> dist(v.size());
int node;
node = pathLongest(v, dist, 1);
node = pathLongest(v, dist, node);
f << dist[node];
f.close();
}
int main()
{
vect v;
read(v);
solve(v);
return 0;
}