Problem p03727 (C++)

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

const int MX_N = 1e5 + 5;

int N;
queue<ii> q;
set<ii> f;
multiset<int> c[MX_N];
int p[MX_N];
int merged = 0;

int findset(int u) { return (p[u] == u) ? u : (p[u] = findset(p[u])); }

void merge(int u, int v) {
  c[u].erase(v);
  c[v].erase(u);
  f.erase({u, v});

  if (SZ(c[u]) > SZ(c[v]))
    swap(u, v);
  p[u] = v;
  // cout << "MERGE " << u << " to " << v << endl;

  for (int x : c[u]) {
    c[x].erase(u);

    ii e = {v, x};
    if (e.fi > e.sc)
      swap(e.fi, e.sc);
    // cout<<"UPDATE " << x << " " << u << " to " << e.fi << " " << e.sc <<
    // endl;
    if (f.count(e))
      q.push(e);
    else {
      c[x].insert(v);
      c[v].insert(x);
      f.insert(e);
    }
  }
  ++merged;
}

int main() {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  FOR(i, 1, N) p[i] = i;
  FOR(i, 1, 2 * N - 2) {
    int a, b;
    cin >> a >> b;
    a = findset(a), b = findset(b);
    assert(a != b);
    if (a > b)
      swap(a, b);
    if (f.count({a, b})) {
      merge(a, b);
    } else {
      c[a].insert(b);
      c[b].insert(a);
      f.insert({a, b});
    }
  }

  // cout<<SZ(q)<<endl;

  while (!q.empty()) {
    ii a = q.front();
    q.pop();
    a.fi = p[a.fi], a.sc = p[a.sc];
    if (a.fi > a.sc)
      swap(a.fi, a.sc);
    merge(a.fi, a.sc);
  }

  cout << (merged == N - 1 ? "YES" : "NO");
}


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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

const int MX_N = 1e5 + 5;

int N;
queue<ii> q;
set<ii> f;
multiset<int> c[MX_N];
int p[MX_N];
int merged = 0;

int findset(int u) { return (p[u] == u) ? u : (p[u] = findset(p[u])); }

void merge(int u, int v) {
  c[u].erase(v);
  c[v].erase(u);
  f.erase({u, v});

  if (SZ(c[u]) > SZ(c[v]))
    swap(u, v);
  p[u] = v;
  // cout << "MERGE " << u << " to " << v << endl;

  for (int x : c[u]) {
    c[x].erase(u);

    ii e = {v, x};
    if (e.fi > e.sc)
      swap(e.fi, e.sc);
    // cout<<"UPDATE " << x << " " << u << " to " << e.fi << " " << e.sc <<
    // endl;
    if (f.count(e))
      q.push(e);
    else {
      c[x].insert(v);
      c[v].insert(x);
      f.insert(e);
    }
  }
  ++merged;
}

int main() {
  // freopen("in.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  FOR(i, 1, N) p[i] = i;
  FOR(i, 1, 2 * N - 2) {
    int a, b;
    cin >> a >> b;
    a = findset(a), b = findset(b);
    assert(a != b);
    if (a > b)
      swap(a, b);
    if (f.count({a, b})) {
      merge(a, b);
    } else {
      c[a].insert(b);
      c[b].insert(a);
      f.insert({a, b});
    }
  }

  // cout<<SZ(q)<<endl;

  while (!q.empty()) {
    ii a = q.front();
    q.pop();
    a.fi = findset(a.fi), a.sc = findset(a.sc);
    if (a.fi > a.sc)
      swap(a.fi, a.sc);
    merge(a.fi, a.sc);
  }

  cout << (merged == N - 1 ? "YES" : "NO");
}