Problem p03727 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x << 1)
#define w2 second
#define rs (x << 1 | 1)
#define mp make_pair
#define pb push_back
#define mid ((l + r) >> 1)
#define SZ(x) (int((x).size()))
#define All(x) (x).begin(), (x).end()
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
#define Rep2(p, x) for (int(p) = cur[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 1e5 + 5;
int n, h, t;
int deg[maxn], fa[maxn];
map<int, int> adj[maxn];
typedef map<int, int>::iterator It;
pa q[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  read(n);
  rep(i, 1, n) fa[i] = i;
  rep(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    adj[x][y]++;
    deg[x]++;
    adj[y][x]++;
    deg[y]++;
  }
  rep(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    adj[x][y]++;
    deg[x]++;
    adj[y][x]++;
    deg[y]++;
    if (adj[x][y] >= 2)
      q[t++] = mp(x, y);
  }
  int cnt = 0;
  while (h != t) {
    int x = q[h].w1, y = q[h].w2;
    h++;
    x = find(x);
    y = find(y);
    if (x == y)
      continue;
    cnt++;
    if (adj[x][y] < 2)
      assert(0);
    if (deg[x] > deg[y])
      swap(x, y);
    adj[y][x]--;
    deg[y]--;
    fa[x] = y;
    for (It it = adj[x].begin(); it != adj[x].end(); it++) {
      deg[y]++;
      if (it->w2)
        adj[y][it->w1] += it->w2;
      adj[it->w1][x] -= it->w2;
      adj[it->w1][y] += it->w2;
      if (adj[it->w1][y] >= 2)
        q[t++] = mp(it->w1, y);
    }
  }
  puts(cnt == n - 1 ? "YES" : "NO");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x << 1)
#define w2 second
#define rs (x << 1 | 1)
#define mp make_pair
#define pb push_back
#define mid ((l + r) >> 1)
#define SZ(x) (int((x).size()))
#define All(x) (x).begin(), (x).end()
#define dprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
#define Rep2(p, x) for (int(p) = cur[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 4e6 + 5;
int n, h, t;
int deg[maxn], fa[maxn];
map<int, int> adj[maxn];
typedef map<int, int>::iterator It;
pa q[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  read(n);
  rep(i, 1, n) fa[i] = i;
  rep(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    adj[x][y]++;
    deg[x]++;
    adj[y][x]++;
    deg[y]++;
  }
  rep(i, 1, n - 1) {
    int x, y;
    read(x);
    read(y);
    adj[x][y]++;
    deg[x]++;
    adj[y][x]++;
    deg[y]++;
    if (adj[x][y] >= 2)
      q[t++] = mp(x, y);
  }
  int cnt = 0;
  while (h != t) {
    int x = q[h].w1, y = q[h].w2;
    h++;
    x = find(x);
    y = find(y);
    if (x == y)
      continue;
    cnt++;
    if (adj[x][y] < 2)
      assert(0);
    if (deg[x] > deg[y])
      swap(x, y);
    adj[y][x]--;
    deg[y]--;
    fa[x] = y;
    for (It it = adj[x].begin(); it != adj[x].end(); it++) {
      deg[y]++;
      if (it->w2)
        adj[y][it->w1] += it->w2;
      adj[it->w1][x] -= it->w2;
      adj[it->w1][y] += it->w2;
      if (adj[it->w1][y] >= 2)
        q[t++] = mp(it->w1, y);
    }
  }
  puts(cnt == n - 1 ? "YES" : "NO");
  return 0;
}