Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- vector<int> v[100005];
- int dp[100005],ans=0;
- void dfs(int node,int pnode)
- {
- vector<int> tmp;
- for (int i=0;i<v[node].size();i++)
- {
- if (v[node][i]!=pnode)
- {
- dfs(v[node][i],node);
- tmp.push_back(dp[v[node][i]]);
- }
- }
- sort(tmp.begin(),tmp.end());
- if (tmp.size()==0)
- return;
- dp[node]=tmp[tmp.size()-1]+1;
- if (tmp.size()==1)
- ans=max(ans,dp[node]);
- else
- ans=max(ans,tmp[tmp.size()-1]+tmp[tmp.size()-2]+2);
- }
- int main()
- {
- int n;
- cin >> n;
- for (int i=1;i<n;i++)
- {
- int a,b;
- cin >> a >> b;
- v[a].push_back(b);
- v[b].push_back(a);
- }
- dfs(1,0);
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement