Advertisement
AlexD2006

smallworld

Sep 27th, 2023
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. vector<int> v[100005];
  7. vector <int> marked;
  8. int dist[100005];
  9. bool frecv[100005];
  10.  
  11. int dfs (int nod, int og)
  12. {
  13.     //cout<<nod<<endl;
  14.     marked.push_back(nod);
  15.     frecv[nod]=1;
  16.     int maxi=0;
  17.     for (int i=0; i<v[nod].size(); i++) {
  18.         int next_nod=v[nod][i];
  19.         //cout<<"."<<next_nod<<".";
  20.         if (frecv[next_nod]==0) {
  21.             //cout<<"-"<<next_nod<<"-";
  22.             maxi=max(dist[og], dfs(next_nod, og)+1);
  23.         }
  24.     }
  25.     //cout<<".";
  26.     return maxi;
  27. }
  28. int main()
  29. {
  30.     freopen("smallworld.in", "r", stdin);
  31.     freopen("smallworld.out", "w",stdout);
  32.     int n, a, b;
  33.     cin>>n;
  34.     for (int i=1; i<n; i++) {
  35.         cin>>a>>b;
  36.         v[a].push_back(b);
  37.         v[b].push_back(a);
  38.     }
  39.     for (int i=1; i<=n; i++) {
  40.         dist[i]=dfs(i, i);
  41.         for (int i=0; i<marked.size(); i++) {
  42.             frecv[marked[i]]=0;
  43.         }
  44.         marked.clear();
  45.         cout<<dist[i]<<'\n';
  46.         //cout<<endl;
  47.     }
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement