Problem p03354 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

#define len(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define x first
#define y second
#define int long long
#define double long double
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<int, int>
#define beg begin

#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl;
#else
#define dbg(x)
#endif

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
}

const int INF = 1e18, MAXN = 2 + 10, MOD = 998244353;
int n, m, ans = 0;
vector<pii> g[MAXN];
int used[MAXN];
set<int> se1, se2;

void dfs(int v, int phl) {
  used[v] = 1;
  se1.insert(v), se2.insert(phl);

  for (auto it : g[v]) {
    int u = it.x, hl = it.y;
    if (!used[u]) {
      dfs(u, hl);
    }
  }
}

void solve() {
  cin >> n >> m;
  vi a(n);
  cin >> a;

  for (int i = 0; i < n; ++i) {
    a[i]--;
  }

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[v].pb({u, a[u]});
    g[u].pb({v, a[v]});
  }

  for (int v = 0; v < n; ++v) {
    if (!used[v]) {
      se1.clear(), se2.clear();
      dfs(v, a[v]);

      for (auto it : se1) {
        if (se2.count(it)) {
          ans++;
        }
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

#define len(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define x first
#define y second
#define int long long
#define double long double
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<int, int>
#define beg begin

#ifdef LOCAL
#define dbg(x) cerr << #x << " = " << x << endl;
#else
#define dbg(x)
#endif

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
}

const int INF = 1e18, MAXN = 2e5 + 10, MOD = 998244353;
int n, m, ans = 0;
vector<pii> g[MAXN];
int used[MAXN];
set<int> se1, se2;

void dfs(int v, int phl) {
  used[v] = 1;
  se1.insert(v), se2.insert(phl);

  for (auto it : g[v]) {
    int u = it.x, hl = it.y;
    if (!used[u]) {
      dfs(u, hl);
    }
  }
}

void solve() {
  cin >> n >> m;
  vi a(n);
  cin >> a;

  for (int i = 0; i < n; ++i) {
    a[i]--;
  }

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[v].pb({u, a[u]});
    g[u].pb({v, a[v]});
  }

  for (int v = 0; v < n; ++v) {
    if (!used[v]) {
      se1.clear(), se2.clear();
      dfs(v, a[v]);

      for (auto it : se1) {
        if (se2.count(it)) {
          ans++;
        }
      }
    }
  }

  cout << ans << endl;
}