Pagini recente » Cod sursa (job #530976) | Cod sursa (job #1431869) | Cod sursa (job #1921565) | Cod sursa (job #3259963) | Cod sursa (job #2581454)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define NMAX 100005
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
struct lan{
int y, dist;
};
vector<int>graph[NMAX];
queue<lan>Q;
int viz[NMAX];
int dist[NMAX];
int n, x, y;
void citire(){
f>>n;
for(int i=1; i<=n-1; i++){
f>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
lan bfs(int nod_start){
memset(viz, 0, sizeof(viz));
Q.push({nod_start, 1});
viz[nod_start] = 1;
lan nod;
while(!Q.empty()){
lan el = Q.front();
nod = el;
Q.pop();
for(auto &v:graph[el.y])
if(viz[v] == 0){
Q.push({v, el.dist+1});
viz[v] = 1;
}
}
return nod;
}
int main()
{
citire();
lan u1 = bfs(1);
lan fin = bfs(u1.y);
g<<fin.dist;
return 0;
}