Problem p01352 (C++)

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

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

const int X = 200;

int N, Q;
int T[44444], A[44444], B[44444], id[44444];

int ans[44444];

int col[44444];
vpint G[44444];
bool ex[44444];

vpint GG[44444];
bool used[44444];
bool rel[44444];
bool dfs(int v, int t, vint &lis) {
  used[v] = true;
  lis.pb(v);
  if (v == t)
    return true;

  for (auto &e : GG[v]) {
    if (!ex[e.se])
      continue;
    if (used[e.fi])
      continue;
    if (dfs(e.fi, t, lis))
      return true;
  }
  return false;
}
void q(int ql, int qr) {
  memset(col, -1, sizeof(col));

  for (int i = ql; i < qr; i++)
    if (T[i] != 3)
      rel[id[i]] = true;

  int n = 0;
  for (int i = 0; i < N; i++) {
    if (col[i] != -1)
      continue;
    queue<int> que;
    col[i] = n++;
    que.push(i);
    while (que.size()) {
      int v = que.front();
      que.pop();
      for (auto &e : G[v]) {
        if (ex[e.se] && col[e.fi] == -1 && !rel[e.se]) {
          col[e.fi] = col[v];
          que.push(e.fi);
        }
      }
    }
  }

  rep(i, n) GG[i].clear();
  for (int i = ql; i < qr; i++)
    if (T[i] != 3) {
      GG[col[A[i]]].pb({col[B[i]], id[i]});
      GG[col[B[i]]].pb({col[A[i]], id[i]});
    }

  for (int i = ql; i < qr; i++) {
    if (T[i] != 3) {
      ex[id[i]] ^= 1;
    } else {
      vint lis;
      ans[i] = dfs(col[A[i]], col[B[i]], lis);
      for (auto u : lis)
        used[u] = 0;
    }
  }

  for (int i = ql; i < qr; i++)
    if (T[i] != 3)
      rel[id[i]] = false;
}

signed main() {
  scanf("%lld%lld", &N, &Q);
  rep(i, Q) scanf("%lld%lld%lld", &T[i], &A[i], &B[i]);

  vector<pint> v;
  rep(i, Q) if (T[i] != 3) v.pb({A[i], B[i]});
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  rep(i, Q) if (T[i] != 3) id[i] =
      lower_bound(all(v), pint(A[i], B[i])) - v.begin();

  rep(i, Q) if (T[i] != 3) {
    G[A[i]].pb({B[i], id[i]});
    G[B[i]].pb({A[i], id[i]});
  }

  for (int i = 0; i * X < Q; i++) {
    q(i * X, min(Q, (i + 1) * X));
  }
  rep(i, Q) if (T[i] == 3) puts(ans[i] ? "YES" : "NO");
  return 0;
}

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

#define int long long

typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

const int X = 400;

int N, Q;
int T[44444], A[44444], B[44444], id[44444];

int ans[44444];

int col[44444];
vpint G[44444];
bool ex[44444];

vpint GG[44444];
bool used[44444];
bool rel[44444];
bool dfs(int v, int t, vint &lis) {
  used[v] = true;
  lis.pb(v);
  if (v == t)
    return true;

  for (auto &e : GG[v]) {
    if (!ex[e.se])
      continue;
    if (used[e.fi])
      continue;
    if (dfs(e.fi, t, lis))
      return true;
  }
  return false;
}
void q(int ql, int qr) {
  memset(col, -1, sizeof(col));

  for (int i = ql; i < qr; i++)
    if (T[i] != 3)
      rel[id[i]] = true;

  int n = 0;
  for (int i = 0; i < N; i++) {
    if (col[i] != -1)
      continue;
    queue<int> que;
    col[i] = n++;
    que.push(i);
    while (que.size()) {
      int v = que.front();
      que.pop();
      for (auto &e : G[v]) {
        if (ex[e.se] && col[e.fi] == -1 && !rel[e.se]) {
          col[e.fi] = col[v];
          que.push(e.fi);
        }
      }
    }
  }

  rep(i, n) GG[i].clear();
  for (int i = ql; i < qr; i++)
    if (T[i] != 3) {
      GG[col[A[i]]].pb({col[B[i]], id[i]});
      GG[col[B[i]]].pb({col[A[i]], id[i]});
    }

  for (int i = ql; i < qr; i++) {
    if (T[i] != 3) {
      ex[id[i]] ^= 1;
    } else {
      vint lis;
      ans[i] = dfs(col[A[i]], col[B[i]], lis);
      for (auto u : lis)
        used[u] = 0;
    }
  }

  for (int i = ql; i < qr; i++)
    if (T[i] != 3)
      rel[id[i]] = false;
}

signed main() {
  scanf("%lld%lld", &N, &Q);
  rep(i, Q) scanf("%lld%lld%lld", &T[i], &A[i], &B[i]);

  vector<pint> v;
  rep(i, Q) if (T[i] != 3) v.pb({A[i], B[i]});
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  rep(i, Q) if (T[i] != 3) id[i] =
      lower_bound(all(v), pint(A[i], B[i])) - v.begin();

  rep(i, Q) if (T[i] != 3) {
    G[A[i]].pb({B[i], id[i]});
    G[B[i]].pb({A[i], id[i]});
  }

  for (int i = 0; i * X < Q; i++) {
    q(i * X, min(Q, (i + 1) * X));
  }
  rep(i, Q) if (T[i] == 3) puts(ans[i] ? "YES" : "NO");
  return 0;
}