Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#using namespace std;

struct Unionfind{
  vector<int> par;
  Unionfind(int n):par(n){
    for(int i=0;i<n;i++){
      par[i]=i;
    }
  }
    /*int root(int x){
      if(par[x]==x) return x;
      return par[x]=root(par[x]);
      }*/
    int root(int x){
      int p=x;
      //int q=n;
      int i=0;
      while(p!=par[p]){
	//q=par[p];
	p=par[p];
	i++;
	}
      return par[x];
    }
      int root_dist(int x){
      int p=x;
      //int q=n;
      int i=0;
      while(p!=par[p]){
	//q=par[p];
	p=par[p];
	i++;
	}
      return i;
    }
      void unite(int x,int y){
	int rx=root(x);
	int ry=root(y);
	if(rx==ry) return;
	par(rx)=ry;
      }
  }
int main(){
  int n,k;
  Unionfind tree(n);
  int a,b;
  for(int i=0;i<n-1;i++){
    cin>>a>>b;
    tree.unite(a-1, b-1);
  }
  vector<int> dist;
  int r=tree.root(n-1);
  for(int j=0;j<n;j++){
    dist[j]=tree.root_dist(r);
  }
  sort(dist.begin(),dist.end());
  int ans=dist[0]+dist[1];
  cout<<ans<<endl;
  return 0;
}


Compiler output:
a.cc:4:2: error: invalid preprocessing directive #using
    4 | #using namespace std;
      |  ^~~~~
a.cc:7:3: error: 'vector' does not name a type
    7 |   vector<int> par;
      |   ^~~~~~
a.cc:45:4: error: expected ';' after struct definition
   45 |   }
      |    ^
      |    ;
a.cc: In constructor 'Unionfind::Unionfind(int)':
a.cc:8:20: error: class 'Unionfind' does not have any field named 'par'
    8 |   Unionfind(int n):par(n){
      |                    ^~~
a.cc:10:7: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   10 |       par[i]=i;
      |       ^~~
      |       __pstl::execution::v1::par
In file included from /usr/include/c++/14/pstl/glue_algorithm_defs.h:15,
                 from /usr/include/c++/14/algorithm:86,
                 from a.cc:3:
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'int Unionfind::root(int)':
a.cc:21:16: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   21 |       while(p!=par[p]){
      |                ^~~
      |                __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:26:14: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   26 |       return par[x];
      |              ^~~
      |              __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'int Unionfind::root_dist(int)':
a.cc:32:16: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   32 |       while(p!=par[p]){
      |                ^~~
      |                __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'void Unionfind::unite(int, int)':
a.cc:43:9: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   43 |         par(rx)=ry;
      |         ^~~
      |         __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In function 'int main()':
a.cc:51:5: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
   51 |     cin>>a>>b;
      |     ^~~
      |     std::cin
In file included from a.cc:1:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:54:3: error: 'vector' was not declared in this scope
   54 |   vector<int> dist;
      |   ^~~~~~
a.cc:54:3: note: suggested alternatives:
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_vector.h:428:11: note:   'std::vector'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
/usr/include/c++/14/vector:93:13: note:   'std::pmr::vector'
   93 |       using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
      |             ^~~~~~
a.cc:54:10: error: expected primary-expression before 'int'
   54 |   vector<int> dist;
      |          ^~~
a.cc:57:5: error: 'dist' was not declared in this scope
   57 |     dist[j]=tree.root_dist(r);
      |     ^~~~
a.cc:59:8: error: 'dist' was not declared in this scope
   59 |   sort(dist.begin(),dist.end());
      |        ^~~~
a.cc:59:3: error: 'sort' was not declared in this scope; did you mean 'std::sort'?
   59 |   sort(dist.begin(),dist.end());
      |   ^~~~
      |   std::sort
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note: 'std::sort' declared here
  296 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
      | ^~~~
a.cc:61:3: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   61 |   cout<<ans<<endl;
      |   ^~~~
      |   std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:61:14: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   61 |   cout<<ans<<endl;
      |              ^~~~
      |              std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~