Skip to content

Instantly share code, notes, and snippets.

@osjayaprakash
Last active December 10, 2015 01:58
Show Gist options
  • Save osjayaprakash/4363196 to your computer and use it in GitHub Desktop.
Save osjayaprakash/4363196 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <iostream>
#include <vector>
#define N 101
using namespace std;
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
int Visited[100000+1]={0};
int min0[100000+1]={0};
int min1[100000+1]={0};
vector<int> *AL;
int fn(int v)
{
int i,j,r0,r1;
vector <int> & k = AL[v];
r0=0;
Visited[v] = 1;
// printf("%d\n", v);
for(i=0;i<k.size();i++)
{
j = k[i];
if(Visited[j]==0)
{
fn(j);
r0=max(min1[j],min0[j])+1;
if(min0[v]<r0)
{
min1[v]=min0[v];
min0[v]=r0;
}
else if(min1[v]<r0)
min1[v]=r0;
}
}
}
int main()
{
int n,a,b;
scanf("%d", &n);
AL = new vector<int>[n+1];
for(int i=0;i<n-1;i++){
scanf("%d%d", &a,&b);
AL[a].push_back(b);
AL[b].push_back(a);
}
fn(1);
a=0;
for(int i=1;i<=n;i++){
// printf("%d %d %d\n",i, min1[i],min0[i] );
if(a<(min1[i]+min0[i]))
a = (min1[i]+min0[i]);
}
printf("%d\n", a );
delete []AL;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment