Pagini recente » Cod sursa (job #1143355) | Cod sursa (job #1429015) | Cod sursa (job #1649710) | Cod sursa (job #2786830) | Cod sursa (job #2810722)
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int MAXN = 1000001;
int n,last,diam;
int counter[MAXN],IsinQ[MAXN];
vector<int> Node[MAXN];
queue<int> Q;
void BFS(int start)
{
memset(counter,0,sizeof(counter));
memset(IsinQ,0,sizeof(IsinQ));
Q.push(start);
counter[start]=1;
IsinQ[start]=1;
while(!Q.empty())
{
int x=Q.front();
for(auto nod : Node[x])
{
if(IsinQ[nod]==0)
{
Q.push(nod);
counter[nod]=counter[x]+1;
IsinQ[nod]=1;
diam=counter[nod];
last=nod;
}
}
Q.pop();
}
}
int main()
{
int x,y;
in>>n;
for(int i=0; i<n-1; i++)
{
in>>x>>y;
Node[x].push_back(y);
Node[y].push_back(x);
}
BFS(1);
BFS(last);
out<<diam;
return 0;
}