Cod sursa(job #3137874)
| Utilizator | Data | 15 iunie 2023 18:53:45 | |
|---|---|---|---|
| Problema | Diametrul unui arbore | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.7 kb |
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("darb.in");
ofstream fout("darb.out");
vector<int> a[100001];
int f[100001];
int rez,pred,maxx,nmaxx;
void dfs(int i)
{
f[i]=f[pred]+1;
if(f[i]>maxx)
{
maxx=f[i];
nmaxx=i;
}
for(auto y:a[i])
{
if(!f[y])
{
pred=i;
dfs(y);
}
}
}
int main()
{
int n;
fin>>n;
int x,y;
while(fin>>x>>y)
{
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
memset(f, 0, sizeof(f));
maxx=0;
dfs(nmaxx);
fout<<maxx;
return 0;
}
