prithivMLmods/Coder-Stat huggingface dataset:
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m;
int p[100010];
int ans;

class UnionFind {
public:
  int N;
  vector<int> Parent;
  vector<int> Rank;
  UnionFind(int n) {
    N = n + 1;
    for (int i = 0; i < N; ++i) {
      Parent.push_back(i);
      Rank.push_back(0);
    }
  }

  int find(int x) {
    if (Parent[x] == x)
      return x;
    return Parent[x] == find(Parent[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (Rank[x] < Rank[y])
      Parent[x] = y;
    else {
      Parent[y] = x;
      if (Rank[x] == Rank[y])
        ++Rank[x];
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> p[i];
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    uf.unite(x, y);
  }
  for (int i = 0; i < n; ++i)
    if (uf.same(p[i], i + 1))
      ++ans;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m;
int p[100010];
int ans;

class UnionFind {
public:
  int N;
  vector<int> Parent;
  vector<int> Rank;
  UnionFind(int n) {
    N = n + 1;
    for (int i = 0; i < N; ++i) {
      Parent.push_back(i);
      Rank.push_back(0);
    }
  }

  int find(int x) {
    if (Parent[x] == x)
      return x;
    return Parent[x] = find(Parent[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (Rank[x] < Rank[y])
      Parent[x] = y;
    else {
      Parent[y] = x;
      if (Rank[x] == Rank[y])
        ++Rank[x];
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> p[i];
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    uf.unite(x, y);
  }
  for (int i = 0; i < n; ++i)
    if (uf.same(p[i], i + 1))
      ++ans;
  cout << ans << endl;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

int find_(int x, vector<int> &v) {
  if (v[x] < 0)
    return x;
  v[x] = find_(v[x], v);
  return v[x];
}

int same_(int a, int b, vector<int> &v) {
  if (find_(a, v) == find_(b, v))
    return 1;
  return 0;
}

void union_(int a, int b, vector<int> &v) {
  if (same_(a, b, v))
    return;
  int a_p = find_(a, v), b_p = find_(b, v);
  if (a_p > b_p)
    swap(a_p, b_p);
  v[a_p] += v[b_p];
  v[b_p] = a_p;
}

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> p(n, 0);
  vector<int> v(n, -1);
  rep(i, n) cin >> p[i];
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    union_(x, y, v);
  }
  int ans = 0;
  rep(i, n) {
    if (same_(p[i], i, v))
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

int find_(int x, vector<int> &v) {
  if (v[x] < 0)
    return x;
  v[x] = find_(v[x], v);
  return v[x];
}

int same_(int a, int b, vector<int> &v) {
  if (find_(a, v) == find_(b, v))
    return 1;
  return 0;
}

void union_(int a, int b, vector<int> &v) {
  if (same_(a, b, v))
    return;
  int a_p = find_(a, v), b_p = find_(b, v);
  if (a_p > b_p)
    swap(a_p, b_p);
  v[a_p] += v[b_p];
  v[b_p] = a_p;
}

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> p(n, 0);
  vector<int> v(n, -1);
  rep(i, n) {
    int q;
    cin >> q;
    p[i] = q - 1;
  }
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    union_(x, y, v);
  }
  int ans = 0;
  rep(i, n) {
    if (same_(p[i], i, v))
      ans++;
  }
  cout << ans << endl;
}
Problem p03356 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

struct union_find {
  vector<int> par;

  union_find(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else
      return root(par[x]);
  }

  void unite(int x, int y) {
    if (root(x) == root(y))
      return;
    else
      par[root(y)] = root(x);
  }

  bool find(int x, int y) { return root(x) == root(y); }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];

  union_find uf(N + 1);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    uf.unite(x, y);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (uf.find(i + 1, p[i]))
      ans++;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

struct union_find {
  vector<int> par;

  union_find(int N) : par(N) {
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    if (root(x) == root(y))
      return;
    else
      par[root(y)] = root(x);
  }

  bool find(int x, int y) { return root(x) == root(y); }
};

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];

  union_find uf(N + 1);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    uf.unite(x, y);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (uf.find(i + 1, p[i]))
      ans++;
  }

  cout << ans << endl;
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
vector<int> parent;
vector<int> nodes;

int getParent(int x) {
  int y = x;
  while (y != parent[y]) {
    y = parent[y];
  }
  parent[x] = y;
  return y;
}

bool unite(int x, int y) {
  x = getParent(x);
  y = getParent(y);
  if (x == y)
    return false;

  if (nodes[x] < nodes[y])
    swap(x, y);
  parent[y] = x;
  nodes[x] += nodes[y];
  return true;
}

bool isSame(int x, int y) {
  x = getParent(x);
  y = getParent(y);
  return (x == y);
}

int main(void) {
  int64_t N, M;
  cin >> N >> M;
  vector<int> P(N);
  unordered_map<int, int> pos;
  rep(i, N) {
    cin >> P[i];
    pos[P[i]] = i;
  }

  parent.resize(N);
  nodes.resize(N);
  rep(i, N) {
    parent[i] = i;
    nodes[i] = 1;
  }
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    unite(x, y);
  }

  int answer = 0;
  for (int i = 1; i <= N; ++i) {
    if (isSame(i - 1, pos[i]))
      ++answer;
  }
  cout << answer << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
vector<int> parent;
vector<int> nodes;

int getParent(int x) {
  int y = x;
  while (y != parent[y]) {
    y = parent[y];
  }
  parent[x] = y;
  return y;
}

bool unite(int x, int y) {
  x = getParent(x);
  y = getParent(y);
  if (x == y)
    return false;

  if (nodes[x] < nodes[y])
    swap(x, y);
  parent[y] = x;
  nodes[x] += nodes[y];
  return true;
}

bool isSame(int x, int y) {
  x = getParent(x);
  y = getParent(y);
  return (x == y);
}

int main(void) {
  int64_t N, M;
  cin >> N >> M;
  vector<int> P(N);
  unordered_map<int, int> pos;
  rep(i, N) {
    cin >> P[i];
    pos[P[i]] = i;
  }

  parent.resize(N);
  nodes.resize(N);
  rep(i, N) {
    parent[i] = i;
    nodes[i] = 1;
  }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    unite(x, y);
  }

  int answer = 0;
  for (int i = 1; i <= N; ++i) {
    if (isSame(i - 1, pos[i]))
      ++answer;
  }
  cout << answer << endl;
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

const int nmax = 10005;
vector<int> g[nmax];
int v[nmax];
bool visited[nmax];
vector<int> pos;
vector<int> val;

void dfs(int node) {
  pos.push_back(node);
  val.push_back(v[node]);
  for (int it : g[node]) {
    if (!visited[it]) {
      visited[it] = 1;
      dfs(it);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int nsol = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      pos.clear();
      val.clear();
      visited[i] = 1;
      dfs(i);
      sort(pos.begin(), pos.end());
      sort(val.begin(), val.end());
      int ii = 0, j = 0;

      while (ii < pos.size() && j < val.size()) {
        if (pos[ii] == val[j]) {
          ii++;
          j++;
          nsol++;
        } else if (pos[ii] > val[j])
          j++;
        else if (pos[ii] < val[j])
          ii++;
      }
    }
  }
  cout << nsol;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

const int nmax = 100005;
vector<int> g[nmax];
int v[nmax];
bool visited[nmax];
vector<int> pos;
vector<int> val;

void dfs(int node) {
  pos.push_back(node);
  val.push_back(v[node]);
  for (int it : g[node]) {
    if (!visited[it]) {
      visited[it] = 1;
      dfs(it);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int nsol = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      pos.clear();
      val.clear();
      visited[i] = 1;
      dfs(i);
      sort(pos.begin(), pos.end());
      sort(val.begin(), val.end());
      int ii = 0, j = 0;

      while (ii < pos.size() && j < val.size()) {
        if (pos[ii] == val[j]) {
          ii++;
          j++;
          nsol++;
        } else if (pos[ii] > val[j])
          j++;
        else if (pos[ii] < val[j])
          ii++;
      }
    }
  }
  cout << nsol;
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int Cin() {
  int n;
  cin >> n;
  return n;
}

// Union-Find木（素集合森：Disjoint-Set Forest）
// グループ分けを木構造で管理するデータ構造
class UnionFindTree {
private:
  // 各ノードの親ノード番号リスト
  vector<int> m_parents;

public:
  // コンストラクタ
  UnionFindTree(const int N) : m_parents(N) {
    // 最初は全ての親が自分自身＝根であるとして初期化
    for (int i = 0; i < N; ++i)
      m_parents[i] = i;
  }

  // データvが属する木の根を返す
  const int Root(const int v) {
    if (m_parents[v] == v)
      return v;
    return Root(m_parents[v]);
  }

  // データv1とv2の木を統合する
  void Unite(const int v1, const int v2) {
    int r1 = Root(v1), r2 = Root(v2);
    if (r1 == r2)
      return;
    m_parents[r1] = r2;
  }

  // ２つのデータが同じ木に属するかどうか判定
  const bool AreInSameTree(const int v1, const int v2) {
    return Root(v1) == Root(v2);
  }
};

int main() {
  int N = Cin(), M = Cin();
  vector<int> ps(N);
  for (auto &pi : ps) {
    cin >> pi;
    --pi;
  }

  // Union-Find木
  UnionFindTree tree(N);
  for (int i = 0; i < M; ++i) {
    int xi, yi;
    cin >> xi >> yi;
    tree.Unite(xi - 1, yi - 1); // 入れ替え可能＝同じ連結成分
  }

  // piがviと同じ連結成分に属していれば，入れ替えでpi=viとできる
  int ans = 0;
  for (int vi = 0; vi < N; ++vi) {
    int pi = ps[vi];
    if (tree.AreInSameTree(vi, pi))
      ++ans;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int Cin() {
  int n;
  cin >> n;
  return n;
}

// Union-Find木（素集合森：Disjoint-Set Forest）
// グループ分けを木構造で管理するデータ構造
class UnionFindTree {
private:
  // 各ノードの親ノード番号リスト
  vector<int> m_parents;

public:
  // コンストラクタ
  UnionFindTree(const int N) : m_parents(N) {
    // 最初は全ての親が自分自身＝根であるとして初期化
    for (int i = 0; i < N; ++i)
      m_parents[i] = i;
  }

  // データvが属する木の根を返す
  const int Root(const int v) {
    if (m_parents[v] == v)
      return v;
    return m_parents[v] = Root(m_parents[v]);
  }

  // データv1とv2の木を統合する
  void Unite(const int v1, const int v2) {
    int r1 = Root(v1), r2 = Root(v2);
    if (r1 == r2)
      return;
    m_parents[r1] = r2;
  }

  // ２つのデータが同じ木に属するかどうか判定
  const bool AreInSameTree(const int v1, const int v2) {
    return Root(v1) == Root(v2);
  }
};

int main() {
  int N = Cin(), M = Cin();
  vector<int> ps(N);
  for (auto &pi : ps) {
    cin >> pi;
    --pi;
  }

  // Union-Find木
  UnionFindTree tree(N);
  for (int i = 0; i < M; ++i) {
    int xi, yi;
    cin >> xi >> yi;
    tree.Unite(xi - 1, yi - 1); // 入れ替え可能＝同じ連結成分
  }

  // piがviと同じ連結成分に属していれば，入れ替えでpi=viとできる
  int ans = 0;
  for (int vi = 0; vi < N; ++vi) {
    int pi = ps[vi];
    if (tree.AreInSameTree(vi, pi))
      ++ans;
  }
  cout << ans << endl;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int p[maxn];
vector<int> graph[maxn];
int c[maxn];
void dfs(int u, int pp, int comp) {
  c[u] = comp;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    if (v == pp)
      continue;
    dfs(v, u, comp);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int ans1 = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i] == i)
      ans1++;
  }
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] == 0)
      dfs(i, -1, i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] != c[p[i]]) {
      ans++;
    }
  }
  ans = max(ans1, n - ans);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int p[maxn];
vector<int> graph[maxn];
int c[maxn];
void dfs(int u, int pp, int comp) {
  c[u] = comp;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i];
    if (v == pp)
      continue;
    if (c[v] == 0)
      dfs(v, u, comp);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int ans1 = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i] == i)
      ans1++;
  }
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] == 0)
      dfs(i, -1, i);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] != c[p[i]]) {
      ans++;
    }
  }
  ans = max(ans1, n - ans);
  printf("%d\n", ans);
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < n; i++)
template <class T, class U> void cmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> void cmin(T &a, U b) {
  if (a > b)
    a = b;
}

class UnionFind {
private:
  vector<int> parent;
  vector<int> rank;

public:
  UnionFind(int size) : parent(size), rank(size, 0) {
    for (int i = 0; i < size; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<int> p(n), x(n), y(n);
  rep(i, n) cin >> p[i];
  rep(i, m) cin >> x[i] >> y[i];

  UnionFind uf(n + 1);
  rep(i, m) { uf.unite(x[i], y[i]); }

  int ans = 0;
  rep(i, n) {
    if (uf.same(p[i], i + 1))
      ans++;
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < n; i++)
template <class T, class U> void cmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <class T, class U> void cmin(T &a, U b) {
  if (a > b)
    a = b;
}

class UnionFind {
private:
  vector<int> parent;
  vector<int> rank;

public:
  UnionFind(int size) : parent(size), rank(size, 0) {
    for (int i = 0; i < size; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<int> p(n), x(m), y(m);
  rep(i, n) cin >> p[i];
  rep(i, m) cin >> x[i] >> y[i];

  UnionFind uf(n + 1);
  rep(i, m) { uf.unite(x[i], y[i]); }

  int ans = 0;
  rep(i, n) {
    if (uf.same(p[i], i + 1))
      ans++;
  }

  cout << ans << endl;
}
Problem p03356 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int maxn = 1e5 + 7;

int father[maxn], arr[maxn];

int root(int x) {
  if (x == father[x])
    return x;
  return root(father[x]);
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    father[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int fx, fy, x, y;
    scanf("%d%d", &x, &y);
    fx = root(x);
    fy = root(y);
    father[fx] = fy;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (root(i) == root(arr[i]))
      ans++;
  }
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int maxn = 1e5 + 7;

int father[maxn], arr[maxn];

int root(int x) {
  if (x == father[x])
    return x;
  return father[x] = root(father[x]);
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    father[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int fx, fy, x, y;
    scanf("%d%d", &x, &y);
    fx = root(x);
    fy = root(y);
    father[fx] = fy;
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (root(i) == root(arr[i]))
      ans++;
  }
  printf("%d", ans);
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long llong;
static const int max_n = 10010;
using namespace std;

vector<int> numbers(max_n);

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++) {
      makeSet(i);
    }
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

  bool same(int x, int y) { return findSet(x) == findSet(y); }

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

int main() {
  int N, M, p, a;
  scanf("%d %d", &N, &M);
  DisjointSet DS(N);
  for (int i = 0; i < N; i++) {
    scanf("%d ", &p);
    numbers[i] = p - 1;
  }

  for (int i = 0; i < M; i++) {
    scanf("%d %d", &p, &a);
    DS.unite(p - 1, a - 1);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (int)DS.same(i, numbers[i]);
  }

  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long llong;
static const int max_n = 100010;
using namespace std;

vector<int> numbers(max_n);

class DisjointSet {
public:
  vector<int> rank, p;

  DisjointSet() {}
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++) {
      makeSet(i);
    }
  }

  void makeSet(int x) {
    p[x] = x;
    rank[x] = 0;
  }

  bool same(int x, int y) { return findSet(x) == findSet(y); }

  void unite(int x, int y) { link(findSet(x), findSet(y)); }

  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y]) {
        rank[y]++;
      }
    }
  }

  int findSet(int x) {
    if (x != p[x]) {
      p[x] = findSet(p[x]);
    }
    return p[x];
  }
};

int main() {
  int N, M, p, a;
  scanf("%d %d", &N, &M);
  DisjointSet DS(N);
  for (int i = 0; i < N; i++) {
    scanf("%d ", &p);
    numbers[i] = p - 1;
  }

  for (int i = 0; i < M; i++) {
    scanf("%d %d", &p, &a);
    DS.unite(p - 1, a - 1);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (int)DS.same(i, numbers[i]);
  }

  printf("%d\n", ans);
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main() {

  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }

  vector<vector<int>> G(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  vector<int> group(n, -1);
  int color = 0;
  auto dfs = [&](auto &&dfs, int u, int par) -> void {
    group[u] = color;
    for (int v : G[u]) {
      if (v == par)
        continue;
      dfs(dfs, v, u);
    }
  };
  for (int i = 0; i < n; i++) {
    if (group[i] == -1) {
      dfs(dfs, i, -1);
      color++;
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (group[p[i]] == group[i])
      ans++;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main() {

  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }

  vector<vector<int>> G(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  vector<int> group(n, -1);
  int color = 0;
  auto dfs = [&](auto &&dfs, int u, int par) -> void {
    if (group[u] != -1)
      return;
    group[u] = color;
    for (int v : G[u]) {
      if (v == par)
        continue;
      dfs(dfs, v, u);
    }
  };
  for (int i = 0; i < n; i++) {
    if (group[i] == -1) {
      dfs(dfs, i, -1);
      color++;
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (group[p[i]] == group[i])
      ans++;
  }
  cout << ans << endl;

  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Rep(i, n) for (int i = 0; i < n; ++i)
#define rep(i, n) for (int i = 1; i <= n; ++i)
#define ll long long int
#define INF 100000000
#define INFLL 100000000000
#define MOD 1000000007
#define PI 3.14159265359
#define All(a) (a).begin(), (a).end()

using namespace std;
using P = pair<ll, ll>;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) {}
};

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  bool same(int x, int y) {
    x = find(x);
    y = find(y);
    return (x == y);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  // 要素 k が属する集合の要素の数を求める
  int size(int k) { return -data[find(k)]; }
};

int N, M;
int p[110000];

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

  rep(i, N) cin >> p[i];

  UnionFind uni = UnionFind(N);

  rep(i, M) {
    int x, y;
    cin >> x >> y;
    uni.unite(p[x], p[y]);
  }

  int ans = 0;

  rep(i, N) {
    if (uni.same(i, p[i]))
      ans++;
  }

  cout << ans << "\n";
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Rep(i, n) for (int i = 0; i < n; ++i)
#define rep(i, n) for (int i = 1; i <= n; ++i)
#define ll long long int
#define INF 100000000
#define INFLL 100000000000
#define MOD 1000000007
#define PI 3.14159265359
#define All(a) (a).begin(), (a).end()

using namespace std;
using P = pair<ll, ll>;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) {}
};

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  bool same(int x, int y) {
    x = find(x);
    y = find(y);
    return (x == y);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  // 要素 k が属する集合の要素の数を求める
  int size(int k) { return -data[find(k)]; }
};

int N, M;
int p[110000];

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

  rep(i, N) cin >> p[i];

  UnionFind uni = UnionFind(N + 1);

  rep(i, M) {
    int x, y;
    cin >> x >> y;
    uni.unite(p[x], p[y]);
  }

  int ans = 0;

  rep(i, N) {
    if (uni.same(i, p[i]))
      ans++;
  }

  cout << ans << "\n";
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
#define inf 1000000000
#define mod 1000000007
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for (int i = a; i >= b; i--)
#define f first
#define ss second
#define pb push_back
// #define mp make_pair

int n, m, x, y, a[202020];
vi adj[202020];
set<int> p, q;
bool vis[202020];
map<int, int> mp;

void dfs(int s) {

  if (vis[s])
    return;
  vis[s] = true;

  p.insert(s);
  q.insert(mp[s]);

  for (auto u : adj[s]) {
    if (!vis[u])
      dfs(u);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

  cin >> n >> m;

  rep(i, 1, n) {

    cin >> a[i];
    mp[a[i]] = i;
  }

  rep(i, 1, m) {

    cin >> x >> y;

    adj[x].pb(y);
    adj[y].pb(x);
  }

  int cnt = 0;
  rep(i, 1, n) {

    if (vis[i])
      continue;

    dfs(i);

    // sort(q.begin(), q.end());

    for (auto t : p) {
      auto l = q.find(t);
      if (l != q.end())
        cnt++;
    }

    q.clear();
    p.clear();
  }

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
#define inf 1000000000
#define mod 1000000007
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for (int i = a; i >= b; i--)
#define f first
#define ss second
#define pb push_back
// #define mp make_pair

int n, m, x, y, a[202020];
vi adj[202020];
set<int> p, q;
bool vis[202020];
map<int, int> mp;

void dfs(int s) {

  if (vis[s])
    return;
  vis[s] = true;

  p.insert(s);
  q.insert(mp[s]);

  for (auto u : adj[s]) {
    if (!vis[u])
      dfs(u);
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  cin >> n >> m;

  rep(i, 1, n) {

    cin >> a[i];
    mp[a[i]] = i;
  }

  rep(i, 1, m) {

    cin >> x >> y;

    adj[x].pb(y);
    adj[y].pb(x);
  }

  int cnt = 0;
  rep(i, 1, n) {

    if (vis[i])
      continue;

    dfs(i);

    // sort(q.begin(), q.end());

    for (auto t : p) {
      auto l = q.find(t);
      if (l != q.end())
        cnt++;
    }

    q.clear();
    p.clear();
  }

  cout << cnt << endl;

  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll;
int ans;
int parent[N];

int find(int v) {
  if (parent[v] == v)
    return v;
  return parent[v] = find(parent[v]);
}

void Union(int v, int u) { parent[find(v)] = find(u); }

int main() {
  freopen("1_022.txt", "r", stdin);

  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    parent[i] = i;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    Union(a, b);
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] == i)
      ans++;
    else {
      if (find(arr[i]) == find(i))
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 100005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll;
int ans;
int parent[N];

int find(int v) {
  if (parent[v] == v)
    return v;
  return parent[v] = find(parent[v]);
}

void Union(int v, int u) { parent[find(v)] = find(u); }

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    parent[i] = i;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    Union(a, b);
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] == i)
      ans++;
    else {
      if (find(arr[i]) == find(i))
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional> // std::function<void(int)>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int N) {
    par.assign(N, 0);
    rank.assign(N, 0);
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      swap(x, y);

    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
};

signed main() {
  int N, m;
  cin >> N >> m;
  UnionFind uf(N);
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    uf.unite(x, y);
  }

  int res = 0;
  for (int i = 0; i < N; i++) {
    if (uf.find(i) == uf.find(p[i]))
      res++;
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional> // std::function<void(int)>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int N) {
    par.assign(N, 0);
    rank.assign(N, 0);
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      swap(x, y);

    par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
  }
};

signed main() {
  int N, m;
  cin >> N >> m;
  UnionFind uf(N);
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    uf.unite(x, y);
  }

  int res = 0;
  for (int i = 0; i < N; i++) {
    if (uf.same(i, p[i] - 1))
      res++;
  }
  cout << res << endl;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n, 0) { rep(i, n) par[i] = i; }
  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = par[x];
    y = par[y];
    if (x == y)
      return;
    if (par[x] < par[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--, y[i]--;
  }
  UnionFind uf(n);
  rep(i, m) { uf.unite(x[i], y[i]); }
  int ans = 0;
  rep(i, n) {
    if (uf.same(p[i], i))
      ans++;
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct UnionFind {
  vector<int> par, rank;
  UnionFind(int n) : par(n), rank(n, 0) { rep(i, n) par[i] = i; }
  int find(int x) {
    if (par[x] == x)
      return x;
    else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (par[x] < par[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> p(n);
  rep(i, n) {
    cin >> p[i];
    p[i]--;
  }
  vector<int> x(m), y(m);
  rep(i, m) {
    cin >> x[i] >> y[i];
    x[i]--, y[i]--;
  }
  UnionFind uf(n);
  rep(i, m) { uf.unite(x[i], y[i]); }
  int ans = 0;
  rep(i, n) {
    if (uf.same(p[i], i))
      ans++;
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct UF {
  vector<int> e;
  UF(int n) : e(n, -1) {}
  bool same_set(int a, int b) { return find(a) == find(b); }
  int size(int x) { return -e[find(x)]; }
  int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
  void join(int a, int b) {
    a = find(a), b = find(b);
    if (a == b)
      return;
    if (e[a] > e[b])
      swap(a, b);
    e[a] += e[b], e[b] = a;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), id(n);
  UF uf(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> a[i], id[a[i]] = i;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    uf.join(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (uf.same_set(i, id[i]))
      ans++;
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

struct UF {
  vector<int> e;
  UF(int n) : e(n, -1) {}
  bool same_set(int a, int b) { return find(a) == find(b); }
  int size(int x) { return -e[find(x)]; }
  int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
  void join(int a, int b) {
    a = find(a), b = find(b);
    if (a == b)
      return;
    if (e[a] > e[b])
      swap(a, b);
    e[a] += e[b], e[b] = a;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n + 1), id(n + 1);
  UF uf(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> a[i], id[a[i]] = i;
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    uf.join(u, v);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (uf.same_set(i, id[i]))
      ans++;
  }
  cout << ans << '\n';
}
Problem p03356 (Python)

Buggy submission (status: Time Limit Exceeded):
class UnionFind:
    def __init__(self, n):
        self.p = {i: i for i in range(n)}
        self.r = {i: 0 for i in range(n)}

    def root(self, x):
        if self.p[x] == x:
            return x
        self.p[x] = self.root(self.p[x])
        return self.p[x]

    def merge(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.r[x] == self.r[y]:
            self.r[x] += 1
        elif self.r[x] > self.r[y]:
            x, y = y, x
        self.p[x] = y

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def cluster(self):
        c = {}
        for x in self.p:
            xx = self.root(x)
            if xx not in c:
                c[xx] = {x}
            else:
                c[xx].add(x)
        return c


def main():
    n, m = list(map(int, input().split()))
    pp = list(map(int, input().split()))
    uf = UnionFind(n)
    for _ in range(m):
        x, y = map(int, input().split())
        uf.merge(x - 1, y - 1)
    cc = uf.cluster()
    count = 0
    for clt in cc.values():
        p = set(pp[i] - 1 for i in clt)
        count += len(p & clt)
    print(count)


if __name__ == "__main__":
    main()


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.p = {i: i for i in range(n)}
        self.r = {i: 0 for i in range(n)}

    def root(self, x):
        if self.p[x] == x:
            return x
        self.p[x] = self.root(self.p[x])
        return self.p[x]

    def merge(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.r[x] < self.r[y]:
            self.p[x] = y
        else:
            self.p[y] = x
            if self.r[x] == self.r[y]:
                self.r[x] += 1

    def cluster(self):
        c = {}
        for x in self.p:
            xx = self.root(x)
            if xx not in c:
                c[xx] = {x}
            else:
                c[xx].add(x)
        return c


def main():
    n, m = list(map(int, input().split()))
    pp = list(map(int, input().split()))
    uf = UnionFind(n)
    for _ in range(m):
        x, y = map(int, input().split())
        uf.merge(x - 1, y - 1)
    cc = uf.cluster()
    count = 0
    for clt in cc.values():
        p = set(pp[i] - 1 for i in clt)
        count += len(p & clt)
    print(count)


if __name__ == "__main__":
    main()
Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long

class UnionFind {
public:
  vector<int> Parent;
  UnionFind(int N) { Parent = vector<int>(N, -1); }
  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }
  int size(int A) { return Parent[root(A)]; }
  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;
    if (size(A) < size(B))
      swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> p(N);
  UnionFind Uni(N + 2);
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    p[a] = i;
  }
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    Uni.connect(x, y);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (Uni.root(i) == Uni.root(p[i]))
      ans++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long

class UnionFind {
public:
  vector<int> Parent;
  UnionFind(int N) { Parent = vector<int>(N, -1); }
  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }
  int size(int A) { return Parent[root(A)]; }
  bool connect(int A, int B) {
    A = root(A);
    B = root(B);
    if (A == B)
      return false;
    if (size(A) < size(B))
      swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> p(N + 1);
  UnionFind Uni(N + 2);
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    p[a] = i;
  }
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    Uni.connect(x, y);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (Uni.root(i) == Uni.root(p[i]))
      ans++;
  }
  cout << ans << endl;
}
Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
static const int INF = 1000000000;
static int table[2][2001][1001];
static int dp[1001][1001];

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  const int m = 2 * n;
  vector<int> color(m);
  vector<int> value(m);
  for (int i = 0; i < m; ++i) {
    string s;
    int x;
    cin >> s >> x;
    color[i] = (s[0] == 'W' ? 0 : 1);
    value[i] = x - 1;
  }
  vector<int> inv[2] = {vector<int>(n), vector<int>(n)};
  for (int i = 0; i < m; ++i) {
    inv[color[i]][value[i]] = i;
  }

  for (int c = 0; c < 2; ++c) {
    for (int j = 0; j <= n; ++j) {
      table[c][0][j] = 0;
    }
    for (int i = 1; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        table[c][i][j] = table[c][i - 1][j];
      }
      if (color[i - 1] == c) {
        for (int j = value[i - 1] + 1; j <= n; ++j) {
          ++table[c][i][j];
        }
      }
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      if (i < n) {
        const int p = inv[0][i];
        dp[i + 1][j] =
            min(dp[i + 1][j], dp[i][j] + p - table[0][p][i] - table[1][p][j]);
      }
      if (j < n) {
        const int p = inv[1][j];
        dp[i][j + 1] =
            min(dp[i][j + 1], dp[i][j] + p - table[0][p][i] - table[1][p][j]);
      }
    }
  }
  cout << dp[n][n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
static const int INF = 1000000000;
static int table[2][4001][2001];
static int dp[2001][2001];

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  const int m = 2 * n;
  vector<int> color(m);
  vector<int> value(m);
  for (int i = 0; i < m; ++i) {
    string s;
    int x;
    cin >> s >> x;
    color[i] = (s[0] == 'W' ? 0 : 1);
    value[i] = x - 1;
  }
  vector<int> inv[2] = {vector<int>(n), vector<int>(n)};
  for (int i = 0; i < m; ++i) {
    inv[color[i]][value[i]] = i;
  }

  for (int c = 0; c < 2; ++c) {
    for (int j = 0; j <= n; ++j) {
      table[c][0][j] = 0;
    }
    for (int i = 1; i <= m; ++i) {
      for (int j = 0; j <= n; ++j) {
        table[c][i][j] = table[c][i - 1][j];
      }
      if (color[i - 1] == c) {
        for (int j = value[i - 1] + 1; j <= n; ++j) {
          ++table[c][i][j];
        }
      }
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      if (i < n) {
        const int p = inv[0][i];
        dp[i + 1][j] =
            min(dp[i + 1][j], dp[i][j] + p - table[0][p][i] - table[1][p][j]);
      }
      if (j < n) {
        const int p = inv[1][j];
        dp[i][j + 1] =
            min(dp[i][j + 1], dp[i][j] + p - table[0][p][i] - table[1][p][j]);
      }
    }
  }
  cout << dp[n][n] << endl;
  return 0;
}
Problem p03357 (C++)

Buggy submission (status: Runtime Error):
// #include "debug.h"
#include <algorithm>
#include <bits/stdc++.h>
#include <limits.h>
#include <map>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

#define MAXN 2001

int n;
int a[MAXN], c[MAXN];
int dp[MAXN][MAXN];
int cost[2][MAXN][MAXN];

int input() {
  if (scanf("%d", &n) < 0)
    return 0;
  for (int i = 0; i < 2 * n; i++) {
    char x;
    scanf(" %c %d", &x, &a[i]);
    a[i]--;
    c[i] = (x == 'B');
  }
  return 1;
}

void init() {}

long solve() {
  memset(cost, 0, sizeof(cost));
  for (int p = 0; p < 2 * n; p++) {
    int i = a[p];
    int k = c[p];
    int d = 0;
    for (int q = 0; q < p; q++) {
      int j = a[q];
      if (c[q] == k)
        d += j > i;
      else
        cost[k][i][j]++;
    }
    cost[k][i][n] = d;
    for (int j = n - 1; j >= 0; j--)
      cost[k][i][j] += cost[k][i][j + 1];
  }
  // for (int k=0; k<2; k++) printg((int*)cost[k], n+1, n+1, MAXN);
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      if (i || j) {
        int cc = INT_MAX;
        if (i)
          cc = min(cc, dp[i - 1][j] + cost[0][i - 1][j]);
        if (j)
          cc = min(cc, dp[i][j - 1] + cost[1][j - 1][i]);
        dp[i][j] = cc;
      }
  }
  // printg((int*)dp, n+1, n+1, MAXN);
  return dp[n][n];
}

void output(long ans) { printf("%ld\n", ans); }

void cleanup() {}

int main() {
  while (input()) {
    init();
    output(solve());
    cleanup();
    // break;
  }
}


Fixed submission:
// #include "debug.h"
#include <algorithm>
#include <bits/stdc++.h>
#include <limits.h>
#include <map>
#include <set>
#include <string.h>
#include <vector>
using namespace std;

#define MAXN 2001

int n;
int a[MAXN * 2], c[MAXN * 2];
int dp[MAXN][MAXN];
int cost[2][MAXN][MAXN];

int input() {
  if (scanf("%d", &n) < 0)
    return 0;
  for (int i = 0; i < 2 * n; i++) {
    char x;
    scanf(" %c %d", &x, &a[i]);
    a[i]--;
    c[i] = (x == 'B');
  }
  return 1;
}

void init() {}

long solve() {
  memset(cost, 0, sizeof(cost));
  for (int p = 0; p < 2 * n; p++) {
    int i = a[p];
    int k = c[p];
    int d = 0;
    for (int q = 0; q < p; q++) {
      int j = a[q];
      if (c[q] == k)
        d += j > i;
      else
        cost[k][i][j]++;
    }
    cost[k][i][n] = d;
    for (int j = n - 1; j >= 0; j--)
      cost[k][i][j] += cost[k][i][j + 1];
  }
  // for (int k=0; k<2; k++) printg((int*)cost[k], n+1, n+1, MAXN);
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++)
      if (i || j) {
        int cc = INT_MAX;
        if (i)
          cc = min(cc, dp[i - 1][j] + cost[0][i - 1][j]);
        if (j)
          cc = min(cc, dp[i][j - 1] + cost[1][j - 1][i]);
        dp[i][j] = cc;
      }
  }
  // printg((int*)dp, n+1, n+1, MAXN);
  return dp[n][n];
}

void output(long ans) { printf("%ld\n", ans); }

void cleanup() {}

int main() {
  while (input()) {
    init();
    output(solve());
    cleanup();
    // break;
  }
}