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


#include <bits/stdc++.h>
#define ll long long int
#define all(x) x.begin(), x.end()
using namespace std;
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  if (g != 1)
    cout << "Inverse doesn't exist";

  // If a and m are relatively prime, then modulo inverse
  // is a^(m-2) mode m

  return power(a, m - 2, m);
}

ll fact[1000];
void fac(ll m) {
  ll i;
  fact[0] = 1;
  for (i = 1; i < 1000; i++) {
    fact[i] = (i * fact[i - 1]) % m;
  }
}

ll comb(ll n, ll k, ll m) {
  return (fact[n] * modInverse(fact[k], m) % m * modInverse(fact[n - k], m) %
          m) %
         m;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return a.second > b.second;
}
void SieveOfEratosthenes(ll n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
void dfs(ll i, vector<ll> &visited, vector<vector<ll>> &adj) {
  visited[i] = 1;
  for (auto x : adj[i]) {
    if (!visited[x])
      dfs(x, visited, adj);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  // cin>>t;
  t = 1;

  while (t-- > 0) {
    ll n, m;
    cin >> n >> m;
    ll i;
    vector<vector<ll>> adj(n);
    vector<ll> visited(n, 0);

    for (i = 0; i < m; i++) {
      ll u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    ll ans = 0;
    for (i = 0; i < n; i++) {
      if (!visited[i]) {

        dfs(i, visited, adj);
        ans++;
      }
    }
    cout << ans - 1;
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#define ll long long int
#define all(x) x.begin(), x.end()
using namespace std;
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  if (g != 1)
    cout << "Inverse doesn't exist";

  // If a and m are relatively prime, then modulo inverse
  // is a^(m-2) mode m

  return power(a, m - 2, m);
}

ll fact[1000];
void fac(ll m) {
  ll i;
  fact[0] = 1;
  for (i = 1; i < 1000; i++) {
    fact[i] = (i * fact[i - 1]) % m;
  }
}

ll comb(ll n, ll k, ll m) {
  return (fact[n] * modInverse(fact[k], m) % m * modInverse(fact[n - k], m) %
          m) %
         m;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return a.second > b.second;
}
void SieveOfEratosthenes(ll n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}
void dfs(ll i, vector<ll> &visited, vector<vector<ll>> adj) {
  visited[i] = 1;
  for (auto x : adj[i]) {
    if (!visited[x])
      dfs(x, visited, adj);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  // cin>>t;
  t = 1;

  while (t-- > 0) {
    ll n, m;
    cin >> n >> m;
    ll i;
    vector<vector<ll>> adj(n);
    vector<ll> visited(n, 0);

    for (i = 0; i < m; i++) {
      ll u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
    ll ans = 0;
    for (i = 0; i < n; i++) {
      if (!visited[i]) {

        dfs(0, visited, adj);
        ans++;
      }
    }
    cout << ans - 1;
  }
  return 0;
}