Skip to content

Instantly share code, notes, and snippets.

@Mohammad-debug
Created August 5, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mohammad-debug/5aa2132189925f68afc9f2401d8a68b3 to your computer and use it in GitHub Desktop.
Save Mohammad-debug/5aa2132189925f68afc9f2401d8a68b3 to your computer and use it in GitHub Desktop.
//
// Created by Anas on 05-08-2020.
//
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[10000];
vector<bool>v(10000, false);
int cnt;
int bfs(int a,int n){
cnt=0;
queue<int>q;
int level[10000]={0};
q.push(a);
for(int i=0;i<10000;i++)
v[i]=false;
int f=0;
while(!q.empty()){
f=q.front();
v[f]=true;
q.pop();
//cout<<f<<" ";
int flg=1;
for(int i=0;i<adj[f].size();i++) {
if(!v[adj[f][i]]) {
if(flg)
cnt++;
flg=0;
level[adj[f][i]] = level[f] + 1;
q.push(adj[f][i]);
}
}
}
return f;
}
void solve(int n){
int x=0,y=0;
for(int i=0;i<n-1;i++){
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
int k=bfs(1,n);
// cout<<k<<endl;
cnt=0;
k= bfs(k,n);
//cout<<k<<endl;
cout <<cnt<<endl;
}
int main(){
int n;
cin>>n;
solve(n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment