Problem p01352 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

int t[40000], u[40000], v[40000];
#define B 200

class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};

multiset<int> E[40000];
int d[40000];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, k) { scanf("%d%d%d", &t[i], &u[i], &v[i]); }
  set<P> se;
  for (int i = 0; i < k; i += B) {
    set<P> s2, s3;
    for (int j = i; j < k && j - i < B; j++) {
      if (t[j] == 2) {
        if (se.count(P(u[j], v[j]))) {
          se.erase(P(u[j], v[j]));
          s3.insert(P(u[j], v[j]));
        }
      }
    }
    UnionFind uf(n);
    for (auto p : se)
      uf.unite(p.first, p.second);
    rep(j, n) E[j].clear();
    for (auto p : s3) {
      E[uf.find(p.first)].insert(uf.find(p.second));
      E[uf.find(p.second)].insert(uf.find(p.first));
    }
    for (int j = i; j < k && j - i < B; j++) {
      if (t[j] == 1) {
        s2.insert(P(u[j], v[j]));
        u[j] = uf.find(u[j]);
        v[j] = uf.find(v[j]);
        E[u[j]].insert(v[j]);
        E[v[j]].insert(u[j]);
      } else if (t[j] == 2) {
        s2.erase(P(u[j], v[j]));
        u[j] = uf.find(u[j]);
        v[j] = uf.find(v[j]);
        E[u[j]].erase(E[u[j]].find(v[j]));
        E[v[j]].erase(E[v[j]].find(u[j]));
      } else {
        memset(d, 0, sizeof(d));
        queue<int> que;
        d[uf.find(u[j])] = 1;
        que.push(uf.find(u[j]));
        while (!que.empty()) {
          int p = que.front();
          que.pop();
          for (int v : E[p]) {
            if (d[v] == 0) {
              d[v] = 1;
              que.push(v);
            }
          }
        }
        if (d[uf.find(v[j])] == 1)
          puts("YES");
        else
          puts("NO");
      }
    }
    for (auto p : s2)
      se.insert(p);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

int t[40000], u[40000], v[40000];
#define B 200

class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) {
    par = sz = vector<int>(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      sz[i] = 1;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;
    if (sz[x] > sz[y]) {
      par[y] = x;
      sz[x] += sz[y];
    } else {
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return sz[find(x)]; }
};

multiset<int> E[40000];
int d[40000];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, k) { scanf("%d%d%d", &t[i], &u[i], &v[i]); }
  set<P> se; //?????¨?????????
  for (int i = 0; i < k; i += B) {
    set<P> s2;
    for (int j = i; j < k && j - i < B; j++) {
      if (t[j] == 2) {
        if (se.count(P(u[j], v[j]))) {
          se.erase(P(u[j], v[j]));
          s2.insert(P(u[j], v[j]));
        }
      }
    }
    UnionFind uf(n);
    for (auto p : se)
      uf.unite(p.first, p.second);
    rep(j, n) E[j].clear();
    for (int j = i; j < k && j - i < B; j++) {
      if (t[j] == 1) {
        s2.insert(P(u[j], v[j]));
        u[j] = uf.find(u[j]);
        v[j] = uf.find(v[j]);
        E[u[j]].insert(v[j]);
        E[v[j]].insert(u[j]);
      } else if (t[j] == 2) {
        s2.erase(P(u[j], v[j]));
        u[j] = uf.find(u[j]);
        v[j] = uf.find(v[j]);
        E[u[j]].erase(E[u[j]].find(v[j]));
        E[v[j]].erase(E[v[j]].find(u[j]));
      } else {
        memset(d, 0, sizeof(d));
        queue<int> que;
        d[uf.find(u[j])] = 1;
        que.push(uf.find(u[j]));
        while (!que.empty()) {
          int p = que.front();
          que.pop();
          for (int v : E[p]) {
            if (d[v] == 0) {
              d[v] = 1;
              que.push(v);
            }
          }
        }
        if (d[uf.find(v[j])] == 1)
          puts("YES");
        else
          puts("NO");
      }
    }
    for (auto p : s2)
      se.insert(p);
  }
}