alexjercan/bugnet huggingface dataset:
Problem p01224 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {

  int N, S, f, cnt;

  while (1) {
    S = 0;
    cnt = 0;
    cin >> N;
    if (N == 0)
      break;
    if (N % 2 == 0)
      f = 1;
    else
      f = 2;
    for (int i = 1; i * i < N; i += f) {
      if (N % i == 0) {
        S += i + (N / i);
      }
    }
    S -= N;
    if (sqrt((double)N) == (double)(int)sqrt((double)N))
      S -= (int)sqrt((double)N);
    if (N == S)
      cout << "perfect number" << endl;
    if (N > S)
      cout << "deficient number" << endl;
    if (N < S)
      cout << "abundant number" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {

  int N, S, f, e;

  while (1) {
    S = 0;
    e = 1;
    cin >> N;
    if (N == 0)
      break;
    if (N % 2 == 0)
      f = 1;
    else
      f = 2;
    for (int i = 2; N != 1; i++) {
      if (N % i == 0) {
        e = i;
        break;
      }
    }
    for (int i = 1; i <= (double)(N / e); i += f) {
      if (N % i == 0)
        S += i;
    }
    if (N == S)
      cout << "perfect number" << endl;
    if (N > S)
      cout << "deficient number" << endl;
    if (N < S)
      cout << "abundant number" << endl;
  }

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

#include <cmath>
#include <iostream>
using namespace std;
int main() {

  int n, a, b;

  while (1) {

    a = 0;

    cin >> n;

    if (n == 0)
      break;

    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        if (i == n / i) {
          a += i;
        } else {
          a += i + n / i;
        }
      }
    }

    b = a - n;

    if (n == 1) {
      b = 0;
    }

    if (n == b) {
      cout << "perfect number" << endl;
    } else if (n > b) {
      cout << "deficient number" << endl;
    } else if (n < b) {
      cout << "abundant number" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {

  int n, a, b;

  while (1) {

    a = 0;

    cin >> n;

    if (n == 0)
      break;

    for (int i = 1; i <= sqrt(n); i++) {
      for (int j = sqrt(n); j <= n; j++) {
        if (i * j == n) {
          a += i + j;
        }
      }
    }

    b = a - n;

    if (n == 1) {
      b = 0;
    }

    cout << b << endl;

    if (n == b) {
      cout << "perfect number" << endl;
    } else if (n > a) {
      cout << "deficient number" << endl;
    } else if (n < a) {
      cout << "abundant number" << endl;
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int sum, hoge = n;
    n < 2 ? sum = 0 : sum = 1;
    for (int i = 2; i < hoge && sum < n; i++) {
      if (n % i == 0) { // 約数
        hoge = n / i;   // n = i * hoge
        if (i != hoge) {
          sum += i + hoge;
        } else {
          sum += i;
        }
      }
    }
    if (sum == n)
      cout << "perfect number" << endl;
    else if (sum < n)
      cout << "deficient number" << endl;
    else
      cout << "abundant number" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int sum;
    n < 2 ? sum = 0 : sum = 1;
    for (int i = 2; i <= n / 2; i++) {
      if (n % i == 0) {
        sum += n / i;
      }
    }
    if (sum == n)
      cout << "perfect number" << endl;
    else if (sum < n)
      cout << "deficient number" << endl;
    else
      cout << "abundant numver" << endl;
  }
}
Problem p01224 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  int N;
  ll S;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    S = 0;
    for (int i = 1; i * i < N; i++)
      if (N % i == 0)
        S += (i + N / i);
    if (N != 1)
      S -= N;
    if (N == S)
      cout << "perfect number" << endl;
    else if (N > S)
      cout << "deficient number" << endl;
    else
      cout << "abundant number" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;
int main() {
  int N;
  ll S;
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    S = 0;
    for (int i = 1; i <= N / 2; i++)
      if (N % i == 0)
        S += i;
    if (N == S)
      cout << "perfect number" << endl;
    else if (N > S)
      cout << "deficient number" << endl;
    else
      cout << "abundant number" << endl;
  }
  return 0;
}
Problem p01225 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>

#define NUMBER 10
#define CARD_NUM 9
#define COLOR_NUM 3

using namespace std;

const char *color = "RGB";

bool checkSet(int *card, int num) {
  int numnum[NUMBER] = {
      0,
  };
  for (int i = 0; i < num; ++i) {
    numnum[card[i]]++;
  }
  for (int i = 1; i < NUMBER - 2; ++i) {
    if (numnum[i] >= 1 && numnum[i + 1] >= 1 && numnum[i + 2] >= 1) {
      numnum[i]--;
      numnum[i + 1]--;
      numnum[i + 2]--;
      --i;
    }
  }
  for (int i = 1; i < NUMBER; ++i) {
    if (numnum[i] == 3) {
      numnum[i] -= 3;
    }
  }

  for (int i = 1; i < NUMBER; ++i) {
    if (numnum[i] > 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    bool win = true;
    int ccardnum[COLOR_NUM] = {
        0,
    };
    int card[CARD_NUM];
    int ccard[COLOR_NUM][CARD_NUM];
    char cardcolor[CARD_NUM];

    for (int j = 0; j < CARD_NUM; ++j)
      cin >> card[j];
    for (int j = 0; j < CARD_NUM; ++j)
      cin >> cardcolor[j];

    for (int k = 0; k < COLOR_NUM; ++k) {
      for (int j = 0; j < CARD_NUM; ++j) {
        if (cardcolor[j] == color[k]) {
          ccard[k][ccardnum[k]] = card[j];
          ccardnum[k]++;
        }
      }
    }
    for (int k = 0; k < COLOR_NUM; ++k) {
      if (!checkSet(ccard[k], ccardnum[k])) {
        win = false;
        break;
      }
    }

    if (win)
      cout << '1';
    else
      cout << '0';
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

#define CARD_NUM 9
#define COLOR_NUM 3

using namespace std;

const char *color = "RGB";

bool checkSet(int *card, int num) {
  sort(card, card + num);
  int numnum[CARD_NUM] = {
      0,
  };
  for (int i = 0; i < num; ++i) {
    numnum[card[i]]++;
  }
  for (int i = 0; i < num; ++i) {
    if (numnum[card[i]] == 3) {
      numnum[card[i]] -= 3;
    }
  }
  for (int i = 0; i < CARD_NUM - 3; ++i) {
    if (numnum[i] >= 1 && numnum[i + 1] >= 1 && numnum[i + 2] >= 1) {
      numnum[i]--;
      numnum[i + 1]--;
      numnum[i + 2]--;
    }
  }
  for (int i = 0; i < CARD_NUM; ++i) {
    if (numnum[i] > 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    bool win = true;
    int ccardnum[COLOR_NUM] = {
        0,
    };
    int card[CARD_NUM];
    int ccard[COLOR_NUM][CARD_NUM];
    char cardcolor[CARD_NUM];
    for (int j = 0; j < CARD_NUM; ++j) {
      cin >> card[j];
    }
    for (int j = 0; j < CARD_NUM; ++j) {
      cin >> cardcolor[j];
    }
    for (int k = 0; k < COLOR_NUM; ++k) {
      for (int j = 0; j < CARD_NUM; ++j) {
        if (cardcolor[j] == color[k]) {
          ccard[k][ccardnum[k]++] = card[j];
        }
      }
    }
    for (int k = 0; k < COLOR_NUM; ++k) {
      if (!checkSet(ccard[k], ccardnum[k])) {
        win = false;
        break;
      }
    }

    if (win) {
      cout << '1';
    } else {
      cout << '0';
    }
    cout << endl;
  }
  return 0;
}
Problem p01228 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

ll gcd(ll m, ll n) { return n ? gcd(n, m % n) : m; }

ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }

string hexstr = "0123456789ABCDEF";

int main() {
  int T;
  cin >> T;
  int abc = 0;
  while (T--) {
    int N;
    cin >> N;
    vector<string> v(N);
    vector<int> gcds;
    for (int i = 0; i < N; ++i)
      cin >> v[i];

    ll total = 1;
    for (auto &s : v) {
      int g = s.size() / 2;
      for (int i = 0; i < s.size(); i += 2) {
        if (s[i] != '0' || s[i + 1] != '0')
          g = gcd(g, i / 2);
      }
      total = lcm(total, s.size() / 2 / g);
      total = min(total, 1025ll);
      gcds.push_back(g);
    }

    if (total * 2 <= 2048) {
      vector<int> ans(total);
      for (int j = 0; j < v.size(); ++j) {
        string &s = v[j];
        int g = gcds[j];
        int mul = total / (s.size() / 2 / g);
        for (int i = 0; i < s.size(); i += 2) {
          if (s[i] == '0' && s[i + 1] == '0')
            continue;
          int pos = i / 2 / g * mul;
          ans[pos] |= (s[i] - '0') * 16;
          ans[pos] |= (s[i + 1] - '0');
        }
      }
      for (auto &e : ans) {
        int x = e / 16, y = e % 16;
        cout << hexstr[x] << hexstr[y];
      }
      cout << endl;
    } else {
      cout << "Too complex." << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

ll gcd(ll m, ll n) { return n ? gcd(n, m % n) : m; }

ll lcm(ll m, ll n) { return m / gcd(m, n) * n; }

string hexstr = "0123456789ABCDEF";

int main() {
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    vector<string> v(N);
    vector<int> gcds;
    for (int i = 0; i < N; ++i)
      cin >> v[i];

    ll total = 1;
    for (auto &s : v) {
      int g = s.size() / 2;
      for (int i = 0; i < s.size(); i += 2) {
        if (s[i] != '0' || s[i + 1] != '0')
          g = gcd(g, i / 2);
      }
      total = lcm(total, s.size() / 2 / g);
      gcds.push_back(g);
    }
    if (total * 2 <= 2048) {
      vector<int> ans(total);
      for (int j = 0; j < v.size(); ++j) {
        string &s = v[j];
        int g = gcds[j];
        int mul = total / (s.size() / 2 / g);
        for (int i = 0; i < s.size(); i += 2) {
          if (s[i] == '0' && s[i + 1] == '0')
            continue;
          int pos = i / 2 / g * mul;
          ans[pos] |= (s[i] - '0') * 16;
          ans[pos] |= (s[i + 1] - '0');
        }
      }
      for (auto &e : ans) {
        int x = e / 16, y = e % 16;
        cout << hexstr[x] << hexstr[y];
      }
      cout << endl;
    } else {
      cout << "Too complex." << endl;
    }
  }

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

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

int n, maxi;
int Ea[81], dp[81][321][321];

int rec(int idx, int prev, int now) {
  if (idx == n - 1)
    return now;
  if (dp[idx][prev][now])
    return dp[idx][prev][now];
  int res = 0;
  res = max(res, rec(idx + 1, now, Ea[idx + 1]));
  if (now > 0)
    res = max(res, rec(idx + 1, now - 1, prev + Ea[idx + 1]));
  return dp[idx][prev][now] = res;
}

int main() {
  int N;
  cin >> N;
  while (N--) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> Ea[i];
    cout << ((n == 1) ? Ea[0] : rec(1, Ea[0], Ea[1])) << endl;
  }
}

Error: TLE

Fixed submission:

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

int n, maxi;
int Ea[81], dp[81][321][321];

int rec(int idx, int prev, int now) {
  if (idx == n - 1)
    return now;
  if (dp[idx][prev][now])
    return dp[idx][prev][now];
  int res = 0;
  res = max(res, rec(idx + 1, now, Ea[idx + 1]));
  for (int i = 0; i < n; i++) {
    if (now > 0)
      res = max(res, rec(idx + 1, now - 1, prev + Ea[idx + 1]));
  }
  return dp[idx][prev][now] = res;
}

int main() {
  int N;
  cin >> N;
  while (N--) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> Ea[i];
    cout << ((n == 1) ? Ea[0] : rec(1, Ea[0], Ea[1])) << endl;
  }
}
Problem p01229 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 15
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

bool p[400][400][81];

int dfs(vector<int> v) {
  if (v.size() <= 2) {
    return v.back();
  } else {
    if (p[v[0]][v[1]][v.size()]++) {
      return 0;
    } else {
      int n = dfs(vector<int>(v.begin() + 1, v.end()));
      if (v[1]) {
        v[2] += v[0];
        v[1]--;
        n = max(n, dfs(vector<int>(v.begin() + 1, v.end())));
      }
      return n;
    }
  }
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    int l;
    cin >> l;
    vector<int> v(l);
    for (int i = 0; i < l; i++) {
      cin >> v[i];
    }
    fill(p[0][0], p[400][0], false);
    cout << dfs(v) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

// 15
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int p[400][400][81];

int dfs(vector<int> v) {
  if (v.size() <= 2) {
    return v.back();
  } else {
    if (p[v[0]][v[1]][v.size()]++) {
      return 0;
    } else {
      int n = dfs(vector<int>(v.begin() + 1, v.end()));
      if (v[1]) {
        v[2] += v[0];
        v[1]--;
        n = max(n, dfs(vector<int>(v.begin() + 1, v.end())));
      }
      return n;
    }
  }
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    int l;
    cin >> l;
    vector<int> v(l);
    for (int i = 0; i < l; i++) {
      cin >> v[i];
    }
    fill(p[0][0], p[400][0], false);
    cout << dfs(v) << endl;
  }
  return 0;
}
Problem p01229 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> P2;
vector<P2> v1, v2;
void solve() {
  int n, sum = 0, ans = 0;
  cin >> n;
  int a[n + 1];
  r(i, n) cin >> a[i], sum += a[i];
  if (n <= 2) {
    cout << a[n - 1] << endl;
    return;
  }
  a[n] = 0;
  v1.clear();
  v2.clear();
  v1.push_back(P2(P(a[0], a[1]), a[2]));
  r(l, n - 2) {
    set<P2> s;
    r(o, v1.size()) {
      int i = v1[o].first.first;
      int j = v1[o].first.second;
      int k = v1[o].second;
      if (j) {
        P2 p = P2(P(j - 1, k + i), a[l + 3]);
        if (!s.count(p)) {
          v2.push_back(p);
          s.insert(p);
        }
      }
      if (k) {
        P2 p = P2(P(0, k - 1), a[l + 3] + j);
        if (!s.count(p)) {
          v2.push_back(p);
          s.insert(p);
        }
      }
      P2 p = P2(P(j, k), a[l + 3]);
      if (!s.count(p)) {
        v2.push_back(p);
        s.insert(p);
      }
    }
    v1 = v2;
    v2.clear();
  }
  r(o, v1.size()) {
    int j = v1[o].first.second;
    ans = max(j, ans);
  }
  cout << ans << endl;
}
int main() {
  int TEST;
  cin >> TEST;
  while (TEST--)
    solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> P2;
vector<P2> v1, v2;
void solve() {
  int n, sum = 0, ans = 0;
  cin >> n;
  int a[n + 1];
  r(i, n) cin >> a[i], sum += a[i];
  if (n <= 2) {
    cout << a[n - 1] << endl;
    return;
  }
  a[n] = 0;
  v1.clear();
  v2.clear();
  v1.push_back(P2(P(a[0], a[1]), a[2]));
  r(l, n - 2) { // cout<<l+3<<endl;
    r(o, v1.size()) {
      int i = v1[o].first.first;
      int j = v1[o].first.second;
      int k = v1[o].second;
      if (j)
        v2.push_back(P2(P(j - 1, k + i), a[l + 3]));
      if (k)
        v2.push_back(P2(P(0, k - 1), a[l + 3] + j));
      v2.push_back(P2(P(j, k), a[l + 3]));
    }
    v1 = v2;
    v2.clear();
  }
  r(o, v1.size()) {
    int j = v1[o].first.second;
    ans = max(j, ans);
  }
  cout << ans << endl;
}
int main() {
  int TEST;
  cin >> TEST;
  while (TEST--)
    solve();
}
Problem p01229 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <set>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int n;
int e[80];
int memo[80][100][100];

int solve(int pos, int prev, int now) {
  if (pos == n - 1)
    return now;
  if (prev < 100 && now < 100)
    if (memo[pos][prev][now] != -1)
      return memo[pos][prev][now];

  int ret = 0;

  if (now > 0) {
    // hit
    ret = max(ret, solve(pos + 1, now - 1, e[pos + 1] + prev));
  }

  ret = max(ret, solve(pos + 1, now, e[pos + 1]));

  return memo[pos][prev][now] = ret;
}

int main() {
  int cc = getInt();

  while (cc-- > 0) {
    n = getInt();
    REP(i, n) e[i] = getInt();
    memset(memo, -1, sizeof(memo));

    if (n >= 3)
      printf("%d\\n", solve(1, e[0], e[1]));
    else
      printf("%d\\n", e[n - 1]);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <set>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

int n;
int e[80];
int memo[80][350][350];

int solve(int pos, int prev, int now) {
  if (pos == n - 1)
    return now;
  if (memo[pos][prev][now] != -1)
    return memo[pos][prev][now];

  int ret = 0;

  if (now > 0) {
    // hit
    ret = max(ret, solve(pos + 1, now - 1, e[pos + 1] + prev));
  }

  ret = max(ret, solve(pos + 1, now, e[pos + 1]));

  return memo[pos][prev][now] = ret;
}

int main() {
  int cc = getInt();

  while (cc-- > 0) {
    n = getInt();
    REP(i, n) e[i] = getInt();
    memset(memo, -1, sizeof(memo));

    if (n >= 3)
      printf("%d\\n", solve(1, e[0], e[1]));
    else
      printf("%d\\n", e[n - 1]);
  }

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

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

void p(bool a[101][101], bool b[101][101]);
void pow(bool a[101][101], unsigned int z) {
  bool e[101][101];
  memset(e, 0, sizeof e);
  for (int i = 0; i < 101; i++)
    e[i][i] = 1;
  while (z) {
    if (z & 1) {
      p(e, a);
    }
    p(a, a);
    z >>= 1;
  }
  memcpy(a, e, sizeof e);
}

void p(bool a[101][101], bool b[101][101]) {
  bool c[101][101];
  memset(c, 0, sizeof c);
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
      for (int k = 0; k < 101; k++) {
        c[i][j] |= a[i][k] & b[k][j];
      }
    }
  }
  memcpy(a, c, sizeof c);
}

int main() {
  unsigned int n, m, z;
  cin >> n >> m >> z;
  while (n) {
    int r[101][2];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      r[i][0] = r[m + i][1] = a;
      r[i][1] = r[m + i][0] = b;
    }
    r[2 * m][0] = 0;
    r[2 * m][1] = 1;
    bool a[101][101];
    memset(a, 0, sizeof a);
    for (int i = 0; i <= 2 * m; i++) {
      for (int j = 0; j <= 2 * m; j++) {
        if (r[i][1] == r[j][0] && r[i][0] != r[j][1])
          a[j][i] = 1;
      }
    }
    bool v[101] = {false};
    v[2 * m] = 1;
    pow(a, z);
    bool _v[101] = {false};
    for (int j = 0; j <= 2 * m; j++) {
      for (int k = 0; k <= 2 * m; k++) {
        if (a[j][k] && v[k])
          _v[j] = 1;
      }
    }
    bool f = false;
    for (int i = 0; i < 2 * m; i++) {
      if (_v[i] && r[i][1] == n) {
        f = true;
        break;
      }
    }
    if (f)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
    cin >> n >> m >> z;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  unsigned int n, m, z;
  cin >> n >> m >> z;
  while (n) {
    int r[101][2];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      r[i][0] = r[m + i][1] = a;
      r[i][1] = r[m + i][0] = b;
    }
    r[2 * m][0] = 0;
    r[2 * m][1] = 1;
    bool a[101][101];
    memset(a, 0, sizeof a);
    for (int i = 0; i <= 2 * m; i++) {
      for (int j = 0; j <= 2 * m; j++) {
        if (r[i][1] == r[j][0] && r[i][0] != r[j][1])
          a[j][i] = 1;
      }
    }
    bool v[101] = {false};
    v[2 * m] = 1;
    for (unsigned int i = 0; i < z; i++) {
      bool _v[101] = {false};
      for (int j = 0; j <= 2 * m; j++) {
        for (int k = 0; k <= 2 * m; k++) {
          if (a[j][k] && v[k])
            _v[j] = 1;
        }
      }
      memcpy(v, _v, sizeof v);
    }
    bool f = false;
    for (int i = 0; i < 2 * m; i++) {
      if (v[i] && r[i][1] == n) {
        f = true;
        break;
      }
    }
    if (f)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
    cin >> n >> m >> z;
  }
}
Problem p01231 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
short dp[5010][60][60];
int g[60][60];
int n;
int solve(int a, int b, int c) {
  if (dp[a][b][c] >= 0)
    return dp[a][b][c];
  if (b == c)
    return dp[a][b][c] = a;
  if (a == 2 * n) {
    return dp[a][b][c] = a;
  }
  if (a % 2 == 0) {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      if (g[b][i]) {
        ret = max(ret, solve(a + 1, i, c));
      }
    }
    //	printf("%d %d %d: %d\\n",a,b,c,ret);
    return dp[a][b][c] = ret;
  } else {
    int ret = 2 * n;
    for (int i = 0; i < n; i++) {
      if (g[c][i])
        ret = min(ret, solve(a + 1, b, i));
    }
    //	printf("%d %d %d: %d\\n",a,b,c,ret);
    return dp[a][b][c] = ret;
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int a;
    scanf("%d", &a);
    n = a;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        scanf("%d", &g[i][j]);
    for (int i = 0; i <= 2 * a; i++)
      for (int j = 0; j < a; j++)
        for (int k = 0; k < a; k++)
          dp[i][j][k] = -1;
    for (int i = 0; i < a; i++)
      g[i][i] = 1;
    int s, t;
    scanf("%d%d", &s, &t);
    solve(0, s, t);
    int ret = solve(0, s, t) / 2;
    if (ret == a)
      printf("infinity\\n");
    else
      printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
short dp[5010][60][60];
int g[60][60];
int n;
int solve(int a, int b, int c) {
  if (dp[a][b][c] >= 0)
    return dp[a][b][c];
  if (b == c)
    return dp[a][b][c] = a;
  if (a == 2 * n * n) {
    return dp[a][b][c] = a;
  }
  if (a % 2 == 0) {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      if (g[b][i]) {
        ret = max(ret, solve(a + 1, i, c));
      }
    }
    //	printf("%d %d %d: %d\\n",a,b,c,ret);
    return dp[a][b][c] = ret;
  } else {
    int ret = 2 * n * n;
    for (int i = 0; i < n; i++) {
      if (g[c][i])
        ret = min(ret, solve(a + 1, b, i));
    }
    //	printf("%d %d %d: %d\\n",a,b,c,ret);
    return dp[a][b][c] = ret;
  }
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int a;
    scanf("%d", &a);
    n = a;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        scanf("%d", &g[i][j]);
    for (int i = 0; i <= 2 * a * a; i++)
      for (int j = 0; j < a; j++)
        for (int k = 0; k < a; k++)
          dp[i][j][k] = -1;
    for (int i = 0; i < a; i++)
      g[i][i] = 1;
    int s, t;
    scanf("%d%d", &s, &t);
    solve(0, s, t);
    int ret = solve(0, s, t) / 2;
    if (ret == a * a)
      printf("infinity\\n");
    else
      printf("%d\\n", ret);
  }
}