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

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define orep(i, n) for (long long i = 1; i <= (n); ++i)     // one rep
#define drep(i, n) for (long long i = (n)-1; i >= 0; --i)   // down rep
#define srep(i, s, t) for (long long i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end()

// range
#define rrng(a) (a).rbegin(), (a).rend() // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;

template <typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template <typename T> void pv(vector<T> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << v << ",";
  }
  cout << "]" << endl;
}

template <> void pv(vector<P> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << "<" << v.fi << "," << v.se << ">"
         << ",";
  }
  cout << "]" << endl;
}

template <typename T> void pv2(vector<vector<T>> vec) {
  for (auto &v : vec)
    pv(v);
}

int n;
vvi maze;
vi seen;
int mxlength;
vi p;
int tmp;

int dfs(int x, int length) {
  int sz = maze.at(x).size();
  mxlength++;
  seen.at(x) = 1;
  rep(i, sz) {
    int nx = maze.at(x).at(i);
    if (!seen.at(nx)) {
      dfs(nx, length + 1);
    }
  }
  return length;
}

int main() {
  int n, m;
  cin >> n >> m;

  maze = vvi(n, vi());
  seen = vi(n, 0);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    maze.at(a).eb(b);
    maze.at(b).eb(a);
  }

  int ans = 0;
  rep(i, n) {
    int length = 0;
    mxlength = 0;
    if (!seen.at(i)) {
      dfs(i, length);
      ans = max(ans, mxlength);
    }
  }

  cout << ans << endl;

  return 0;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define orep(i, n) for (long long i = 1; i <= (n); ++i)     // one rep
#define drep(i, n) for (long long i = (n)-1; i >= 0; --i)   // down rep
#define srep(i, s, t) for (long long i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end()

// range
#define rrng(a) (a).rbegin(), (a).rend() // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;

template <typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template <typename T> void pv(vector<T> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << v << ",";
  }
  cout << "]" << endl;
}

template <> void pv(vector<P> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << "<" << v.fi << "," << v.se << ">"
         << ",";
  }
  cout << "]" << endl;
}

template <typename T> void pv2(vector<vector<T>> vec) {
  for (auto &v : vec)
    pv(v);
}

int n;
vvi maze;
vi seen;
int mxlength;
vi p;
int tmp;

int dfs(int x, int length) {
  int sz = maze.at(x).size();
  seen.at(x) = 1;
  tmp = x;
  mxlength = max(length, mxlength);
  rep(i, sz) {
    int nx = maze.at(x).at(i);
    if (!seen.at(nx)) {
      dfs(nx, length + 1);
    }
  }
  return length;
}

int main() {
  int n, m;
  cin >> n >> m;

  maze = vvi(n, vi());
  seen = vi(n, 0);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    maze.at(a).eb(b);
    maze.at(b).eb(a);
  }

  int ans = 0;
  rep(i, n) {
    int length = 0;
    mxlength = 0;
    seen = vi(n, 0);
    if (!seen.at(i)) {
      dfs(i, length);
      ans = max(ans, mxlength);
    }
  }

  cout << ans + 1 << endl;

  return 0;

  return 0;
}