Created
August 5, 2020 13:55
-
-
Save Mohammad-debug/5aa2132189925f68afc9f2401d8a68b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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