Cod sursa(job #1094705)
| Utilizator | Data | 29 ianuarie 2014 19:04:38 | |
|---|---|---|---|
| Problema | Diametrul unui arbore | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.5 kb |
#include<fstream>
#include<vector>
#define N 100100
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
vector<int> v[N];
int i,n,x,y,ma,best,t,viz[N];
void dfs(int x)
{
viz[x]=1;
++t;
if(t>ma)
{
ma=t;
best=x;
}
for(int i=0;i<v[x].size();++i)
if(!viz[v[x][i]])
dfs(v[x][i]);
--t;
}
int main ()
{
f>>n;
for(i=1;i<n;++i)
{
f>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
memset(viz,0,sizeof(viz));
dfs(best);
g<<ma;
return 0;
}