Problem p03691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 19;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;

bool dchk[405][405];
bool tdchk[405][405];
vector<pii> Vu;
void update(int a, int b, int N) {
  if (dchk[a][b] || tdchk[a][b])
    return;
  Vu.emplace_back(a, b);
  tdchk[a][b] = tdchk[b][a] = true;
  if (a == b)
    for (int i = 1; i <= N; i++)
      update(a, i, N);
}

int main() {
  int N, M, i, j, k;
  scanf("%d %d", &N, &M);
  while (M--) {
    int t1, t2;
    scanf("%d %d", &t1, &t2);
    // dchk[t1][t2] = dchk[t2][t1] = true;
    for (i = 1; i <= N; i++) {
      if (dchk[t1][i])
        update(t2, i, N);
      if (dchk[t2][i])
        update(t1, i, N);
    }
    update(t1, t2, N);
    for (auto it : Vu)
      dchk[it.first][it.second] = dchk[it.second][it.first] = true;
  }

  int ans = 0;
  for (i = 1; i <= N; i++)
    for (j = i + 1; j <= N; j++)
      if (!dchk[i][j])
        ans++;
  return !printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 19;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;

bool dchk[405][405];
bool tdchk[405][405];
vector<pii> Vu;
void update(int a, int b, int N) {
  if (dchk[a][b] || tdchk[a][b])
    return;
  Vu.emplace_back(a, b);
  tdchk[a][b] = tdchk[b][a] = true;
  if (a == b)
    for (int i = 1; i <= N; i++)
      update(a, i, N);
}

int main() {
  int N, M, i, j, k;
  scanf("%d %d", &N, &M);
  while (M--) {
    int t1, t2;
    scanf("%d %d", &t1, &t2);
    // dchk[t1][t2] = dchk[t2][t1] = true;
    for (i = 1; i <= N; i++) {
      if (dchk[t1][i])
        update(t2, i, N);
      if (dchk[t2][i])
        update(t1, i, N);
    }
    update(t1, t2, N);
    for (auto it : Vu)
      dchk[it.first][it.second] = dchk[it.second][it.first] = true;
    Vu.clear();
  }

  int ans = 0;
  for (i = 1; i <= N; i++)
    for (j = i + 1; j <= N; j++)
      if (!dchk[i][j])
        ans++;
  return !printf("%d\n", ans);
}