Language: C++

#include <cstdio>
#include <vector>
#include <set>
#include <numeric>

using namespace std;

vector<int> parent, urank;

int root(int x) { return (parent.at(x) == x ? x : parent.at(x) = root(parent.at(x))); }

bool in_same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
        if (urank.at(x) < urank.at(y)) parent.at(x) = y;
        else {
            parent.at(y) = x;
            if (urank.at(x) == urank.at(y)) ++(urank.at(x));
        }
    }
}

int main() {
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    parent.resize(n + 1);
    urank.resize(n + 1);
    iota(parent.begin(), parent.end(), 0);
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%*d", &x, &y);
        unite(x, y);
    }
    set<int> ans;
    for (int i = 1; i <= n; ++i) ans.insert(root(i));
    printf("%d\n", int(ans.size()));
    return 0;
}
#include <cstdio>
#include <vector>
#include <set>
#include <numeric>

using namespace std;

vector<int> parent, urank;

int root(int x) { return (parent.at(x) == x ? x : parent.at(x) = root(parent.at(x))); }

bool in_same(int x, int y) { return root(x) == root(y); }
void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
        if (urank.at(x) < urank.at(y)) parent.at(x) = y;
        else {
            parent.at(y) = x;
            if (urank.at(x) == urank.at(y)) ++(urank.at(x));
        }
    }
}

int main() {
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    parent.resize(n + 1);
    urank.resize(n + 1);
    iota(parent.begin(), parent.end(), 0);
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%*d", &x, &y);
        unite(x, y);
    }
    set<int> ans;
    for (int i = 1; i <= n; ++i) ans.insert(root(i));
    printf("%d\n", int(ans.size()));
    return 0;
}


Compiler output:
a.cc:47:13: error: redefinition of 'std::vector<int> parent'
   47 | vector<int> parent, urank;
      |             ^~~~~~
a.cc:8:13: note: 'std::vector<int> parent' previously declared here
    8 | vector<int> parent, urank;
      |             ^~~~~~
a.cc:47:21: error: redefinition of 'std::vector<int> urank'
   47 | vector<int> parent, urank;
      |                     ^~~~~
a.cc:8:21: note: 'std::vector<int> urank' previously declared here
    8 | vector<int> parent, urank;
      |                     ^~~~~
a.cc:49:5: error: redefinition of 'int root(int)'
   49 | int root(int x) { return (parent.at(x) == x ? x : parent.at(x) = root(parent.at(x))); }
      |     ^~~~
a.cc:10:5: note: 'int root(int)' previously defined here
   10 | int root(int x) { return (parent.at(x) == x ? x : parent.at(x) = root(parent.at(x))); }
      |     ^~~~
a.cc:51:6: error: redefinition of 'bool in_same(int, int)'
   51 | bool in_same(int x, int y) { return root(x) == root(y); }
      |      ^~~~~~~
a.cc:12:6: note: 'bool in_same(int, int)' previously defined here
   12 | bool in_same(int x, int y) { return root(x) == root(y); }
      |      ^~~~~~~
a.cc:52:6: error: redefinition of 'void unite(int, int)'
   52 | void unite(int x, int y) {
      |      ^~~~~
a.cc:13:6: note: 'void unite(int, int)' previously defined here
   13 | void unite(int x, int y) {
      |      ^~~~~
a.cc:64:5: error: redefinition of 'int main()'
   64 | int main() {
      |     ^~~~
a.cc:25:5: note: 'int main()' previously defined here
   25 | int main() {
      |     ^~~~