Problem p03727 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define maxn 100010

#define fi first
#define se second
typedef pair<int, int> par;

template <typename tp> void read(tp &dig) {
  char ch = getchar();
  dig = 0;
  int flag = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (isdigit(ch))
    dig = dig * 10 + ch - '0', ch = getchar();
  if (flag)
    dig = -dig;
}

set<int>::iterator it;
set<int> s[maxn];
map<par, int> mp;
queue<par> que;
int n, fa[maxn];

inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

int main() {

#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
#endif

  read(n);
  int nn = 2 * n - 2;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1, x, y; i <= nn; ++i) {
    read(x);
    read(y);
    s[x].insert(y), s[y].insert(x);
    ++mp[par(x, y)], ++mp[par(y, x)];
    if (mp[par(x, y)] == 2)
      que.push(par(x, y));
  }
  while (!que.empty()) {
    par tmp = que.front();
    que.pop();
    int x = find(tmp.fi), y = find(tmp.se);
    s[x].erase(y), s[y].erase(x);
    if (s[x].size() < s[y].size())
      swap(x, y);
    for (it = s[y].begin(); it != s[y].end(); it++) {
      int to = *(it);
      mp[par(to, y)] = mp[par(y, to)] = 0;
      s[to].erase(y), s[to].insert(x);
      ++mp[par(to, x)], ++mp[par(x, to)];
      if (mp[par(to, x)] == 2)
        que.push(par(to, x));
      s[x].insert(to);
    }
    fa[y] = x;
    s[y].clear();
  }
  int sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += (find(i) == i);
  if (sum == 1)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 100010

#define fi first
#define se second
typedef pair<int, int> par;

template <typename tp> void read(tp &dig) {
  char ch = getchar();
  dig = 0;
  int flag = 0;
  while (!isdigit(ch)) {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (isdigit(ch))
    dig = dig * 10 + ch - '0', ch = getchar();
  if (flag)
    dig = -dig;
}

set<int>::iterator it;
set<int> s[maxn];
map<par, int> mp;
queue<par> que;
int n, fa[maxn];

inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }

int main() {

  read(n);
  int nn = 2 * n - 2;
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  for (int i = 1, x, y; i <= nn; ++i) {
    read(x);
    read(y);
    s[x].insert(y), s[y].insert(x);
    ++mp[par(x, y)], ++mp[par(y, x)];
    if (mp[par(x, y)] == 2)
      que.push(par(x, y));
  }
  while (!que.empty()) {
    par tmp = que.front();
    que.pop();
    int x = find(tmp.fi), y = find(tmp.se);
    s[x].erase(y), s[y].erase(x);
    if (s[x].size() < s[y].size())
      swap(x, y);
    for (it = s[y].begin(); it != s[y].end(); it++) {
      int to = *(it);
      mp[par(to, y)] = mp[par(y, to)] = 0;
      s[to].erase(y), s[to].insert(x);
      ++mp[par(to, x)], ++mp[par(x, to)];
      if (mp[par(to, x)] == 2)
        que.push(par(to, x));
      s[x].insert(to);
    }
    fa[y] = x;
    s[y].clear();
  }
  int sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += (find(i) == i);
  if (sum == 1)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}