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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define prec(i) cout << fixed << setprecision(i)

void output_yn(bool isYes) {
  if (isYes)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const long long MOD = 1000000007;

long long fact(long long k) {
  long long sum = 1;
  for (long long i = 2; i <= k; i++)
    sum *= i;

  return sum;
}

long long fact_mod(long long k) {
  long long sum = 1;
  for (long long i = 2; i <= k; i++)
    sum = (sum * i) % MOD;

  return sum;
}

int ctoi(char c) {
  if (c >= '0' && c <= '9')
    return c - '0';
  else
    return -1;
}

long long pow_mod(int a, int b) {
  long long ans = 1;
  for (int i = 0; i < b; i++)
    ans = (ans * a) % MOD;

  return ans;
}

long long powll(long long a, long long b) {
  long long ans = 1;
  for (long long i = 0; i < b; i++)
    ans = ans * a;

  return ans;
}

long long modinv(long long a) {

  long long b = MOD, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0)
    u += MOD;

  return u;
}

long long _gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);

  if (a % b == 0)
    return b;
  else
    return _gcd(b, a % b);
}

long long _lcm(long long a, long long b) {
  return (((a * b) % MOD) * modinv(_gcd(a, b))) % MOD;
}

long long ncr(long long n, long long r) {
  long long sum = 1;
  for (long long i = r + 1; i <= n; i++)
    sum *= i;

  return sum;
}

vector<vector<int>> graph;
vector<bool> seen;
int grpCnt = 0;

void dfs(const vector<vector<int>> &graph, int v) {
  seen[v] = true;
  grpCnt++;
  for (auto next_v : graph[v]) {
    if (seen[next_v])
      continue;
    dfs(graph, next_v);
  }
}

int main() {
  int n, m, max = 0;
  cin >> n >> m;

  graph = vector<vector<int>>(n);
  seen.assign(n, false);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
  }

  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      grpCnt = 0;
      dfs(graph, i);
      if (grpCnt > max)
        max = grpCnt;
    }
  }

  cout << max << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define prec(i) cout << fixed << setprecision(i)

void output_yn(bool isYes) {
  if (isYes)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const long long MOD = 1000000007;

long long fact(long long k) {
  long long sum = 1;
  for (long long i = 2; i <= k; i++)
    sum *= i;

  return sum;
}

long long fact_mod(long long k) {
  long long sum = 1;
  for (long long i = 2; i <= k; i++)
    sum = (sum * i) % MOD;

  return sum;
}

int ctoi(char c) {
  if (c >= '0' && c <= '9')
    return c - '0';
  else
    return -1;
}

long long pow_mod(int a, int b) {
  long long ans = 1;
  for (int i = 0; i < b; i++)
    ans = (ans * a) % MOD;

  return ans;
}

long long powll(long long a, long long b) {
  long long ans = 1;
  for (long long i = 0; i < b; i++)
    ans = ans * a;

  return ans;
}

long long modinv(long long a) {

  long long b = MOD, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= MOD;
  if (u < 0)
    u += MOD;

  return u;
}

long long _gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);

  if (a % b == 0)
    return b;
  else
    return _gcd(b, a % b);
}

long long _lcm(long long a, long long b) {
  return (((a * b) % MOD) * modinv(_gcd(a, b))) % MOD;
}

long long ncr(long long n, long long r) {
  long long sum = 1;
  for (long long i = r + 1; i <= n; i++)
    sum *= i;

  return sum;
}

vector<vector<int>> graph;
vector<bool> seen;
int grpCnt = 0;

void dfs(const vector<vector<int>> graph, int v) {
  seen[v] = true;
  grpCnt++;
  for (auto next_v : graph[v]) {
    if (seen[next_v])
      continue;
    dfs(graph, next_v);
  }
}

int main() {
  int n, m, max = 0;
  cin >> n >> m;

  graph = vector<vector<int>>(n);
  seen.assign(n, false);

  map<pair<int, int>, bool> isAdded;

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    graph[a - 1].push_back(b - 1);
    graph[b - 1].push_back(a - 1);
  }

  for (int i = 0; i < n; i++) {
    if (!seen[i]) {
      grpCnt = 0;
      dfs(graph, i);
      if (grpCnt > max)
        max = grpCnt;
    }
  }

  cout << max << endl;

  return 0;
}