Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector <int> V[1000001];
- int najdalszy=0;
- int kolor[1000001];
- int odl=0;
- int index=0;
- void DFS1(int a)
- {
- kolor[a]=1;
- for (int i=0;i<V[a].size();i++)
- {
- if (kolor[V[a][i]]==0)
- {
- odl++;
- if (najdalszy<odl)
- {
- index=V[a][i];
- najdalszy=odl;
- }
- DFS1(V[a][i]);
- odl--;
- }
- }
- }
- void DFS2(int n)
- {
- kolor[n]=2;
- for (int i=0;i<V[n].size();i++)
- {
- if (kolor[V[n][i]]==1)
- {
- odl++;
- najdalszy = max (odl, najdalszy);
- DFS2(V[n][i]);
- odl--;
- }
- }
- }
- int main()
- {
- std::ios_base::sync_with_stdio(false);
- 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);
- }
- DFS1(1);
- odl=0;
- najdalszy=0;
- DFS2(index);
- cout << najdalszy;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement