Problem p03727 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
inline int read() {
  int f = 1, ans = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
const int MAXN = 200001;
map<pair<int, int>, int> M;
set<int> s[MAXN];
queue<pair<int, int>> que;
void add(int u, int v) {
  s[u].insert(v), s[v].insert(u);
  if (u > v)
    swap(u, v);
  M[make_pair(u, v)]++;
  if (M[make_pair(u, v)] == 2)
    que.push(make_pair(u, v));
  return;
}
void del(int u, int v) {
  s[u].erase(v), s[v].erase(u);
  if (u > v)
    swap(u, v);
  M.erase(make_pair(u, v));
}
set<int>::iterator it, it1;
int n, f[MAXN];
int find(int x) {
  if (f[x] == x)
    return x;
  return f[x] = find(f[x]);
}
bool solve() {
  int tot = 0;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  while (!que.empty()) {
    int u = que.front().first, v = que.front().second;
    que.pop();
    tot++;
    u = find(u), v = find(v);
    if (s[u].size() < s[v].size())
      swap(u, v);
    del(u, v);
    f[v] = u;
    for (it = s[v].begin(); it != s[v].end();) {
      it1 = it;
      it++;
      int end = *it1;
      end = find(end);
      del(v, end), add(u, end);
    }
    //		s[v].clear();
  }
  return tot == (n - 1);
}
int main() {
  freopen("tree.in", "r", stdin);
  freopen("tree.out", "w", stdout);
  n = read();
  for (int i = 1; i < n; i++)
    add(read(), read());
  for (int i = 1; i < n; i++)
    add(read(), read());
  if (solve()) {
    printf("YES\n");
    return 0;
  }
  printf("NO\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
inline int read() {
  int f = 1, ans = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
const int MAXN = 200001;
map<pair<int, int>, int> M;
set<int> s[MAXN];
queue<pair<int, int>> que;
void add(int u, int v) {
  s[u].insert(v), s[v].insert(u);
  if (u > v)
    swap(u, v);
  M[make_pair(u, v)]++;
  if (M[make_pair(u, v)] == 2)
    que.push(make_pair(u, v));
  return;
}
void del(int u, int v) {
  s[u].erase(v), s[v].erase(u);
  if (u > v)
    swap(u, v);
  M.erase(make_pair(u, v));
}
set<int>::iterator it, it1;
int n, f[MAXN];
int find(int x) {
  if (f[x] == x)
    return x;
  return f[x] = find(f[x]);
}
bool solve() {
  int tot = 0;
  for (int i = 1; i <= n; i++)
    f[i] = i;
  while (!que.empty()) {
    int u = que.front().first, v = que.front().second;
    que.pop();
    tot++;
    u = find(u), v = find(v);
    if (s[u].size() < s[v].size())
      swap(u, v);
    del(u, v);
    f[v] = u;
    for (it = s[v].begin(); it != s[v].end();) {
      it1 = it;
      it++;
      int end = *it1;
      end = find(end);
      del(v, end), add(u, end);
    }
    //		s[v].clear();
  }
  return tot == (n - 1);
}
int main() {
  // 	freopen("tree.in","r",stdin);
  // 	freopen("tree.out","w",stdout);
  n = read();
  for (int i = 1; i < n; i++)
    add(read(), read());
  for (int i = 1; i < n; i++)
    add(read(), read());
  if (solve()) {
    printf("YES\n");
    return 0;
  }
  printf("NO\n");
  return 0;
}