Problem p03727 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#define mp(a, b) make_pair(a, b)
using namespace std;
int fa[100010], n, x, y;
map<pair<int, int>, int> G, vis;
multiset<int> S[100010];
pair<int, int> q[100010];
int t, h;
void p(int &x, int &y) {
  if (x > y)
    swap(x, y);
}
int find(int x) {
  // cout<<x<<endl;
  if (fa[x] != x)
    fa[x] = find(fa[x]);
  return fa[x];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++)
    scanf("%d%d", &x, &y), p(x, y), G[mp(x, y)]++, S[x].insert(y),
        S[y].insert(x);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y), p(x, y);
    S[x].insert(y), S[y].insert(x);
    if (++G[mp(x, y)] > 1)
      vis[mp(x, y)] = 1, q[++t] = mp(x, y);
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int k = 1; k < n; k++) {
    int x, y;
    while (1) {
      if (h == t) {
        printf("NO");
        return 0;
      }
      x = find(q[++h].first), y = find(q[h].second);
      if (x == y)
        continue;
      else
        break;
    }
    if (S[x].size() > S[y].size())
      swap(x, y);
    fa[x] = y;
    for (set<int>::iterator i = S[x].begin(); i != S[x].end(); i++) {
      int nw = find(*i);
      if (nw == y)
        continue;
      S[y].insert(nw), S[nw].insert(y);
      int xx, yy;
      if (nw > y)
        xx = y, yy = nw;
      else
        xx = nw, yy = y;
      if (++G[mp(xx, yy)] > 1 && vis[mp(xx, yy)] != 1)
        vis[mp(xx, yy)] = 1, q[++t] = mp(xx, yy);
      if (S[nw].find(x) != S[nw].end())
        S[nw].erase(x);
    }
    S[y].erase(x);
  }
  printf("YES");
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#define mp(a, b) make_pair(a, b)
using namespace std;
int fa[100010], n, x, y;
map<pair<int, int>, int> G, vis;
multiset<int> S[100010];
pair<int, int> q[1000010];
int t, h;
void p(int &x, int &y) {
  if (x > y)
    swap(x, y);
}
int find(int x) {
  // cout<<x<<endl;
  if (fa[x] != x)
    fa[x] = find(fa[x]);
  return fa[x];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++)
    scanf("%d%d", &x, &y), p(x, y), G[mp(x, y)]++, S[x].insert(y),
        S[y].insert(x);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y), p(x, y);
    S[x].insert(y), S[y].insert(x);
    if (++G[mp(x, y)] > 1)
      vis[mp(x, y)] = 1, q[++t] = mp(x, y);
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int k = 1; k < n; k++) {
    int x, y;
    while (1) {
      if (h == t) {
        printf("NO");
        return 0;
      }
      x = find(q[++h].first), y = find(q[h].second);
      if (x == y)
        continue;
      else
        break;
    }
    if (S[x].size() > S[y].size())
      swap(x, y);
    fa[x] = y;
    for (set<int>::iterator i = S[x].begin(); i != S[x].end(); i++) {
      int nw = find(*i);
      if (nw == y)
        continue;
      S[y].insert(nw), S[nw].insert(y);
      int xx, yy;
      if (nw > y)
        xx = y, yy = nw;
      else
        xx = nw, yy = y;
      if (++G[mp(xx, yy)] > 1 && vis[mp(xx, yy)] != 1)
        vis[mp(xx, yy)] = 1, q[++t] = mp(xx, yy);
      if (S[nw].find(x) != S[nw].end())
        S[nw].erase(x);
    }
    S[y].erase(x);
  }
  printf("YES");
}