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

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n, q;
int type[40005], f[40005], t[40005];
int B = 200;
set<int> G[40005];
multiset<int> G2[405];
multiset<int> bridge[405];
set<int> tgt;
int cmp[40005];
bool used[405];

void dfs(int v, int c) {
  cmp[v] = c;
  for (set<int>::iterator it = G[v].begin(); it != G[v].end(); it++) {
    if (cmp[*it] == -1) {
      if (tgt.find(v) == tgt.end() || tgt.find(*it) == tgt.end()) {
        dfs(*it, c);
      } else {
        bridge[c].insert(*it);
      }
    }
  }
}

void dfs2(int v) {
  used[v] = true;
  for (multiset<int>::iterator it = G2[v].begin(); it != G2[v].end(); it++) {
    if (!used[*it]) {
      dfs2(*it);
    }
  }
}

int main(void) {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &type[i], &f[i], &t[i]);
  }
  for (int i = 0; i < q; i++) {
    if (i % B == 0) {
      memset(cmp, -1, sizeof(cmp));
      tgt.clear();
      for (int j = 0; j < B * 2; j++) {
        G2[j].clear();
        bridge[j].clear();
      }
      for (int j = 0; (j < B && i + j < q); j++) {
        tgt.insert(f[i + j]);
        tgt.insert(t[i + j]);
      }
      int k = 0;
      for (set<int>::iterator it = tgt.begin(); it != tgt.end(); it++) {
        if (cmp[*it] == -1) {
          dfs(*it, k++);
        }
      }
      for (int j = 0; j < k; j++) {
        for (multiset<int>::iterator it = bridge[j].begin();
             it != bridge[j].end(); it++) {
          int nv = cmp[*it];
          G2[j].insert(nv);
          G2[nv].insert(j);
        }
      }
    }
    if (type[i] == 1) {
      G[f[i]].insert(t[i]);
      G[t[i]].insert(f[i]);
      G2[cmp[f[i]]].insert(cmp[t[i]]);
      G2[cmp[t[i]]].insert(cmp[f[i]]);
    }
    if (type[i] == 2) {
      G[f[i]].erase(t[i]);
      G[t[i]].erase(f[i]);
      multiset<int>::iterator it = G2[cmp[f[i]]].lower_bound(cmp[t[i]]);
      if (it != G2[cmp[f[i]]].end() && *it == cmp[t[i]]) {
        G2[cmp[f[i]]].erase(it);
      }
      it = G2[cmp[t[i]]].lower_bound(cmp[f[i]]);
      if (it != G2[cmp[t[i]]].end() && *it == cmp[f[i]]) {
        G2[cmp[t[i]]].erase(it);
      }
    }
    if (type[i] == 3) {
      memset(used, false, sizeof(used));
      dfs2(cmp[f[i]]);
      printf("%s\\n", used[cmp[t[i]]] ? "YES" : "NO");
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

map<int, map<int, bool>> ok;
int n, k;
vector<int> G[40001];
bool used[40001];

bool dfs(int f, int t) {
  memset(used, false, sizeof(used));
  queue<int> que;
  que.push(f);
  while (que.size()) {
    int v = que.front();
    que.pop();
    if (used[v])
      continue;
    used[v] = true;
    if (v == t)
      return true;
    for (int i = 0; i < G[v].size(); i++) {
      if (!used[G[v][i]] && ok[v][G[v][i]])
        que.push(G[v][i]);
    }
  }
  return false;
}

int main(void) {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a == 1) {
      ok[b][c] = true;
      ok[c][b] = true;
      G[b].push_back(c);
      G[c].push_back(b);
    } else if (a == 2) {
      ok[b][c] = false;
      ok[c][b] = false;
    } else if (a == 3) {
      if (dfs(b, c))
        puts("YES");
      else
        puts("NO");
    }
  }
  return 0;
}