prithivMLmods/Coder-Stat huggingface dataset:
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  vector<bool> isACs(N, false);
  vector<int> WANums(N, 0);
  int penaltyNum = 0;
  for (int i = 0; i < M; ++i) {
    int pi;
    cin >> pi;
    --pi;
    string Si;
    cin >> Si;
    if (Si == "AC") {
      if (isACs[pi])
        continue;
      penaltyNum += WANums[pi];
      isACs[pi] = true;
    } else {
      ++WANums[i];
    }
  }
  int ACNum = count_if(isACs.begin(), isACs.end(),
                       [](const bool isAC) { return isAC; });
  cout << ACNum << ' ' << penaltyNum << '\n';
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;
  vector<bool> isACs(N, false);
  vector<int> WANums(N, 0);
  int penaltyNum = 0;
  for (int i = 0; i < M; ++i) {
    int pi;
    cin >> pi;
    --pi;
    string Si;
    cin >> Si;
    if (Si == "AC") {
      if (isACs[pi])
        continue;
      penaltyNum += WANums[pi];
      isACs[pi] = true;
    } else {
      ++WANums[pi];
    }
  }
  int ACNum = count_if(isACs.begin(), isACs.end(),
                       [](const bool isAC) { return isAC; });
  cout << ACNum << ' ' << penaltyNum << '\n';
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1001001001;
vector<pair<int64_t, int64_t>> func(int64_t x) {
  vector<pair<int64_t, int64_t>> p;
  for (int64_t i = 2; i * i <= x; i++) {
    int cnt = 0;
    if (x % i == 0) {
      while (x % i == 0) {
        cnt++;
        x /= i;
      }
      p.push_back(make_pair(i, cnt));
    }
  }
  if (x != 1) {
    p.push_back(make_pair(x, 1));
  }
  return p;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<bool> ok(N, false);
  vector<int> wa(M);
  int W = 0, A = 0;
  for (int i = 0; i < M; i++) {
    int p;
    string s;
    cin >> p >> s;
    if (ok[p]) {
      continue;
    }
    if (s == "AC") {
      A++;
      W += wa[p];
      ok[p] = true;
    } else {
      wa[p]++;
    }
  }
  cout << A << " " << W << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <cmath>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1001001001;
vector<pair<int64_t, int64_t>> func(int64_t x) {
  vector<pair<int64_t, int64_t>> p;
  for (int64_t i = 2; i * i <= x; i++) {
    int cnt = 0;
    if (x % i == 0) {
      while (x % i == 0) {
        cnt++;
        x /= i;
      }
      p.push_back(make_pair(i, cnt));
    }
  }
  if (x != 1) {
    p.push_back(make_pair(x, 1));
  }
  return p;
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<bool> ok(N, false);
  vector<int> wa(N);
  int W = 0, A = 0;
  for (int i = 0; i < M; i++) {
    int p;
    string s;
    cin >> p >> s;
    if (ok[p]) {
      continue;
    }
    if (s == "AC") {
      A++;
      W += wa[p];
      ok[p] = true;
    } else {
      wa[p]++;
    }
  }
  cout << A << " " << W << endl;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _LIBCPP_DEBUG 0
using namespace std;

int main() {
  int64_t n, m, ans, penalty;
  int64_t p;
  string s;
  ans = 0;
  penalty = 0;
  cin >> n >> m;

  vector<int> ac(n);
  vector<int> wa(n);

  for (int64_t i = 0; i < n; i++) {
    ac.at(i) = 0;
    wa.at(i) = 0;
  }
  //  vector<int> p(m);
  // vector<string> s(m);

  // vector<int> a(n-1);

  for (int i = 0; i < m; i++) {
    // cin>>p.at(i);
    // cin>>s.at(i);
    cin >> p;
    cin >> s;
    if (s == "AC" && ac.at(p - 1) == 0) {
      ac.at(p - 1) = 1;
      ans += 1;
    } else if (s == "WA" && ac.at(p - 1) == 0) {
      penalty += 1;
      wa.at(p - 1) += 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ac.at(i) == 0)
      penalty -= wa.at(p - 1);
  }

  cout << ans << " " << penalty << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define _LIBCPP_DEBUG 0
using namespace std;

int main() {
  int64_t n, m, ans, penalty;
  int64_t p;
  string s;
  ans = 0;
  penalty = 0;
  cin >> n >> m;

  vector<int> ac(n);
  vector<int> wa(n);

  for (int64_t i = 0; i < n; i++) {
    ac.at(i) = 0;
    wa.at(i) = 0;
  }
  //  vector<int> p(m);
  // vector<string> s(m);

  // vector<int> a(n-1);

  for (int i = 0; i < m; i++) {
    // cin>>p.at(i);
    // cin>>s.at(i);
    cin >> p;
    cin >> s;
    if (s == "AC" && ac.at(p - 1) == 0) {
      ac.at(p - 1) = 1;
      ans += 1;
    } else if (s == "WA" && ac.at(p - 1) == 0) {
      penalty += 1;
      wa.at(p - 1) += 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ac.at(i) == 0)
      penalty -= wa.at(i);
  }

  cout << ans << " " << penalty << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> ac(N - 1, 0), wa(N - 1, 0);
  int num;
  string result;
  int ac_cnt = 0, wa_cnt = 0;
  for (int i = 0; i < M; i++) {
    cin >> num >> result;
    if (result == "AC") {
      if (ac[num - 1] == 1) {
        continue;
      } else {
        ac[num - 1] = 1;
        ac_cnt += 1;
        wa_cnt += wa[num - 1];
      }
    } else {
      if (ac[num - 1] == 1) {
        continue;
      } else {
        wa[num - 1] = wa[num - 1] + 1;
      }
    }
  }

  cout << ac_cnt << " " << wa_cnt << endl;
}


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

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> ac(N, 0), wa(N, 0);
  int num;
  string result;
  int ac_cnt = 0, wa_cnt = 0;
  for (int i = 0; i < M; i++) {
    cin >> num >> result;
    if (result == "AC") {
      if (ac[num - 1] == 1) {
        continue;
      } else {
        ac[num - 1] = 1;
        ac_cnt += 1;
        wa_cnt += wa[num - 1];
      }
    } else {
      if (ac[num - 1] == 1) {
        continue;
      } else {
        wa[num - 1] = wa[num - 1] + 1;
      }
    }
  }

  cout << ac_cnt << " " << wa_cnt << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define N 200100
#define pii pair<int, int>
#define rep(i, k, n) for (int i = k; i < n; i++)
#define vi vector<int>
#define inf 1e9
void solve() {
  int n, m;
  cin >> n >> m;
  map<int, int> mp;
  map<int, bool> ms;
  ll ans = 0;
  ll acs = 0;
  while (m--) {
    int x;
    string s;
    cin >> x >> s;
    if (s == "AC" && ms[x] == false) {
      ans += mp[x];
      acs++;
      ms[x] = true;
    } else if (s == "WA") {
      mp[x]++;
    }
  }
  cout << acs << " " << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w+", stdout);
#endif
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define N 200100
#define pii pair<int, int>
#define rep(i, k, n) for (int i = k; i < n; i++)
#define vi vector<int>
#define inf 1e9
void solve() {
  int n, m;
  cin >> n >> m;
  map<int, int> mp;
  map<int, bool> ms;
  ll ans = 0;
  ll acs = 0;
  while (m--) {
    int x;
    string s;
    cin >> x >> s;
    if (s == "AC" && ms[x] == false) {
      ans += mp[x];
      acs++;
      ms[x] = true;
    } else if (s == "WA") {
      mp[x]++;
    }
  }
  cout << acs << " " << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> judge(n);
  int ac = 0, wa = 0;
  rep(i, m) {
    int p;
    cin >> p;
    string s;
    cin >> s;

    if (s == "AC" && judge[p].first == 0) {
      judge[p].first++;
      ac++;
      wa += judge[p].second;
    } else
      judge[p].second++;
  }
  cout << ac << " " << wa << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<P> judge(n);
  int ac = 0, wa = 0;
  rep(i, m) {
    int p;
    cin >> p;
    p--;
    string s;
    cin >> s;

    if (s == "AC" && judge[p].first == 0) {
      judge[p].first++;
      ac++;
      wa += judge[p].second;
    } else
      judge[p].second++;
  }
  cout << ac << " " << wa << endl;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using ll = long long;
#define rep(inc, bgn, end) for (int inc = bgn; inc < end; ++inc)
#define repe(inc, bgn, end) for (int inc = bgn; inc <= end; ++inc)

using namespace std;

int digit(int n) { return (int)(log10(n) + 1); }

// 152-d
int main() {
  int n, m;
  cin >> n >> m;

  int sucsum = 0, missum = 0;

  int p;
  string s;
  vector<int> miscnt(m, 0);
  vector<bool> misflg(m, false);
  rep(i, 0, m) {
    cin >> p >> s;
    p--;
    if (s == "AC" && misflg[p] == false) {
      sucsum++;
      missum += miscnt[p];
      misflg[p] = true;
    } else {
      miscnt[p]++;
    }
  }
  cout << sucsum << " " << missum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using ll = long long;
#define rep(inc, bgn, end) for (int inc = bgn; inc < end; ++inc)
#define repe(inc, bgn, end) for (int inc = bgn; inc <= end; ++inc)

using namespace std;

int digit(int n) { return (int)(log10(n) + 1); }

// 152-d
int main() {
  int n, m;
  cin >> n >> m;

  int sucsum = 0, missum = 0;

  int p;
  string s;
  vector<int> miscnt(n, 0);
  vector<bool> misflg(n, false);
  rep(i, 0, m) {
    cin >> p >> s;
    p--;
    if (s == "AC" && misflg[p] == false) {
      sucsum++;
      missum += miscnt[p];
      misflg[p] = true;
    } else {
      miscnt[p]++;
    }
  }
  cout << sucsum << " " << missum << endl;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  long long ac = 0;
  long long wa = 0;

  vector<long long> wrong(n, 0);
  vector<bool> isac(n, 0);

  for (int i = 0; i < m; ++i) {
    int p;
    string s;
    cin >> p >> s;
    if (!isac[p]) {
      if (s == "AC") {
        isac[p] = true;
        wa += wrong[p];
        ac++;
      } else {
        wrong[p]++;
      };
    };
  }
  cout << ac << " " << wa << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  long long ac = 0;
  long long wa = 0;

  vector<long long> wrong(n, 0);
  vector<bool> isac(n, 0);

  for (int i = 0; i < m; ++i) {
    int p;
    string s;
    cin >> p >> s;
    --p;
    if (!isac[p]) {
      if (s == "AC") {
        isac[p] = true;
        wa += wrong[p];
        ac++;
      } else {
        wrong[p]++;
      };
    };
  }
  cout << ac << " " << wa << endl;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  static int wa[10000];
  static bool ac[10000];

  int n, m;
  cin >> n >> m;

  int p, a = 0, w = 0;
  string s;
  for (int i = 0; i < m; i++) {
    cin >> p >> s;
    if (ac[p])
      continue;
    if (s == "WA")
      wa[p]++;
    else {
      ac[p] = true;
      a++;
      w += wa[p];
    }
  }

  cout << a << " " << w << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  static int wa[100001];
  static bool ac[100001];

  int n, m;
  cin >> n >> m;

  int p, a = 0, w = 0;
  string s;
  for (int i = 0; i < m; i++) {
    cin >> p >> s;
    if (ac[p])
      continue;
    if (s == "WA")
      wa[p]++;
    else {
      ac[p] = true;
      a++;
      w += wa[p];
    }
  }

  cout << a << " " << w << endl;
}
Problem p02802 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, m;
  cin >> n >> m;
  ll count_ac = 0;
  ll count_wa = 0;
  vi p(m);
  vector<string> M(m);
  rep(i, m) { cin >> p[i] >> M[i]; }
  for (ll i = 1; i <= n; i++) {
    ll count_wai = 0;
    rep(j, m) {
      if (p[j] == i && M[j] == "WA") {
        count_wai++;
      } else if (p[j] == i && M[j] == "AC") {
        count_ac++;
        count_wa += count_wai;
        break;
      }
    }
  }
  cout << count_ac << " " << count_wa << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, m;
  cin >> n >> m;
  ll count_ac = 0;
  ll count_wa = 0;
  vi p(m);
  vector<string> M(m);
  vector<bool> a(n, true);
  vi b(n, 0);
  rep(i, m) {
    cin >> p[i] >> M[i];
    p[i]--;
  }
  rep(i, m) {
    if (a[p[i]] == true && M[i] == "WA") {
      b[p[i]]++;
    } else if (a[p[i]] == true && M[i] == "AC") {
      a[p[i]] = false;
      count_ac++;
      count_wa += b[p[i]];
    }
  }
  cout << count_ac << " " << count_wa << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> p(n + 10);
  vector<ll> wa(n + 10);
  ll pen = 0;
  ll ans = 0;
  rep(i, m) {
    ll prob;
    string res;
    cin >> prob >> res;
    if (p[prob] == 1) {
    } else {
      if (res == "WA") {
        wa[prob] += 1;
      } else {
        ans += 1;
        p[prob] = 1;
      }
    }
  }
  rep(i, m + 1) {
    if (p[i] == 1) {
      pen += wa[i];
    }
  }
  cout << ans << " " << pen << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> p(n + 10);
  vector<ll> wa(n + 10);
  ll pen = 0;
  ll ans = 0;
  rep(i, m) {
    ll prob;
    string res;
    cin >> prob >> res;
    if (p[prob] == 1) {
    } else {
      if (res == "WA") {
        wa[prob] += 1;
      } else {
        ans += 1;
        p[prob] = 1;
      }
    }
  }
  rep(i, n + 1) {
    if (p[i] == 1) {
      pen += wa[i];
    }
  }
  cout << ans << " " << pen << endl;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int INF = 1e9 + 5;
const int N = 2e5 + 314;
void foo() {
  int n, m;
  cin >> n >> m;
  vi v(n + 1);
  vi cnt(n + 1);
  int ok = 0, wa = 0;
  for (int i = 0; i < m; i++) {
    int x;
    string c;
    cin >> x >> c;
    if (c == "AC")
      v[x] = 1;
    else {
      if (!v[i])
        cnt[x]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i]) {
      ok++;
      wa += cnt[i];
    }
  }
  cout << ok << " " << wa;
}
int main() {
  speed;
  int t = 1;
  // cin>>t;
  while (t--)
    foo();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int INF = 1e9 + 5;
const int N = 2e5 + 314;
void foo() {
  int n, m;
  cin >> n >> m;
  vi v(n + 1);
  vi cnt(n + 1);
  int ok = 0, wa = 0;
  for (int i = 0; i < m; i++) {
    int x;
    string c;
    cin >> x >> c;
    if (c == "AC")
      v[x] = 1;
    else {
      if (!v[x])
        cnt[x]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (v[i]) {
      ok++;
      wa += cnt[i];
    }
  }
  cout << ok << " " << wa;
}
int main() {
  speed;
  int t = 1;
  // cin>>t;
  while (t--)
    foo();
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, AC = 0, WA = 0;
  cin >> N >> M;
  vector<int> p(M), wa(M, 0);
  vector<string> S(M);
  vector<bool> flag(105000, false);
  for (int i = 0; i < M; ++i) {
    cin >> p.at(i) >> S.at(i);
    if (!flag.at(p.at(i))) {
      if (S.at(i) == "WA") {
        wa.at(p.at(i))++;
      } else if (S.at(i) == "AC") {
        ++AC;
        WA += wa.at(p.at(i));
        flag.at(p.at(i)) = true;
      }
    }
  }

  cout << AC << " " << WA << endl;
}


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

int main() {
  int N, M, AC = 0, WA = 0;
  cin >> N >> M;
  vector<int> p(M), wa(105000, 0);
  vector<string> S(M);
  vector<bool> flag(105000, false);
  for (int i = 0; i < M; ++i) {
    cin >> p.at(i) >> S.at(i);
    if (!flag.at(p.at(i))) {
      if (S.at(i) == "WA") {
        wa.at(p.at(i))++;
      } else if (S.at(i) == "AC") {
        ++AC;
        WA += wa.at(p.at(i));
        flag.at(p.at(i)) = true;
      }
    }
  }

  cout << AC << " " << WA << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m, wans = 0, ans = 0;
  cin >> n >> m;
  vector<int> p(m), ac(n, 0), wa(n, 0);
  vector<string> s(m);
  rep(i, m) {
    cin >> p.at(i) >> s.at(i);
    if (ac.at(p.at(i) - 1) == 0) {
      if (s.at(i) == "WA") {
        wans++;
        wa.at(p.at(i))++;
      }
      if (s.at(i) == "AC") {
        ans++;
        ac.at(p.at(i) - 1)++;
      }
    }
  }
  rep(i, n) {
    if (ac.at(i) == 0) {
      wans -= wa.at(i);
    }
  }
  cout << ans << ' ' << wans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m, wans = 0, ans = 0;
  cin >> n >> m;
  vector<int> p(m), ac(n, 0), wa(n, 0);
  vector<string> s(m);
  rep(i, m) {
    cin >> p.at(i) >> s.at(i);
    if (ac.at(p.at(i) - 1) == 0) {
      if (s.at(i) == "WA") {
        wans++;
        wa.at(p.at(i) - 1)++;
      }
      if (s.at(i) == "AC") {
        ans++;
        ac.at(p.at(i) - 1)++;
      }
    }
  }
  rep(i, n) {
    if (ac.at(i) == 0) {
      wans -= wa.at(i);
    }
  }
  cout << ans << ' ' << wans << endl;

  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m, p, countWA = 0, countAC = 0;
  string s;
  cin >> n >> m;
  std::deque<int> deq(n + 1, 0);
  std::vector<vector<int>> vec(n, vector<int>(1, 0));
  for (int i = 0; i < m; i++) {
    cin >> p >> s;
    if (deq[p] == 0 && s == "AC") {
      countAC++;
      countWA += vec[p][0];
      deq[p] = 1;
    } else if (deq[p] == 0 && s == "WA") {
      vec[p][0]++;
    }
  }
  cout << countAC << " " << countWA << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m, p, countWA = 0, countAC = 0;
  string s;
  cin >> n >> m;
  std::deque<int> deq(n + 1, 0);
  std::vector<vector<int>> vec(n + 1, vector<int>(1, 0));
  for (int i = 0; i < m; i++) {
    cin >> p >> s;
    if (deq[p] == 0 && s == "AC") {
      countAC++;
      countWA += vec[p][0];
      deq[p] = 1;
    } else if (deq[p] == 0 && s == "WA") {
      vec[p][0]++;
    }
  }
  cout << countAC << " " << countWA << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  int ac = 0, wa = 0;
  vector<bool> AC(n);
  vector<int> WA(0);
  for (int i = 0; i < m; i++) {
    int p;
    string s;
    cin >> p >> s;
    p--;

    if (AC[p])
      continue;

    if (s == "AC") {
      ac++;
      AC[p] = true;
      wa += WA[p];
    } else {
      WA[p]++;
    }
  }
  cout << ac << " " << wa << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  int ac = 0, wa = 0;
  vector<bool> AC(n);
  vector<int> WA(n, 0);
  for (int i = 0; i < m; i++) {
    int p;
    string s;
    cin >> p >> s;
    p--;

    if (AC[p])
      continue;

    if (s == "AC") {
      ac++;
      AC[p] = true;
      wa += WA[p];
    } else {
      WA[p]++;
    }
  }
  cout << ac << " " << wa << endl;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<string> b(m);
  int i;
  for (i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
  }
  int count_1 = 0, count_2 = 0;
  vector<pair<bool, int>> is(n);
  rep(i, m) {
    if (b.at(i) == "AC") {
      is.at(a.at(i) - 1).first = true;
    }
    if (b.at(i) == "WA" && is.at(a.at(i) - 1).first == false) {
      is.at(a.at(i) - 1).second++;
    }
  }
  rep(i, n) {
    if (is.at(i).first) {
      count_1++;
      count_2 += is.at(a.at(i) - 1).second;
    }
  }
  cout << count_1 << " " << count_2;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<string> b(m);
  int i;
  for (i = 0; i < m; i++) {
    cin >> a.at(i) >> b.at(i);
  }
  int count_1 = 0, count_2 = 0;
  vector<pair<bool, int>> is(n);
  rep(i, m) {
    if (b.at(i) == "AC") {
      is.at(a.at(i) - 1).first = true;
    }
    if (b.at(i) == "WA" && is.at(a.at(i) - 1).first == false) {
      is.at(a.at(i) - 1).second++;
    }
  }
  rep(i, n) {
    if (is.at(i).first) {
      count_1++;
      count_2 += is.at(i).second;
    }
  }
  cout << count_1 << " " << count_2;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9;
const int MOD = 1000000007;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> p(m);
  vector<string> s(m);
  rep(i, m) cin >> p[i] >> s[i];
  vector<ll> num(m);

  ll sum1 = 0;
  ll sum2 = 0;
  rep(i, m) {
    if (s[i] == "AC") {
      if (num[p[i] - 1] >= 0) {
        sum2 += num[p[i] - 1];
        num[p[i] - 1] = -1;
        sum1++;
      }
    } else {
      if (num[p[i] - 1] >= 0)
        num[p[i] - 1]++;
    }
  }
  cout << sum1 << " " << sum2 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9;
const int MOD = 1000000007;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> p(m);
  vector<string> s(m);
  rep(i, m) cin >> p[i] >> s[i];
  vector<ll> num(n);

  ll sum1 = 0;
  ll sum2 = 0;
  rep(i, m) {
    if (s[i] == "AC") {
      if (num[p[i] - 1] >= 0) {
        sum2 += num[p[i] - 1];
        num[p[i] - 1] = -1;
        sum1++;
      }
    } else {
      if (num[p[i] - 1] >= 0)
        num[p[i] - 1]++;
    }
  }
  cout << sum1 << " " << sum2 << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, N) for (long long i = x; i < (N); i++)
using namespace std;
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> a(M);
  vector<string> b(M);
  vector<pair<long long, string>> p(M);
  rep(i, 0, M) {
    cin >> a[i] >> b[i];
    p[i] = make_pair(a[i], b[i]);
  }
  vector<long long> flag(N), flag2(N);
  rep(i, 0, N) {
    flag[i] = 0;
    flag2[i] = 0;
  }
  rep(i, 0, M) {
    if (p[i].second == "AC") {
      flag2[p[i].first] = 1;
    }
  }
  long long count_ac = 0, count_wa = 0;
  rep(i, 0, M) {
    if (p[i].second == "AC" && flag[p[i].first] == 0) {
      flag[p[i].first] = 1;
      count_ac++;
    } else if (p[i].second == "WA" && flag[p[i].first] == 0 &&
               flag2[p[i].first] == 1) {
      count_wa++;
    }
  }
  cout << count_ac << ' ' << count_wa << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, N) for (long long i = x; i < (N); i++)
using namespace std;
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> a(M);
  vector<string> b(M);
  vector<pair<long long, string>> p(M);
  rep(i, 0, M) {
    cin >> a[i] >> b[i];
    p[i] = make_pair(a[i], b[i]);
  }
  long long H;
  H = max(N, M);
  vector<long long> flag(H), flag2(H);
  if (M > N) {

    rep(i, 0, M) {
      flag[i] = 0;
      flag2[i] = 0;
    }
  } else {
    rep(i, 0, N) {
      flag[i] = 0;
      flag2[i] = 0;
    }
  }
  rep(i, 0, M) {
    if (p[i].second == "AC") {
      flag2[p[i].first] = 1;
    }
  }
  long long count_ac = 0, count_wa = 0;
  rep(i, 0, M) {
    if (p[i].second == "AC" && flag[p[i].first] == 0) {
      flag[p[i].first] = 1;
      count_ac++;
    } else if (p[i].second == "WA" && flag[p[i].first] == 0 &&
               flag2[p[i].first] == 1) {
      count_wa++;
    }
  }
  cout << count_ac << ' ' << count_wa << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int p[10007] = {};
bool ac[10007] = {};

int main() {
  int n, m, a;
  cin >> n >> m;
  string s;
  int pen = 0, corr = 0;
  for (int i = 0; i < m; i++) {
    cin >> a >> s;
    if (s == "AC") {
      if (!ac[a]) {
        pen += p[a];
        ac[a] = 1;
        corr++;
      }
    } else {
      p[a]++;
    }
  }
  cout << corr << ' ' << pen << '\n';
}


Fixed submission:
#include <iostream>
using namespace std;

int p[100007] = {};
bool ac[100007] = {};

int main() {
  int n, m, a;
  cin >> n >> m;
  string s;
  int pen = 0, corr = 0;
  for (int i = 0; i < m; i++) {
    cin >> a >> s;
    if (s == "AC") {
      if (!ac[a]) {
        pen += p[a];
        ac[a] = 1;
        corr++;
      }
    } else {
      p[a]++;
    }
  }
  cout << corr << ' ' << pen << '\n';
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fo(i, s, e) for (int i = (s); i < (e); i++)
#define all(obj) (obj).begin(), (obj).end()
/*卍卍卍 #define int long long 卍卍卍*/
signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> acwa_pena(n, vector<int>(2, 0));
  rep(i, m) {
    int no;
    string acwa;
    cin >> no >> acwa;
    if (acwa == "AC")
      acwa_pena[no][0] = 1;
    else if (acwa_pena[no][0] == 0)
      acwa_pena[no][1] += 1;
  }
  int ac = 0;
  int pena = 0;
  rep(i, n) {
    ac += acwa_pena[i][0];
    if (acwa_pena[i][0])
      pena += acwa_pena[i][1];
  }
  cout << ac << " " << pena << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fo(i, s, e) for (int i = (s); i < (e); i++)
#define all(obj) (obj).begin(), (obj).end()
/*卍卍卍 #define int long long 卍卍卍*/
signed main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> acwa_pena(n, vector<int>(2, 0));
  rep(i, m) {
    int no;
    string acwa;
    cin >> no >> acwa;
    if (acwa == "AC")
      acwa_pena[no - 1][0] = 1;
    else if (acwa_pena[no - 1][0] == 0)
      acwa_pena[no - 1][1] += 1;
  }
  int ac = 0;
  int pena = 0;
  rep(i, n) {
    ac += acwa_pena[i][0];
    if (acwa_pena[i][0])
      pena += acwa_pena[i][1];
  }
  cout << ac << " " << pena << endl;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  int na = 0;
  vector<bool> V(N, false);
  vector<int> np(N, 0);

  for (int i = 0; i < M; ++i) {
    int p;
    string s;
    cin >> p >> s;
    --p;

    if (V[p])
      continue;
    if (s == "AC") {
      V[p] = true;
      na++;
    } else {
      np[p]++;
    }
  }

  int sum = 0;
  for (int i = 0; i < M; ++i) {
    sum += np[i] * V[i];
  }

  cout << na << ' ' << sum << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  int na = 0;
  vector<bool> V(N, false);
  vector<int> np(N, 0);

  for (int i = 0; i < M; ++i) {
    int p;
    string s;
    cin >> p >> s;
    --p;

    if (V[p])
      continue;
    if (s == "AC") {
      V[p] = true;
      na++;
    } else {
      np[p]++;
    }
  }

  int sum = 0;
  for (int i = 0; i < N; ++i) {
    sum += np[i] * V[i];
  }

  cout << na << ' ' << sum << endl;

  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int penalties[10001] = {}, rez_w = 0, rez_a = 0, n, m, p;
  char ch[3];
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> p >> ch;
    if (strcmp(ch, "WA") == 0 && penalties[p] != -1)
      penalties[p]++;
    else {
      if (penalties[p] != -1) {
        rez_w += penalties[p];
        rez_a++;
        penalties[p] = -1;
      }
    }
  }
  cout << rez_a << ' ' << rez_w;
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int penalties[100001] = {}, rez_w = 0, rez_a = 0, n, m, p;
  char ch[3];
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> p >> ch;
    if (strcmp(ch, "WA") == 0 && penalties[p] != -1)
      penalties[p]++;
    else {
      if (penalties[p] != -1) {
        rez_w += penalties[p];
        rez_a++;
        penalties[p] = -1;
      }
    }
  }
  cout << rez_a << ' ' << rez_w;
  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<bool> y(n, true);
  vector<int> x(m, 0);
  int ac = 0, pen = 0;
  for (int i = 0; i < m; ++i) {
    int p;
    string s;
    cin >> p >> s;
    --p;
    if (y[p]) {
      if (s == "WA") {
        ++x[p];
      } else {
        ++ac;
        y[p] = false;
        pen += x[p];
      }
    }
  }

  cout << ac << " " << pen << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<bool> y(n, true);
  vector<int> x(n, 0);
  int ac = 0, pen = 0;
  for (int i = 0; i < m; ++i) {
    int p;
    string s;
    cin >> p >> s;
    --p;
    if (y[p]) {
      if (s == "WA") {
        ++x[p];
      } else {
        ++ac;
        y[p] = false;
        pen += x[p];
      }
    }
  }

  cout << ac << " " << pen << '\n';

  return 0;
}
Problem p02802 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

int main() {
  long long N, M;
  std::cin >> N >> M;
  std::vector<bool> flag(N, false);
  std::vector<long long> wa(N, 0);
  long long ac = 0;
  for (long long i = 0; i < M; i++) {
    long long p;
    std::string s;
    std::cin >> p >> s;
    if (s == "AC" && !flag[p]) {
      flag[p] = true;
      ac++;
    } else if (s == "WA" && !flag[p]) {
      wa[p]++;
    }
  }
  long long w = 0;
  for (long long i = 0; i < N; i++) {
    w += flag[i] ? wa[i] : 0;
  }
  std::cout << ac << " " << w << std::endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>

int main() {
  long long N, M;
  std::cin >> N >> M;
  std::vector<bool> flag(N, false);
  std::vector<long long> wa(N, 0);
  long long ac = 0;
  for (long long i = 0; i < M; i++) {
    long long p;
    std::string s;
    std::cin >> p >> s;
    p--;
    if (s == "AC" && !flag[p]) {
      flag[p] = true;
      ac++;
    } else if (s == "WA" && !flag[p]) {
      wa[p]++;
    }
  }
  long long w = 0;
  for (long long i = 0; i < N; i++) {
    w += flag[i] ? wa[i] : 0;
  }
  std::cout << ac << " " << w << std::endl;
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int N, M, ans;

int main() {
  cin >> N >> M;
  vector<vector<int>> maze(N, vector<int>(M, 1));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      char a;
      cin >> a;
      if (a == '.')
        maze[i][j] = 0;
    }

  vector<vector<int>> G(N * M, vector<int>(0));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (i > 0 && maze[i][j] == 0 && maze[i - 1][j] == 0) {
        G[i * M + j].push_back((i - 1) * M + j);
        G[(i - 1) * M + j].push_back(i * M + j);
      }
      if (j > 0 && maze[i][j] == 0 && maze[i][j - 1] == 0) {
        G[i * N + j].push_back(i * N + j - 1);
        G[i * N + (j - 1)].push_back(i * N + j);
      }
    }

  ans = 0;
  for (int i = 0; i < N * M; i++) {
    if (G[i].size() == 0)
      continue;
    vector<int> dist(N * M, -1);
    queue<int> que;
    dist[i] = 0;
    que.push(i);

    while (!que.empty()) {
      int v = que.front();
      que.pop();

      for (int nv : G[v]) {
        if (dist[nv] != -1)
          continue;
        dist[nv] = dist[v] + 1;
        que.push(nv);
      }
    }
    sort(dist.begin(), dist.end());
    if (dist[N * M - 1] > ans)
      ans = dist[N * M - 1];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int N, M, ans;

int main() {
  cin >> N >> M;
  vector<vector<int>> maze(N, vector<int>(M, 1));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      char a;
      cin >> a;
      if (a == '.')
        maze[i][j] = 0;
    }

  vector<vector<int>> G(N * M, vector<int>(0));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (i > 0 && maze[i][j] == 0 && maze[i - 1][j] == 0) {
        G[i * M + j].push_back((i - 1) * M + j);
        G[(i - 1) * M + j].push_back(i * M + j);
      }
      if (j > 0 && maze[i][j] == 0 && maze[i][j - 1] == 0) {
        G[i * M + j].push_back(i * M + j - 1);
        G[i * M + (j - 1)].push_back(i * M + j);
      }
    }

  ans = 0;
  for (int i = 0; i < N * M; i++) {
    if (G[i].size() == 0)
      continue;
    vector<int> dist(N * M, -1);
    queue<int> que;
    dist[i] = 0;
    que.push(i);

    while (!que.empty()) {
      int v = que.front();
      que.pop();

      for (int nv : G[v]) {
        if (dist[nv] != -1)
          continue;
        dist[nv] = dist[v] + 1;
        que.push(nv);
      }
    }
    sort(dist.begin(), dist.end());
    if (dist[N * M - 1] > ans)
      ans = dist[N * M - 1];
  }
  cout << ans << endl;
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
// Author: πα

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

const int N = 22;
char g[N][N];
long dp[N][N];
bool vis[N][N];

void dfs(int x, int y, long k = 0) {
  if (vis[x][y] or g[x][y] != '.')
    return;
  vis[x][y] = 1;
  dp[x][y] = min(dp[x][y], k);
  dfs(x + 1, y, k + 1);
  dfs(x - 1, y, k + 1);
  dfs(x, y + 1, k + 1);
  dfs(x, y - 1, k + 1);
  vis[x][y] = 0;
}

int main() {
  int n, m;
  long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> g[i] + 1;
  for (int x = 1; x <= n; ++x)
    for (int y = 1; y <= m; ++y)
      if (g[x][y] == '.') {
        memset(dp, -1, sizeof(dp));
        dfs(x, y);
        for (int i = 1; i <= n; ++i)
          for (int j = 1; j <= m; ++j)
            if (g[i][j] == '.')
              ans = max(ans, dp[i][j]);
      }
  cout << ans;
  return 0;
}


Fixed submission:
// Author: πα

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

const int N = 22;
char g[N][N];
long dp[N][N];
bool vis[N][N];

void dfs(int x, int y, long k = 0) {
  if (vis[x][y] or g[x][y] != '.' or dp[x][y] == k)
    return;
  vis[x][y] = 1;
  dp[x][y] = min(dp[x][y], k);
  dfs(x + 1, y, k + 1);
  dfs(x - 1, y, k + 1);
  dfs(x, y + 1, k + 1);
  dfs(x, y - 1, k + 1);
  vis[x][y] = 0;
}

int main() {
  int n, m;
  long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> g[i] + 1;
  for (int x = 1; x <= n; ++x)
    for (int y = 1; y <= m; ++y)
      if (g[x][y] == '.') {
        memset(dp, -1, sizeof(dp));
        dfs(x, y);
        for (int i = 1; i <= n; ++i)
          for (int j = 1; j <= m; ++j)
            if (g[i][j] == '.')
              ans = max(ans, dp[i][j]);
      }
  cout << ans;
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<string> map(H);
  for (auto &s : map)
    cin >> s;

  auto bfs = [&](int s) {
    if (map[s / W][s % W] == '#')
      return -1;
    vector<int> d(H * W, numeric_limits<int>::max());
    queue<pair<int, int>> qs;
    qs.emplace(s, 0);
    while (not qs.empty()) {
      auto q = qs.front();
      qs.pop();
      auto s = q.first;
      auto dist = q.second;
      if (d[s] <= dist)
        continue;
      auto go = [&](int t) {
        if (map[t / W][t % W] == '.' and d[t] > dist + 1) {
          qs.emplace(t, dist + 1);
        }
      };
      // -1
      if (s % W > 0) {
        go(s - 1);
      }
      // 1
      if (s % W != W - 1) {
        go(s + 1);
      }
      // -W
      if (s / W > 0) {
        go(s - W);
      }
      // W
      if (s / W != H - 1) {
        go(s + W);
      }
    }
    auto res = 0;
    for (auto v : d) {
      if (v == numeric_limits<int>::max())
        continue;
      res = max(res, v);
    }
    return res;
  };
  auto ans = 0;
  for (int i = 0; i < H * W; i++) {
    ans = max(ans, bfs(i));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vector<string> map(H);
  for (auto &s : map)
    cin >> s;

  auto bfs = [&](int s) {
    if (map[s / W][s % W] == '#')
      return -1;
    vector<int> d(H * W, numeric_limits<int>::max());
    queue<pair<int, int>> qs;
    qs.emplace(s, 0);
    while (not qs.empty()) {
      auto q = qs.front();
      qs.pop();
      auto s = q.first;
      auto dist = q.second;
      if (d[s] <= dist)
        continue;
      d[s] = dist;
      auto go = [&](int t) {
        if (map[t / W][t % W] == '.' and d[t] > dist + 1) {
          qs.emplace(t, dist + 1);
        }
      };
      // -1
      if (s % W > 0) {
        go(s - 1);
      }
      // 1
      if (s % W != W - 1) {
        go(s + 1);
      }
      // -W
      if (s / W > 0) {
        go(s - W);
      }
      // W
      if (s / W != H - 1) {
        go(s + W);
      }
    }
    auto res = 0;
    for (auto v : d) {
      if (v == numeric_limits<int>::max())
        continue;
      res = max(res, v);
    }
    return res;
  };
  auto ans = 0;
  for (int i = 0; i < H * W; i++) {
    ans = max(ans, bfs(i));
  }
  cout << ans << endl;

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int stepX[4] = {1, -1, 0, 0};
int stepY[4] = {0, 0, 1, -1};

bool inArea(int tx, int ty, int w, int h) {
  return (0 <= tx && tx < w && 0 <= ty && ty < h);
}

int getFar(vector<vector<bool>> &mat, int sh, int sw, int h, int w) {
  queue<int> que;
  vector<int> steps(h * w, h * w);
  vector<bool> checker(w * h);
  que.push(sh * w + sw);
  steps[sh * w + sw] = 0;
  checker[sh * w + sw] = true;
  int dist = 0;
  while (!que.empty()) {
    int num = que.front();
    que.pop();
    int tx = num % w;
    int ty = num / w;
    int stp = steps[ty * w + tx];
    for (int i = 0; i < 4; ++i) {
      int ttx = tx + stepX[i];
      int tty = ty + stepY[i];
      if (inArea(ttx, tty, w, h) && mat[tty][ttx] && !checker[tty * w + ttx]) {
        que.push(tty * w + ttx);
        checker[tty * w + ttx] = true;
        steps[tty * w + ttx] = stp + 1;
        dist = max(dist, stp + 1);
      }
    }
  }
  return dist;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<bool>> mat(h);
  for (int i = 0; i < h; ++i) {
    vector<bool> row(w);
    for (int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      row[j] = c == '.';
    }
    mat[i] = row;
  }
  int ans = 0;
  for (int i = 0; i < h * w; ++i) {
    int tx = i % w;
    int ty = i / w;
    if (!mat[ty][tx]) {
      continue;
    }
    ans = max(ans, getFar(mat, tx, ty, h, w));
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int stepX[4] = {1, -1, 0, 0};
int stepY[4] = {0, 0, 1, -1};

bool inArea(int tx, int ty, int w, int h) {
  return (0 <= tx && tx < w && 0 <= ty && ty < h);
}

int getFar(vector<vector<bool>> &mat, int sh, int sw, int h, int w) {
  queue<int> que;
  vector<int> steps(h * w, h * w);
  vector<bool> checker(w * h);
  que.push(sh * w + sw);
  steps[sh * w + sw] = 0;
  checker[sh * w + sw] = true;
  int dist = 0;
  while (!que.empty()) {
    int num = que.front();
    que.pop();
    int tx = num % w;
    int ty = num / w;
    int stp = steps[ty * w + tx];
    for (int i = 0; i < 4; ++i) {
      int ttx = tx + stepX[i];
      int tty = ty + stepY[i];
      if (inArea(ttx, tty, w, h) && mat[tty][ttx] && !checker[tty * w + ttx]) {
        que.push(tty * w + ttx);
        checker[tty * w + ttx] = true;
        steps[tty * w + ttx] = stp + 1;
        dist = max(dist, stp + 1);
      }
    }
  }
  return dist;
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<bool>> mat(h);
  for (int i = 0; i < h; ++i) {
    vector<bool> row(w);
    for (int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      row[j] = c == '.';
    }
    mat[i] = row;
  }
  int ans = 0;
  for (int i = 0; i < h * w; ++i) {
    int tx = i % w;
    int ty = i / w;
    if (!mat[ty][tx]) {
      continue;
    }
    ans = max(ans, getFar(mat, ty, tx, h, w));
  }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int h, w;
vector<vector<int>> d;

void make_way(int i, int j, int s, int t) {
  d[i * h + j][s * h + t] = 1;
  d[s * h + t][i * h + j] = 1;
}

int main() {
  cin >> h >> w;
  const int INF = 1 << 20;
  REP(i, h * w) {
    vector<int> tmp(h * w, INF);
    d.push_back(tmp);
  }
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  REP(i, h * w) d[i][i] = 0;

  REP(i, h) {
    REP(j, w) {
      if (s[i][j] == '#')
        continue;
      if (i != 0 && s[i - 1][j] == '.')
        make_way(i, j, i - 1, j);
      if (j != 0 && s[i][j - 1] == '.')
        make_way(i, j, i, j - 1);
      if (i != h - 1 && s[i + 1][j] == '.')
        make_way(i, j, i + 1, j);
      if (j != w - 1 && s[i][j + 1] == '.')
        make_way(i, j, i, j + 1);
    }
  }

  REP(k, h * w) {
    REP(i, h * w) {
      REP(j, h * w) { chmin(d[i][j], d[i][k] + d[k][j]); }
    }
  }

  int ans = 0;
  REP(i, h * w) {
    REP(j, h * w) {
      if (d[i][j] != INF)
        chmax(ans, d[i][j]);
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int h, w;
vector<vector<int>> d;

void make_way(int i, int j, int s, int t) {
  d[i * w + j][s * w + t] = 1;
  d[s * w + t][i * w + j] = 1;
}

int main() {
  cin >> h >> w;
  const int INF = 1 << 20;
  REP(i, h * w) {
    vector<int> tmp(h * w, INF);
    d.push_back(tmp);
  }
  vector<string> s(h);
  REP(i, h) { cin >> s[i]; }
  REP(i, h * w) d[i][i] = 0;

  REP(i, h) {
    REP(j, w) {
      if (s[i][j] == '#')
        continue;
      if (i != 0 && s[i - 1][j] == '.')
        make_way(i, j, i - 1, j);
      if (j != 0 && s[i][j - 1] == '.')
        make_way(i, j, i, j - 1);
      if (i != h - 1 && s[i + 1][j] == '.')
        make_way(i, j, i + 1, j);
      if (j != w - 1 && s[i][j + 1] == '.')
        make_way(i, j, i, j + 1);
    }
  }

  REP(k, h * w) {
    REP(i, h * w) {
      REP(j, h * w) { chmin(d[i][j], d[i][k] + d[k][j]); }
    }
  }

  int ans = 0;
  REP(i, h * w) {
    REP(j, h * w) {
      if (d[i][j] != INF)
        chmax(ans, d[i][j]);
    }
  }

  cout << ans << endl;

  return 0;
}