Pagini recente » Cod sursa (job #2177446) | Cod sursa (job #1063272) | Cod sursa (job #340679) | Cod sursa (job #2787945) | Cod sursa (job #2850874)
#include <bits/stdc++.h>
#define Dmax 100005
#define inf 0x3F3F3F3F
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
int n,viz[Dmax],dist[Dmax],diametru,last;
vector<int>G[Dmax];
queue<int> Q;
void BFS(int start)
{
for(int i = 1; i <= n; i++)
dist[i] = inf;
diametru = 0;
Q.push(start);
viz[start] = 1;
dist[start] = 1;
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(vector<int>::iterator it = G[nod].begin(); it < G[nod].end(); it++)
if(dist[*it] > dist[nod] + 1)
{
dist[*it] = dist[nod] + 1;
Q.push(*it);
if(diametru < dist[*it])
{
last = *it;
diametru = dist[*it];
}
}
}
}
int main()
{
f>>n;
int x,y;
while(f>>x>>y)
{
G[x].push_back(y);
G[y].push_back(x);
}
BFS(1);
BFS(last);
g<<diametru;
return 0;
}