alexjercan/bugnet huggingface dataset:
Problem p02419 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int main() {
  int cnt = 0, match = 0;
  string w, t[300];
  cin >> w;
  string tt;
  int a = 0;
  while (cin >> tt, tt != "END_OF_TEXT") {
    t[a] = tt;
    a++;
  }
  for (int j = 0; j < a; j++) {
    for (int i = 0; i < t[j].size(); i++) {
      if (w.size() != t[j].size()) {
        break;
      }
      if (32 + w[match] == t[j][i] || w[match] == t[j][i] ||
          w[match] == t[j][i] + 32) {
        match++;
      } else {
        match = 0;
      }
      if (match == w.size()) {
        cnt++;
        match = 0;
      }
    }
    match = 0;
  }
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int cnt = 0, match = 0;
  string w, t[150];
  cin >> w;
  string tt;
  int a = 0;
  while (cin >> tt, tt != "END_OF_TEXT") {
    t[a] = tt;
    a++;
  }
  for (int j = 0; j < a; j++) {
    for (int i = 0; i < t[j].size(); i++) {
      if (w.size() != t[j].size()) {
        break;
      }
      if (32 + w[match] == t[j][i] || w[match] == t[j][i] ||
          w[match] == t[j][i] + 32) {
        match++;
      } else {
        match = 0;
      }
      if (match == w.size()) {
        cnt++;
        match = 0;
      }
    }
    match = 0;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  string W, T;
  int i, a = 0;
  cin >> W;
  cin >> T;
  while (T != "END_OF_TEXT") {
    for (i = 0; i < T.size(); i++) {
      if ('A' <= T[i] && T[i] <= 'Z') {
        T[i] += 'a' - 'A';
      }
    }
    if (W == T) {
      a++;
    }
    cin >> T;
  }
  cout << a << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;
int main() {
  string W, T;
  int a = 0;
  cin >> W;
  cin >> T;
  while (T != "END_OF_TEXT") {
    for (int i = 0; i < T.size(); i++) {
      if ('A' <= T[i] && T[i] <= 'Z') {
        T[i] += 'A' - 'a';
      }
      if (W == T) {
        a++;
      }
    }
  }
  cout << a << endl;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

int main() {
  char word[12];
  char eot[12] = "END_OF_TEXT";
  int i = 1;
  int count = 0;
  char c;
  bool b;

  word[0] = ' ';

  scanf("%c", &c);
  while (c - 10) {
    if (96 < c && c < 123) {
      word[i] = c - 32;
    } else {
      word[i] = c;
    }
    i++;
    scanf("%c", &c);
  }
  word[i] = ' ';
  i++;

  int j = 0, k = 0;

  c = ' ';
  char t = 'a';

  while (true) {
    if (96 < c && c < 123) {
      c -= 32;
      b = false;
    } else {
      if (c == '\\n') {
        c = ' ';
      }
      b = true;
    }

    if (!(t == c && t == ' ')) {
      if (word[j] == c) {
        j++;
        if (j == i) {
          count++;
          j = 1;
        }
      } else {
        j = 0;
      }

      if (eot[k] == c && b) {
        k++;
        if (k == 11) {
          printf("%d\\n", count);
          return 0;
        }
      } else {
        k = 0;
      }
    }
    t = c;
    scanf("%c", &c);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int main() {
  char word[12];
  char eot[12] = "end_of_text";
  int i = 1;
  int count = 0;
  char c;
  bool b;

  word[0] = ' ';

  scanf("%c", &c);
  while (c - 10) {
    if (64 < c && c < 91) {
      word[i] = c + 32;
    } else {
      word[i] = c;
    }
    i++;
    scanf("%c", &c);
  }
  word[i] = ' ';
  i++;

  int j = 0, k = 0;

  c = ' ';

  while (true) {
    if (64 < c && c < 91) {
      c += 32;
      b = false;
    } else if (c == '\\n') {
      c = ' ';
      b = true;
    }

    if (word[j] == c) {
      j++;
      if (j == i) {
        count++;
        j = 1;
      }
    } else {
      j = 0;
    }

    if (eot[k] == c) {
      k++;
      if (k == 11 && b) {
        printf("%d\\n", count);
        return 0;
      }
    } else {
      k = 0;
    }
    scanf("%c", &c);
  }
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  int ans = 0;
  string w;
  cin >> w;
  for (int i = 0; i < w.size(); i++) {
    if (isupper(w[i])) {
      w[i] = tolower(w[i]);
    }
  }
  string t;
  while (true) {
    cin >> t;
    if (t == "END_OF_TEXT")
      break;

    for (int j = 0; j < t.size(); j++) {
      if (isupper(t[j])) {
        t[j] = tolower(t[j]);
      }
    }

    if (t == w) {
      ans++;
    }
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int ans = 0;
  string w;
  cin >> w;
  for (int i = 0; i < w.size(); i++) {
    if (isupper(w[i])) {
      w[i] = tolower(w[i]);
    }
  }
  string t;
  while (true) {
    cin >> t;
    if (t == "END???OF???TEXT")
      break;

    for (int j = 0; j < t.size(); j++) {
      if (isupper(t[j])) {
        t[j] = tolower(t[j]);
      }
    }

    if (t == w) {
      ans++;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int count = 0;
  string W, T;
  cin >> W;
  transform(W.begin(), W.end(), W.begin(), ::tolower);
  while (1) {
    cin >> T;
    if (T.find("END_OF_TEXT") != string::npos)
      break;
    transform(T.begin(), T.end(), T.begin(), ::tolower);
    if (T == W)
      count++;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int count = 0;
  string W, T;
  cin >> W;
  string b = W;
  transform(b.begin(), b.end(), W.begin(), ::tolower);
  while (1) {
    cin >> T;
    string a = T;
    transform(a.begin(), a.end(), T.begin(), ::tolower);
    if (T.find(W) != string::npos)
      count++;
    if (T.find("END_OF_TEXT") != string::npos)
      break;
  }
  cout << count << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
using namespace std;

#include <cstdio>
#include <cstring>
#include <string>

string Upper(string s) {
  string t = s;
  for (int i = 0; i < s.size(); i++) {
    t[i] = toupper(s[i]);
  }
  return t;
}

int main() {
  string W, T;
  int count = 0;

  cin >> W;
  W = Upper(W);

  while (1) {
    cin >> T;

    if (T == "END_OF_TEXT")
      break;
    T = Upper(T);
    if (T == W)
      count++;
  }

  cout << count << "\\n";

  return 0;
}

Error: -11

Fixed submission:

#include <cctype>
#include <iostream>
using namespace std;

#include <cstdio>
#include <cstring>
#include <string>

string Upper(string s) {
  string t = s;
  for (int i = 0; s.size(); i++) {
    t[i] = toupper(s[i]);
  }
  return t;
}

int main() {
  string W, T;
  int count = 0;

  cin >> W;
  W = Upper(W);

  while (1) {
    cin >> T;

    if (T == "END_OF_TEXT")
      break;
    T = Upper(T);
    if (T == W)
      count++;
  }

  cout << count << "\\n";

  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
void tim(string &a) {

  for (int i = 0; a[i]; i++) {
    if (a[i] < 97) {
      a[i] = (char)(a[i] + 32);
    }
  }
}

int main() {
  string w, t;
  cin >> w;
  tim(w);
  cin >> t;
  int d = 0;
  while (t != "END_OF_TEXT") {
    tim(t);
    if (w == t)
      d++;
    cin >> t;
  }
  cout << d << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
void tim(string &a) {

  for (int i = 0; a[i]; i++) {
    if (a[i] < 97) {
      a[i] = (char)(a[i] + 32);
    }
  }
}
int main() {
  string w, t;
  cin >> w;
  int n = w.size();
  tim(w);
  getline(cin, t);
  int d = 0;
  while (t != "END_OF_TEXT") {
    tim(t);
    int x = 0;
    while (x != -1) {
      x = t.find(w);
      if (x != -1 && t[x - 1] == 20 && t[x + n] == 20) {
        d++;
        t.replace(x, n, "");
      }
    }
    getline(cin, t);
  }
  cout << d << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <stdio.h>

int main(void) {

  char word[11] = "0000000000";
  char t[11] = "0000000000";
  char end[] = "END_OF_TEXT";
  int i = 0;
  int j = 0;
  int k;
  int ans = 0;
  int count = 0;
  int endcount = 1;

  while (1) {
    scanf("%c", &word[i]);

    if (word[i] == '\\n') {
      break;
    }

    if (islower(word[i])) {
      word[i] = toupper(word[i]);
    }

    i++;
  }

  while (1) {
    while (1) {
      scanf("%c", &t[j]);

      if (t[j] == '\\n' || t[j] == ' ') {
        break;
      }

      j++;
    }

    for (k = 0; k < 11; k++) {
      if (t[k] != end[k]) {
        endcount = 0;
      }
    }

    if (endcount == 1) {
      break;
    }

    endcount = 1;

    for (k = 0; k < j; k++) {
      if (islower(t[k])) {
        t[k] = toupper(t[k]);
      }

      if (t[k] == word[k]) {
        ans++;
      } else {
        ans = 0;
      }
    }

    if (ans == i && (t[j] == '\\n' || t[j] == ' ')) {
      count++;
    }

    ans = 0;

    j = 0;
  }

  printf("%d\\n", count);

  return 0;
}

Error: -11

Fixed submission:

#include <cctype>
#include <stdio.h>

int main(void) {

  char word[11] = "0000000000";
  char t[11] = "0000000000";
  char te[11] = "0000000000";
  char end[] = "END_OF_TEXT";
  int i = 0;
  int j = 0;
  int ans = 0;
  int count = 0;
  int endcount = 1;

  while (1) {
    scanf("%c", &word[i]);

    if (word[i] == '\\n') {
      break;
    }

    if (islower(word[i])) {
      word[i] = toupper(word[i]);
    }

    i++;
  }

  while (1) {
    while (1) {
      scanf("%c", &t[j]);
      te[j] = t[j];

      if (islower(t[j])) {
        t[j] = toupper(t[j]);
      }

      if (t[j] == '\\n' || t[j] == ' ') {
        break;
      }

      if (t[j] == word[j]) {
        ans++;
      } else {
        ans = 0;
      }

      j++;
    }

    if (ans == i && (t[j] == '\\n' || t[j] == ' ')) {
      count++;
    }

    ans = 0;

    for (j = 0; j < 11; j++) {
      if (te[j] != end[j]) {
        endcount = 0;
      }
    }

    if (endcount == 1) {
      break;
    }

    endcount = 1;
    j = 0;
  }

  printf("%d\\n", count);

  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

string toUpper(string s) {
  string t = s;
  for (int i = 0; i < s.size(); i++) {
    t[i] = toupper(s[i]);
  }

  return t;
}

int main() {
  int count = 0;
  string w, str;

  cin >> w >> str;
  w = toUpper(w);
  str = toUpper(str);

  while (1) {
    if (str == w)
      count++;

    cin >> str;
    if (str == "END_OF_TEXT")
      break;
    str = toUpper(str);
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int count = 0;
  string w, str;

  cin >> w >> str;

  while (str != "END_OF_TEXT") {
    if (str == w)
      count++;
  }

  cout << count << endl;

  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int count = 0;
  string w;
  string t;
  cin >> w;

  while (1) {
    cin >> t;
    if (t == "END_OF_TEXT")
      break;
    for (int i = 0; i < t.length(); i++) {
      t[i] = tolower(t[i]);
    }
    if (w == t) {
      count++;
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int count = 0;
  string w;
  string t;
  cin >> w;

  while (1) {
    cin >> t;
    std::transform(t.begin(), t.end(), t.begin(), ::tolower);
    if (w == t)
      count++;
    if (t == "END_OF_TEXT")
      break;
  }
  cout << count << endl;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

string uniteBlanks(string t) {
  int i = 0;
  for (i = t.size() - 1; i > 0; i--) {
    if (t[i - 1] == ' ' && t[i] == ' ')
      t.erase(t.begin() + i);
  }
  if (t[0] == ' ')
    t.erase(t.begin());
  return t;
}

int main() {
  string w, t, t_tmp;
  int i, j, count, ix;
  vector<string> list;
  string item;

  getline(cin, w);
  std::transform(w.begin(), w.end(), w.begin(), ::toupper);
  count = 0;

  getline(cin, t_tmp);
  t = uniteBlanks(t_tmp);

  while (t != "END_OF_TEXT") {
    std::transform(t.begin(), t.end(), t.begin(), ::toupper);

    stringstream ss(t);
    while (getline(ss, item, ' ') && !item.empty()) {
      list.push_back(item);
    }

    for (i = 0; i != list.size(); i++) {
      if (w == list[i]) {
        count++;
      }
    }

    list.clear();
    getline(cin, t_tmp);
    t = uniteBlanks(t_tmp);
  }
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string w, t;
  int i;

  getline(cin, w);
  std::transform(w.begin(), w.end(), w.begin(), ::tolower);
  i = 0;

  getline(cin, t);
  std::transform(t.begin(), t.end(), t.begin(), ::tolower);

  while (t != "END_OF_TEXT") {
    if (t.find(w) != string::npos) {
      i++;
    }
    getline(cin, t);
    std::transform(t.begin(), t.end(), t.begin(), ::tolower);
  }
  cout << i << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main(void) {
  string W, H;
  int c = 0;
  cin >> W;
  while (cin >> H) {
    for (int j = 0; j < H.size(); j++) {
      if (isupper(H[j]))
        H[j] = tolower(H[j]);
    }
    if (H == W)
      c++;
    if (H == "END_OF_TEXT")
      break;
  }
  cout << c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
  string W, H;
  int c = 0;
  cin >> W;
  for (int i = 1; i > 0; i++) {
    cin >> H;
    for (int j = 1; j <= H.size(); j++) {
      H[j] = tolower(H[j]);
    }
    if (H == W)
      c++;
    if (H == "END_OF_TEXT")
      break;
  }
  cout << c << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Runtime Error

//
//  main.cpp
//  ITP1-9-A
//
//  Created by h3037153 on 2017/08/28.
//  Copyright ?? 2017??´ h3037153. All rights reserved.
//
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string W, T[1000];
  int i = 0, counter = 0, ii = 0, len;
  char ch = {};
  cin >> W;
  while (cin >> T[i]) {
    if (T[i] == "END_OF_TEXT") {
      break;
    }
    len = T[i].length();
    for (ii = 0; ii < len; ii++) {
      if (isupper(T[i][ii])) {
        ch = tolower(T[i][ii]);
        T[i][ii] = ch;
      }
    }
    if (T[i] == W) {
      counter++;
    } else
      i++;
  }
  cout << counter << endl;
  return 0;
}

Error: 0

Fixed submission:

//
//  main.cpp
//  ITP1-9-A
//
//  Created by h3037153 on 2017/08/28.
//  Copyright ?? 2017??´ h3037153. All rights reserved.
//
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string W, T[100];
  int i = 0, counter = 0, ii = 0, len;
  char ch = {};
  cin >> W;
  while (cin >> T[i]) {
    if (T[i] == "END_OF_TEXT") {
      break;
    }
    len = T[i].length();
    for (ii = 0; ii < len; ii++) {
      if (isupper(T[i][ii])) {
        ch = tolower(T[i][ii]);
        T[i][ii] = ch;
      }
    }
    if (T[i] == W) {
      counter++;
    } else
      i++;
  }
  cout << counter << endl;
  return 0;
}
Problem p02419 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  string n, n2;
  cin >> n;

  int count = 0;

  while (1) {
    cin >> n2;

    if (n2 == "END_OF_TEXT")
      break;

    for (int i = 0; i < n2.size(); i++) {
      if (65 <= n2[i] && n2[i] <= 90) {
        n2[i] += 32;
      }
    }

    if (n == n2) {
      count++;
    }
  }

  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  string n, n2;
  cin >> n;

  int count = 0;

  while (n2 != "END_OF_TEXT") {
    cin >> n2;

    for (int i = 0; i < n2.size(); i++) {
      if (65 <= n2[i] && n2[i] <= 90) {
        n2[i] += 32;
      }
    }

    if (n == n2) {
      count++;
    }
  }

  cout << count << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int mod = 1000000007;
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  void unite(int x, int y) {
    int root_x = root(x);
    int root_y = root(y);
    if (root_x == root_y)
      return;
    if (d[root_x] > d[root_y])
      swap(root_x, root_y);
    d[root_x] += d[root_y];
    d[root_y] = root_x;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a, b);
  }
  int ans = 0;
  rep(i, n) if (uf.d.at(i) < 0) ans++;
  ans = max(ans - 1, 0);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int mod = 1000000007;
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  void unite(int x, int y) {
    int root_x = root(x);
    int root_y = root(y);
    if (root_x == root_y)
      return;
    if (d[root_x] > d[root_y])
      swap(root_x, root_y);
    d[root_x] += d[root_y];
    d[root_y] = root_x;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    a--;
    b--;
    cin >> a >> b;
    uf.unite(a, b);
  }
  int ans = 0;
  rep(i, n) if (uf.d.at(i) < 0) ans++;
  ans = max(ans - 1, 0);
  cout << ans << endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;

const int maxn = 10e6 + 10;
int p[maxn];
int s[maxn];
int vis[maxn] = {0};
int n, m;
int find(int x) {
  if (p[x] != x)
    p[x] = find(p[x]);
  return p[x];
}

int main() {
  scanf("%d%d", &n, &m);

  for (int i = 1; i <= n; i++) {
    p[i] = i;
    s[i] = 1;
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x = find(x);
    y = find(y);
    if (x == y)
      continue;
    p[y] = x;
    s[x] += s[y];
  }

  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    int pa = find(i);
    int pb = find(i + 1);
    if (pa != pb) {
      p[pa] = pb;
      ans++;
    }
  }
  cout << ans;

  return 0;
}


Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1e6 + 10;
int p[N];
int n, m;
int find(int x) {
  if (p[x] != x)
    p[x] = find(p[x]);
  else
    return x;
}
int main() {
  scanf("%d%d", &n, &m);

  for (int i = 1; i <= n; i++) {
    p[i] = i;
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    p[find(x)] = find(y);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (p[i] != i)
      ans++;
  }
  cout << n - ans - 1;

  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

vector<int> edges[100001];
int n, m;
bool *visited = new bool[100001]();

void dfs(int ind) {

  visited[ind] = true;
  for (int i = 0; i < edges[ind].size(); i++) {
    if (!visited[edges[ind][i]]) {
      dfs(edges[ind][i]);
    }
  }
}

int main() {
  // your code goes here

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    int f, s;
    cin >> f >> s;
    edges[f].push_back(s);
    edges[s].push_back(f);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      cnt++;
      dfs(i);
    }
  }
  cout << cnt - 1;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m, c = 0;
  cin >> n >> m;
  vector<set<int>> vs;
  for (int i = 0; i < m; i++) {
    int x, y, f = 1;
    cin >> x >> y;
    for (int j = 0; j < vs.size(); j++) {
      if (vs[i].end() != vs[i].find(x) || vs[i].end() != vs[i].find(y)) {
        vs[i].insert(x);
        vs[i].insert(y);
        f = 0;
      }
    }
    if (f) {
      set<int> s;
      s.insert(x);
      s.insert(y);
      vs.push_back(s);
    }
  }
  for (int j = 0; j < vs.size(); j++) {
    c += vs[j].size();
  }
  cout << vs.size() + (n - c) - 1;
  return 0;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int root(int x, vector<int> &city) {
  if (city.at(x) == x) {
    return x;
  } else {
    return city.at(x) = root(city.at(x), city);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> city(n);
  vector<int> rank(n);
  vector<int> unionCity(n);
  for (int i = 0; i < n; i++) {
    city.at(i) = i;
    rank.at(i) = 0;
    unionCity.at(i) = 0;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int x = city.at(a - 1);
    int y = city.at(b - 1);
    int rtx = root(x, city);
    int rty = root(y, city);
    if (rtx != rty) {
      if (rank[rtx] < rank[rty]) {
        city.at(rtx) = rty;
      } else {
        city.at(rty) = rtx;
      }
      if (rank.at(rtx) == rank.at(rty)) {
        rank.at(rtx)++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    int rt = root(i, city);
    if (unionCity.at(rt) == 0) {
      unionCity.at(rt)++;
    }
  }
  int sum = 0;

  for (int i = 0; i < n; i++) {
    sum += unionCity.at(i);
  }
  cout << sum - 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int root(int x, vector<int> &city) {
  if (city.at(x) == x) {
    return x;
  } else {
    return city.at(x) = root(city.at(x), city);
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> city(n);
  vector<int> rank(n);
  vector<int> unionCity(n);
  for (int i = 0; i < n; i++) {
    city.at(i) = i;
    rank.at(i) = 0;
    unionCity.at(i) = 0;
  }

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    int x = city.at(a - 1);
    int y = city.at(b - 1);
    if (rank[x] < rank[y]) {
      city.at(x) = y;
    } else {
      city.at(y) = x;
    }
    if (rank.at(x) == rank.at(y)) {
      rank.at(x)++;
    }
  }

  for (int i = 0; i < n; i++) {
    int rt = root(i, city);
    if (unionCity.at(rt) == 0) {
      unionCity.at(rt)++;
    }
  }
  int sum = 0;

  for (int i = 0; i < n; i++) {
    sum += unionCity.at(i);
  }
  cout << sum - 1 << endl;
}