Problem p03727 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(2)
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#define N 1000011
using namespace std;
struct point {
  int x, y;
  point() {}
  point(int xx, int yy) {
    x = xx;
    y = yy;
  }
  bool operator<(const point &other) const {
    if (x == other.x)
      return y < other.y;
    return x < other.x;
  }
} que[N];
multiset<int> V[N];
multiset<int>::iterator it;
map<point, int> Map;
int father[N], head, tail, n, ans;
inline int read() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret;
}
int find(int u) {
  if (u == father[u])
    return u;
  father[u] = find(father[u]);
  return father[u];
}
int main() {
  int x, y;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1; i <= n * 2 - 2; i++) {
    x = read(), y = read();
    Map[point(x, y)]++;
    Map[point(y, x)]++;
    V[x].insert(y);
    V[y].insert(x);
    if (Map[point(x, y)] == 2)
      que[++tail] = point(x, y);
  }
  while (head < tail) {
    head++;
    x = que[head].x;
    y = que[head].y;
    x = find(x), y = find(y);
    V[x].erase(y);
    V[y].erase(x);
    if (V[x].size() > V[y].size())
      swap(x, y);
    for (it = V[x].begin(); it != V[x].end(); it++) {
      int t = *it;
      Map[point(x, t)] = Map[point(t, x)] = 0;
      V[y].insert(t);
      V[t].erase(x);
      V[t].insert(y);
      Map[point(y, t)]++;
      Map[point(t, y)]++;
      if (Map[point(y, t)] == 2)
        que[++tail] = point(y, t);
    }
    father[x] = y;
    V[x].clear();
  }
  for (int i = 1; i <= n; i++)
    ans += (i == father[i]);
  if (ans == 1)
    printf("YES");
  else
    printf("NO");
  return 0;
}

Fixed submission:
#pragma GCC optimize(2)
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#define N 1000011
using namespace std;
struct point {
  int x, y;
  point() {}
  point(int xx, int yy) {
    x = xx;
    y = yy;
  }
  bool operator<(const point &other) const {
    if (x == other.x)
      return y < other.y;
    return x < other.x;
  }
} que[N];
set<int> V[N];
set<int>::iterator it;
map<point, int> Map;
int father[N], head, tail, n, ans;
inline int read() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret;
}
int find(int u) {
  if (u == father[u])
    return u;
  father[u] = find(father[u]);
  return father[u];
}
int main() {
  int x, y;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    father[i] = i;
  for (int i = 1; i <= n * 2 - 2; i++) {
    x = read(), y = read();
    Map[point(x, y)]++;
    Map[point(y, x)]++;
    V[x].insert(y);
    V[y].insert(x);
    if (Map[point(x, y)] == 2)
      que[++tail] = point(x, y);
  }
  while (head < tail) {
    head++;
    x = que[head].x;
    y = que[head].y;
    x = find(x), y = find(y);
    V[x].erase(y);
    V[y].erase(x);
    if (V[x].size() > V[y].size())
      swap(x, y);
    for (it = V[x].begin(); it != V[x].end(); it++) {
      int t = *it;
      Map[point(x, t)] = Map[point(t, x)] = 0;
      V[y].insert(t);
      V[t].erase(x);
      V[t].insert(y);
      Map[point(y, t)]++;
      Map[point(t, y)]++;
      if (Map[point(y, t)] == 2)
        que[++tail] = point(y, t);
    }
    father[x] = y;
    V[x].clear();
  }
  for (int i = 1; i <= n; i++)
    ans += (i == father[i]);
  if (ans == 1)
    printf("YES");
  else
    printf("NO");
  return 0;
}