Language: C++

using namespace std;
const int MOD = 1000000007;
const int INF = 1e18;
using Graph = vector<vector<int>>;

//UnionFind
struct UnionFind{
  vector<int> Par;

  //N個の独立した根として初期化
  UnionFind(int N) : Par(N) {
    for(int i = 0; i < N; i++) Par[i] = i;
  }

  //どこの根に属するか得る再帰関数
  int root(int X){
    if( Par.at(X) == X ) return X;
    else return Par.at(X) = root(Par.at(X));
  }

  //XとYが同じ根に属するか判定
  bool same(int X, int Y){
    return root(X) == root(Y);
  }

  //XとYの木を合体
  void unite(int X, int Y){
    X = root(X);
    Y = root(Y);
    if( X == Y ) return;
    Par.at(X) = Y;
  }
};


signed main() {
  int N, M;
  cin >> N >> M;

  UnionFind UF(N);
  Graph G(N);
  for( int i = 0; i < M; i++ ){
    int X, Y, Z;
    cin >> X >> Y >> Z;
    X--;
    Y--;
    UF.unite(X, Y);
  }

  set<int> num;
  for( int i = 0; i < N; i++ ){
    int R = UF.root(i);
    if( num.count(R) ) continue;
    num.insert(R);
  }
  cout << num.size() << endl;

 }


Compiler output:
a.cc:3:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
    3 | const int INF = 1e18;
      |                 ^~~~
a.cc:4:15: error: 'vector' does not name a type
    4 | using Graph = vector<vector<int>>;
      |               ^~~~~~
a.cc:8:3: error: 'vector' does not name a type
    8 |   vector<int> Par;
      |   ^~~~~~
a.cc: In constructor 'UnionFind::UnionFind(int)':
a.cc:11:22: error: class 'UnionFind' does not have any field named 'Par'
   11 |   UnionFind(int N) : Par(N) {
      |                      ^~~
a.cc:12:32: error: 'Par' was not declared in this scope
   12 |     for(int i = 0; i < N; i++) Par[i] = i;
      |                                ^~~
a.cc: In member function 'int UnionFind::root(int)':
a.cc:17:9: error: 'Par' was not declared in this scope
   17 |     if( Par.at(X) == X ) return X;
      |         ^~~
a.cc: In member function 'void UnionFind::unite(int, int)':
a.cc:31:5: error: 'Par' was not declared in this scope
   31 |     Par.at(X) = Y;
      |     ^~~
a.cc: In function 'int main()':
a.cc:38:3: error: 'cin' was not declared in this scope
   38 |   cin >> N >> M;
      |   ^~~
a.cc:1:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | using namespace std;
a.cc:41:3: error: 'Graph' was not declared in this scope
   41 |   Graph G(N);
      |   ^~~~~
a.cc:50:3: error: 'set' was not declared in this scope
   50 |   set<int> num;
      |   ^~~
a.cc:1:1: note: 'std::set' is defined in header '<set>'; this is probably fixable by adding '#include <set>'
  +++ |+#include <set>
    1 | using namespace std;
a.cc:50:7: error: expected primary-expression before 'int'
   50 |   set<int> num;
      |       ^~~
a.cc:53:9: error: 'num' was not declared in this scope; did you mean 'enum'?
   53 |     if( num.count(R) ) continue;
      |         ^~~
      |         enum
a.cc:54:5: error: 'num' was not declared in this scope; did you mean 'enum'?
   54 |     num.insert(R);
      |     ^~~
      |     enum
a.cc:56:3: error: 'cout' was not declared in this scope
   56 |   cout << num.size() << endl;
      |   ^~~~
a.cc:56:3: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:56:11: error: 'num' was not declared in this scope; did you mean 'enum'?
   56 |   cout << num.size() << endl;
      |           ^~~
      |           enum
a.cc:56:25: error: 'endl' was not declared in this scope
   56 |   cout << num.size() << endl;
      |                         ^~~~
a.cc:1:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | using namespace std;