Problem p03727 (C++)

Buggy submission (status: Runtime Error):
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{}());

int rnd(int x) { return mrand() % x; }

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define TASK "text"

const int inf = (int)1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld)-1.0);

const int mod = (int)1e9 + 7;

void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}

int mult(int x, int y) { return (long long)x * y % mod; }

int power(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}

void precalc() {}

const int maxn = (int)1e5 + 10;
int n;

int col[maxn];

vector<pair<int, int>> st;

set<pair<int, int>> edges[2];

void adde(int iter, int s, int t) {
  s = col[s], t = col[t];
  if (s > t) {
    swap(s, t);
  }
  if (edges[!iter].count(mp(s, t))) {
    st.pb(mp(s, t));
    edges[!iter].erase(mp(s, t));
    return;
  }
  edges[iter].insert(mp(s, t));
}

vector<int> *inside[maxn];
vector<vector<int>> es[2];

int read() {
  if (scanf("%d", &n) < 1) {
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    col[i] = i;
    inside[i] = new vector<int>(1, i);
  }
  st.clear();
  edges[0].clear();
  edges[1].clear();

  for (int iter = 0; iter < 2; ++iter) {
    es[iter] = vector<vector<int>>(n);
    for (int i = 0; i < n - 1; ++i) {
      int s, t;
      scanf("%d%d", &s, &t);
      --s, --t;
      adde(iter, s, t);

      es[iter][s].pb(t);
      es[iter][t].pb(s);
    }
  }
  return 1;
}

void solve() {
  for (int l = 0; l < sz(st); ++l) {
    int s = st[l].first, t = st[l].second;
    assert(col[s] == s && col[t] == t);
    if (sz(*inside[s]) > sz(*inside[t])) {
      swap(s, t);
    }

    auto &vs = *inside[s], &vt = *inside[t];

    vector<pair<int, int>> toadd[2];

    for (int x : vs) {
      for (int type = 0; type < 2; ++type) {
        for (int y : es[type][x]) {
          int cx = col[x], cy = col[y];
          if (cx > cy) {
            swap(cx, cy);
          }
          auto now = mp(cx, cy);
          if (edges[type].count(now)) {
            edges[type].erase(now);
            toadd[type].pb(now);
          }
        }
      }
    }

    for (int x : vs) {
      col[x] = t;
      vt.pb(x);
    }

    for (int type = 0; type < 2; ++type) {
      for (auto p : toadd[type]) {
        adde(type, p.first, p.second);
      }
    }
  }
  if (sz(st) < n - 1) {
    printf("NO\n");
    return;
  }
  printf("YES\n");
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Fixed submission:
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{}());

int rnd(int x) { return mrand() % x; }

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define TASK "text"

const int inf = (int)1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld)-1.0);

const int mod = (int)1e9 + 7;

void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}

int mult(int x, int y) { return (long long)x * y % mod; }

int power(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}

void precalc() {}

const int maxn = (int)1e5 + 10;
int n;

int col[maxn];

vector<pair<int, int>> st;

set<pair<int, int>> edges[2];

void adde(int iter, int s, int t) {
  s = col[s], t = col[t];
  if (s > t) {
    swap(s, t);
  }
  if (edges[!iter].count(mp(s, t))) {
    st.pb(mp(s, t));
    edges[!iter].erase(mp(s, t));
    return;
  }
  edges[iter].insert(mp(s, t));
}

vector<int> *inside[maxn];
vector<vector<int>> es[2];

int read() {
  if (scanf("%d", &n) < 1) {
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    col[i] = i;
    inside[i] = new vector<int>(1, i);
  }
  st.clear();
  edges[0].clear();
  edges[1].clear();

  for (int iter = 0; iter < 2; ++iter) {
    es[iter] = vector<vector<int>>(n);
    for (int i = 0; i < n - 1; ++i) {
      int s, t;
      scanf("%d%d", &s, &t);
      --s, --t;
      adde(iter, s, t);

      es[iter][s].pb(t);
      es[iter][t].pb(s);
    }
  }
  return 1;
}

void solve() {
  for (int l = 0; l < sz(st); ++l) {
    int s = st[l].first, t = st[l].second;
    s = col[s], t = col[t];
    if (sz(*inside[s]) > sz(*inside[t])) {
      swap(s, t);
    }

    auto &vs = *inside[s], &vt = *inside[t];

    vector<pair<int, int>> toadd[2];

    for (int x : vs) {
      for (int type = 0; type < 2; ++type) {
        for (int y : es[type][x]) {
          int cx = col[x], cy = col[y];
          if (cx > cy) {
            swap(cx, cy);
          }
          auto now = mp(cx, cy);
          if (edges[type].count(now)) {
            edges[type].erase(now);
            toadd[type].pb(now);
          }
        }
      }
    }

    for (int x : vs) {
      col[x] = t;
      vt.pb(x);
    }

    for (int type = 0; type < 2; ++type) {
      for (auto p : toadd[type]) {
        adde(type, p.first, p.second);
      }
    }
  }
  if (sz(st) < n - 1) {
    printf("NO\n");
    return;
  }
  printf("YES\n");
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}