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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 99999999

#define MAX_S 200
#define MAX_T 200

template <typename T> int lcs(T s, T t) {
  int lens = s.size();
  int lent = t.size();
  int dp[MAX_S + 1][MAX_T + 1] = {};

  rep(i, lens) {
    rep(j, lent) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  return dp[lens][lent];
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s, t;
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 99999999

#define MAX_S 2000
#define MAX_T 2000

template <typename T> int lcs(T s, T t) {
  int lens = s.size();
  int lent = t.size();
  int dp[MAX_S + 1][MAX_T + 1] = {};

  rep(i, lens) {
    rep(j, lent) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }
  return dp[lens][lent];
}

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s, t;
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
#define MAX 100000

static const int N = 100;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int ans = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i; i <= m; i++)
    c[i][0] = 0;
  for (int j; j <= n; j++)
    c[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      ans = max(ans, c[i][j]);
    }
  }
  return ans;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
#define MAX 100000

static const int N = 1000;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int ans = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i; i <= m; i++)
    c[i][0] = 0;
  for (int j; j <= n; j++)
    c[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      ans = max(ans, c[i][j]);
    }
  }
  return ans;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}
Problem p02235 (C++)

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

int main(void) {
  string st1, st2;
  int q, val[151][151];

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> st1;
    cin >> st2;

    for (int j = 0; j <= st1.length(); j++) {
      for (int k = 0; k <= st2.length(); k++) {

        if (j == 0 || k == 0)
          val[j][k] = 0;
        else {
          if (val[j - 1][k] == min(st1.length(), st2.length())) {
            val[j][k] = st1.length();
          } else {

            if (st1[j - 1] == st2[k - 1]) {
              val[j][k] = val[j - 1][k - 1] + 1;
            } else {
              val[j][k] = max(val[j - 1][k], val[j][k - 1]);
            }
          }
        }
      }
    }

    /*
      for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
      cout << val[j][k] << " ";
      }
      cout << "\n";
      }
    */

    cout << val[st1.length()][st2.length()] << "\n";
  }

  return 0;
}


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

int main(void) {
  string st1, st2;
  int q, val[1001][1001];

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> st1;
    cin >> st2;

    for (int j = 0; j <= st1.length(); j++) {
      for (int k = 0; k <= st2.length(); k++) {

        if (j == 0 || k == 0)
          val[j][k] = 0;
        else {
          if (val[j - 1][k] == min(st1.length(), st2.length())) {
            val[j][k] = st1.length();
          } else {

            if (st1[j - 1] == st2[k - 1]) {
              val[j][k] = val[j - 1][k - 1] + 1;
            } else {
              val[j][k] = max(val[j - 1][k], val[j][k - 1]);
            }
          }
        }
      }
    }

    /*
      for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
      cout << val[j][k] << " ";
      }
      cout << "\n";
      }
    */

    cout << val[st1.length()][st2.length()] << "\n";
  }

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
///
// File:  alds1_10_c.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 01:36:16 2017
//

#include <cstdint>
#include <iostream>
#include <string>

using namespace std;
static uint32_t dp[1000][1000];

static void init_dp(uint32_t len1, uint32_t len2) {
  for (uint32_t i = 0; i < len1; i++) {
    dp[i][0] = 0;
  }
  for (uint32_t i = 0; i < len2; i++) {
    dp[0][i] = 0;
  }
}

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str1, str2;
    cin >> str1;
    cin >> str2;
    init_dp(str1.size(), str2.size());
    for (uint32_t i = 1; i <= str1.size(); i++) {
      for (uint32_t j = 1; j <= str2.size(); j++) {
        if (str1[i - 1] == str2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[str1.size()][str2.size()] << endl;
  }

  return 0;
}

Fixed submission:
///
// File:  alds1_10_c.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 01:36:16 2017
//

#include <cstdint>
#include <iostream>
#include <string>

using namespace std;
static uint32_t dp[1001][1001];

static void init_dp(uint32_t len1, uint32_t len2) {
  for (uint32_t i = 0; i < len1; i++) {
    dp[i][0] = 0;
  }
  for (uint32_t i = 0; i < len2; i++) {
    dp[0][i] = 0;
  }
}

int32_t main() {
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str1, str2;
    cin >> str1;
    cin >> str2;
    init_dp(str1.size(), str2.size());
    for (uint32_t i = 1; i <= str1.size(); i++) {
      for (uint32_t j = 1; j <= str2.size(); j++) {
        if (str1[i - 1] == str2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[str1.size()][str2.size()] << endl;
  }

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int q;

string data[MAX_N][2];
ll mem[MAX_L][MAX_L];

ll eval(string s1, string s2, int i, int j) {
  int ii = i - 1;
  int jj = j - 1;
  if (ii == -1 || jj == -1)
    return 0;
  if (s1[ii] == s2[jj])
    return mem[i - 1][j - 1] + 1;
  if (s1[ii] != s2[jj])
    return max(mem[i - 1][j], mem[i][j - 1]);
  throw "exception";
}

ll solve(string s1, string s2) {

  int L1 = s1.size();
  int L2 = s2.size();
  rep(i, L1 + 1) {
    rep(j, L2 + 1) { mem[i][j] = eval(s1, s2, i, j); }
  }

  return mem[L1][L2];
}

int main() {

  cin >> q;
  rep(i, q) {
    cin >> data[i][0];
    cin >> data[i][1];
  }

  rep(i, q) { cout << solve(data[i][0], data[i][1]) << endl; }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int MAX_L = 10000;
int q;

string data[MAX_N][2];
ll mem[MAX_L][MAX_L];

ll eval(string &s1, string &s2, int i, int j) {
  int ii = i - 1;
  int jj = j - 1;
  if (ii == -1 || jj == -1)
    return 0;
  if (s1[ii] == s2[jj])
    return mem[i - 1][j - 1] + 1;
  if (s1[ii] != s2[jj])
    return max(mem[i - 1][j], mem[i][j - 1]);
  throw "exception";
}

ll solve(string s1, string s2) {

  int L1 = s1.size();
  int L2 = s2.size();
  rep(i, L1 + 1) {
    rep(j, L2 + 1) { mem[i][j] = eval(s1, s2, i, j); }
  }

  return mem[L1][L2];
}

int main() {

  cin >> q;
  rep(i, q) {
    cin >> data[i][0];
    cin >> data[i][1];
  }

  rep(i, q) { cout << solve(data[i][0], data[i][1]) << endl; }
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void solve() {
  string x, y;
  cin >> x >> y;
  int xlen = x.length();
  int ylen = y.length();
  int dp[155][155];
  dp[0][0] = 0;

  for (int i = 0; i <= xlen; i++) {
    for (int j = 0; j <= ylen; j++) {
      if (i == 0 || j == 0) {
        dp[i + 1][j + 1] = 0;
      } else if (x[i - 1] == y[j - 1]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  cout << dp[xlen + 1][ylen + 1] << endl;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++)
    solve();
}

Fixed submission:
#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

void solve() {
  string x, y;
  cin >> x >> y;
  int xlen = x.length();
  int ylen = y.length();
  int dp[1010][1010];
  dp[0][0] = 0;

  for (int i = 0; i <= xlen; i++) {
    for (int j = 0; j <= ylen; j++) {
      if (i == 0 || j == 0) {
        dp[i + 1][j + 1] = 0;
      } else if (x[i - 1] == y[j - 1]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      }
    }
  }
  cout << dp[xlen + 1][ylen + 1] << endl;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++)
    solve();
}
Problem p02235 (C++)

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

int main() {
  int n, m, q;
  string a, b;               // two sequences
  int M[1001][1001] = {{0}}; // Memory matrix

  cin >> q;
  for (int l = 0; l < q; l++) {
    cin >> a >> b;
    n = a.length();
    m = b.length();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a[i] == b[j]) {
          M[i + 1][j + 1] = M[i][j] + 1;
        } else {
          M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
        }
      }
    }

    cout << M[n][m] << endl; // The time complexity equals to O (n * m)
  }
  return 0;
}

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

int main() {
  int n, m, q;
  string a, b;               // two sequences
  int M[1010][1010] = {{0}}; // Memory matrix

  cin >> q;
  for (int l = 0; l < q; l++) {
    cin >> a >> b;
    n = a.length();
    m = b.length();
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= m; j++) {
        if (a[i] == b[j]) {
          M[i + 1][j + 1] = M[i][j] + 1;
        } else {
          M[i + 1][j + 1] = max(M[i][j + 1], M[i + 1][j]);
        }
      }
    }

    cout << M[n][m] << endl; // The time complexity equals to O (n * m)
  }
  return 0;
}
Problem p02235 (C++)

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

int main() {
  int q, t1, t2;
  string s1, s2;
  const int maxS = 10;
  int m[maxS][maxS];

  cin >> q;

  for (int i = 0; i < q; i++) {

    for (int i = 0; i < maxS; i++)
      for (int j = 0; j < maxS; j++)
        m[i][j] = 0;

    cin >> s1 >> s2;

    t1 = s1.size() - 1;
    t2 = s2.size() - 1;

    if (s1[0] == s2[0])
      m[0][0] = 1;

    for (int i = 0; i < t1; i++) {
      int f = 0;
      if (s1[i + 1] == s2[0])
        f = 1;
      m[i + 1][0] = max(m[i][0], f);
    }

    for (int j = 0; j < t2; j++) {
      int f = 0;
      if (s2[j + 1] == s1[0])
        f = 1;
      m[0][j + 1] = max(m[0][j], f);
    }

    for (int i = 0; i < t1; i++) {
      for (int j = 0; j < t2; j++) {
        if (s1[i + 1] == s2[j + 1])
          m[i + 1][j + 1] = m[i][j] + 1;
        else
          m[i + 1][j + 1] = max(m[i][j + 1], m[i + 1][j]);
      }
    }

    cout << m[t1][t2] << endl;
  }
  return 0;
}


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

int main() {
  int q, t1, t2;
  string s1, s2;
  const int maxS = 1000;
  int m[maxS][maxS];

  cin >> q;

  for (int i = 0; i < q; i++) {

    for (int i = 0; i < maxS; i++)
      for (int j = 0; j < maxS; j++)
        m[i][j] = 0;

    cin >> s1 >> s2;

    t1 = s1.size() - 1;
    t2 = s2.size() - 1;

    if (s1[0] == s2[0])
      m[0][0] = 1;

    for (int i = 0; i < t1; i++) {
      int f = 0;
      if (s1[i + 1] == s2[0])
        f = 1;
      m[i + 1][0] = max(m[i][0], f);
    }

    for (int j = 0; j < t2; j++) {
      int f = 0;
      if (s2[j + 1] == s1[0])
        f = 1;
      m[0][j + 1] = max(m[0][j], f);
    }

    for (int i = 0; i < t1; i++) {
      for (int j = 0; j < t2; j++) {
        if (s1[i + 1] == s2[j + 1])
          m[i + 1][j + 1] = m[i][j] + 1;
        else
          m[i + 1][j + 1] = max(m[i][j + 1], m[i + 1][j]);
      }
    }

    cout << m[t1][t2] << endl;
  }
  return 0;
}
Problem p02235 (C++)

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

int main() {
  int n;
  char x[10000], y[10000];
  int dp[10010][10010];
  // load n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    // load string
    scanf("%s", x);
    scanf("%s", y);
    // dp
    for (int j = 0; j < strlen(x); j++) {
      for (int k = 0; k < strlen(y); k++) {
        if (x[j] == y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        }

        else {
          if (dp[j][k + 1] > dp[j + 1][k]) {
            dp[j + 1][k + 1] = dp[j][k + 1];
          }

          else {
            dp[j + 1][k + 1] = dp[j + 1][k];
          }
        }
      }
    }
    cout << dp[strlen(x)][strlen(y)] << endl;
  }

  return 0;
}

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

int main() {
  int n;
  char x[1001], y[1001];
  int dp[1010][1010];
  // load n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    // load string
    scanf("%s", x);
    scanf("%s", y);
    // dp
    for (int j = 0; j < strlen(x); j++) {
      for (int k = 0; k < strlen(y); k++) {
        if (x[j] == y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        }

        else {
          if (dp[j][k + 1] > dp[j + 1][k]) {
            dp[j + 1][k + 1] = dp[j][k + 1];
          }

          else {
            dp[j + 1][k + 1] = dp[j + 1][k];
          }
        }
      }
    }
    cout << dp[strlen(x)][strlen(y)] << endl;
  }

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
void solve(void);
int q, count[MAX][MAX] = {}, xlength, ylength;
char X[MAX], Y[MAX];

void solve(void) {
  int i, j;

  xlength = strlen(X);
  ylength = strlen(Y);

  for (i = 1; i <= xlength; i++) {
    for (j = 1; j <= ylength; j++) {
      if (X[i - 1] == Y[j - 1])
        count[i][j] = count[i - 1][j - 1] + 1;
      else if (count[i - 1][j] >= count[i][j - 1])
        count[i][j] = count[i - 1][j];
      else
        count[i][j] = count[i][j - 1];
    }
  }
}

int main() {
  int i;

  scanf("%d", &q);

  for (i = 1; i <= q; i++) {
    scanf("%s", X);
    scanf("%s", Y);
    solve();
    printf("%d\n", count[xlength][ylength]);
  }

  /*for(i=0;i<q;i++){
    printf("%d\n",count[][]);
    }*/

  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1001
void solve(void);
int q, count[MAX][MAX] = {}, xlength, ylength;
char X[MAX], Y[MAX];

void solve(void) {
  int i, j;

  xlength = strlen(X);
  ylength = strlen(Y);

  for (i = 1; i <= xlength; i++) {
    for (j = 1; j <= ylength; j++) {
      if (X[i - 1] == Y[j - 1])
        count[i][j] = count[i - 1][j - 1] + 1;
      else if (count[i - 1][j] >= count[i][j - 1])
        count[i][j] = count[i - 1][j];
      else
        count[i][j] = count[i][j - 1];
    }
  }
}

int main() {
  int i;

  scanf("%d", &q);

  for (i = 1; i <= q; i++) {
    scanf("%s", X);
    scanf("%s", Y);
    solve();
    printf("%d\n", count[xlength][ylength]);
  }

  /*for(i=0;i<q;i++){
    printf("%d\n",count[][]);
    }*/

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int dp[1000][1000];

int main() {
  int q;
  cin >> q;
  REP(loop, q) {
    char S[1000], T[1000];
    cin >> S >> T;

    REP(i, strlen(S)) {
      REP(j, strlen(T)) { dp[i][j] = 0; }
    }
    for (int i = 0; i < strlen(S); i++) {
      for (int j = 0; j < strlen(T); j++) {
        if (S[i] == T[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    cout << dp[strlen(S)][strlen(T)] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int dp[1010][1010];

int main() {
  int q;
  cin >> q;
  REP(loop, q) {
    char S[1000], T[1000];
    cin >> S >> T;

    REP(i, strlen(S)) {
      REP(j, strlen(T)) { dp[i][j] = 0; }
    }
    for (int i = 0; i < strlen(S); i++) {
      for (int j = 0; j < strlen(T); j++) {
        if (S[i] == T[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    cout << dp[strlen(S)][strlen(T)] << endl;
  }

  return 0;
}
Problem p02235 (C++)

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

int N, len1, len2;
string seq1, seq2;
int dp[1000][1000];

int main() {
  cin >> N;
  for (int M = 0; M < N; M++) {
    cin >> seq1 >> seq2;
    len1 = seq1.length();
    len2 = seq2.length();
    for (int i = 1; i <= len1; i++) {
      for (int j = 1; j <= len2; j++) {
        if (seq1[i - 1] == seq2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else if (dp[i][j - 1] > dp[i - 1][j]) {
          dp[i][j] = dp[i][j - 1];
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    cout << dp[len1][len2] << endl;
  }

  return 0;
}

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

int N, len1, len2;
string seq1, seq2;
int dp[1000 + 10][1000 + 10];

int main() {
  cin >> N;
  for (int M = 0; M < N; M++) {
    cin >> seq1 >> seq2;
    len1 = seq1.length();
    len2 = seq2.length();
    for (int i = 1; i <= len1; i++) {
      for (int j = 1; j <= len2; j++) {
        if (seq1[i - 1] == seq2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else if (dp[i][j - 1] > dp[i - 1][j]) {
          dp[i][j] = dp[i][j - 1];
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    cout << dp[len1][len2] << endl;
  }

  return 0;
}
Problem p02235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a, b) (a >> b) & 1
#define BitSet(a, b) a |= (1 << b)
#define BitunSet(a, b) a &= ~(1 << b)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main() {
  int n, m, a, dp[102][102] = {0};
  char s[1001], t[1001];
  scanf("%d", &a);
  for (int k = 0; a > k; k++) {
    scanf("%s %s", s, t);
    n = strlen(s);
    m = strlen(t);
    for (int i = 1; n >= i; i++) {
      for (int j = 1; m >= j; j++) {
        if (s[i - 1] == t[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
    printf("%d\n", dp[n][m]);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a, b) (a >> b) & 1
#define BitSet(a, b) a |= (1 << b)
#define BitunSet(a, b) a &= ~(1 << b)
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main() {
  int n, m, a, dp[1002][1002] = {0};
  char s[1001], t[1001];
  scanf("%d", &a);
  for (int k = 0; a > k; k++) {
    scanf("%s %s", s, t);
    n = strlen(s);
    m = strlen(t);
    for (int i = 1; n >= i; i++) {
      for (int j = 1; m >= j; j++) {
        if (s[i - 1] == t[j - 1])
          dp[i][j] = dp[i - 1][j - 1] + 1;
        else
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
    printf("%d\n", dp[n][m]);
  }
  return 0;
}
Problem p02235 (C++)

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

const int MAX_LENGTH = 256;

char A[MAX_LENGTH + 1];
char B[MAX_LENGTH + 1];

int tu[MAX_LENGTH][MAX_LENGTH];

void lcs(int, int);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A >> B;
    int lA = strlen(A), lB = strlen(B);
    lcs(lA, lB);
    cout << tu[lA][lB] << endl;
  }
}

void lcs(int lA, int lB) {
  for (int i = 0; i < lA; ++i)
    tu[0][i] = 0;
  for (int i = 0; i < lB; ++i)
    tu[i][0] = 0;

  for (int i = 1; i <= lA; i++) {
    for (int j = 1; j <= lB; j++) {
      if (A[i - 1] == B[j - 1])
        tu[i][j] = tu[i - 1][j - 1] + 1;
      else
        tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]);
    }
  }
}

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

const int MAX_LENGTH = 1024;

char A[MAX_LENGTH + 1];
char B[MAX_LENGTH + 1];

int tu[MAX_LENGTH][MAX_LENGTH];

void lcs(int, int);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> A >> B;
    int lA = strlen(A), lB = strlen(B);
    lcs(lA, lB);
    cout << tu[lA][lB] << endl;
  }
}

void lcs(int lA, int lB) {
  for (int i = 0; i < lA; ++i)
    tu[0][i] = 0;
  for (int i = 0; i < lB; ++i)
    tu[i][0] = 0;

  for (int i = 1; i <= lA; i++) {
    for (int j = 1; j <= lB; j++) {
      if (A[i - 1] == B[j - 1])
        tu[i][j] = tu[i - 1][j - 1] + 1;
      else
        tu[i][j] = max(tu[i][j - 1], tu[i - 1][j]);
    }
  }
}
Problem p02237 (C++)

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

const double pi = 2 * acos(0.0);
const double eps = 1e-8;

#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define INF (1 << 29)
#define INFLL (1L << 29)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst;
  Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Cost> Array;
typedef vector<Array> Matrix;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

#define MAX_N 100

int G[MAX_N][MAX_N];

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    rep(j, k) {
      int node_num;
      cin >> node_num;
      G[u][node_num] = 1;
    }
  }
  REP(i, 1, n + 1) {
    REP(j, 1, n + 1) {
      if (j != 1)
        cout << " ";
      cout << G[i][j];
    }
    cout << endl;
  }
  return 0;
}

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

const double pi = 2 * acos(0.0);
const double eps = 1e-8;

#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define INF (1 << 29)
#define INFLL (1L << 29)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int Cost;
struct Edge {
  int src, dst;
  Cost cost;
  Edge(int s, int d, Cost c) : src(s), dst(d), cost(c) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Cost> Array;
typedef vector<Array> Matrix;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

#define MAX_N 101

int G[MAX_N][MAX_N];

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) {
    int u, k;
    cin >> u >> k;
    rep(j, k) {
      int node_num;
      cin >> node_num;
      G[u][node_num] = 1;
    }
  }
  REP(i, 1, n + 1) {
    REP(j, 1, n + 1) {
      if (j != 1)
        cout << " ";
      cout << G[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p02237 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      G[u][v - 1] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j)
        cout << " ";
      cout << G[i][j];
    }
    cout << endl;
  }
}


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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      G[u - 1][v - 1] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j)
        cout << " ";
      cout << G[i][j];
    }
    cout << endl;
  }
}
Problem p02237 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
static const int N = 100;

int main() {
  int M[N][N];
  int n, u, k, v;

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; i < n; j++) {
      if (j)
        cout << " ";
      cout << M[i][j];
    }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
static const int N = 100;

int main() {
  int M[N][N];
  int n, u, k, v;

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j)
        cout << " ";
      cout << M[i][j];
    }
    cout << endl;
  }

  return 0;
}
Problem p02237 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>

#define MAX 100

int main() {
  int n;
  int M[MAX][MAX];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    int id, k;
    scanf("%d %d", &id, &k);
    if (k == 0)
      continue;
    else {
      for (int j = 1; j <= k; j++) {
        int v;
        scanf("%d", &v);
        M[id][v] = 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j == n)
        printf("%d\n", M[i][j]);
      else
        printf("%d ", M[i][j]);
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <stdio.h>

#define MAX 101

int main() {
  int n;
  int M[MAX][MAX];
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    int id, k;
    scanf("%d %d", &id, &k);
    if (k == 0)
      continue;
    else {
      for (int j = 1; j <= k; j++) {
        int v;
        scanf("%d", &v);
        M[id][v] = 1;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (j == n)
        printf("%d\n", M[i][j]);
      else
        printf("%d ", M[i][j]);
    }
  }

  return 0;
}
Problem p02237 (C++)

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
vector<int> adj[100];
int res[100][100] = {0};

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    REP(j, k) {
      int tmp;
      cin >> tmp;
      res[u][tmp] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++)
      cout << res[i][j] << " ";
    cout << res[i][n] << endl;
  }
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
const int INF = 1e9;

using namespace std;
vector<int> adj[100];
int res[105][105] = {0};

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    int u, k;
    cin >> u >> k;
    REP(j, k) {
      int tmp;
      cin >> tmp;
      res[u][tmp] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++)
      cout << res[i][j] << " ";
    cout << res[i][n] << endl;
  }
  return 0;
}
Problem p02237 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)             \
    ;

using namespace std;

int main() {

  int n;
  cin >> n;
  vector<vector<int>> ans(n, vector<int>(4, 0));

  rep(i, n) {
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k) {
      int v;
      cin >> v;
      v--;
      ans[u][v] = 1;
    }
  }

  rep(i, n) {
    rep(j, n - 1) { cout << ans[i][j] << " "; }
    cout << ans[i][n - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define DUMP(list)                                                             \
  cout << "{ ";                                                                \
  for (auto nth : list) {                                                      \
    cout << nth << " ";                                                        \
  }                                                                            \
  cout << "}" << endl
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)             \
    ;

using namespace std;

int main() {

  int n;
  cin >> n;
  vector<vector<int>> ans(n, vector<int>(n, 0));

  rep(i, n) {
    int u, k;
    cin >> u >> k;
    u--;
    rep(j, k) {
      int v;
      cin >> v;
      v--;
      ans[u][v] = 1;
    }
  }

  rep(i, n) {
    rep(j, n - 1) { cout << ans[i][j] << " "; }
    cout << ans[i][n - 1] << endl;
  }

  return 0;
}
Problem p02238 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[N][N];
int color[N], d[N], f[N], tt;

void dfs_visit(int u) {
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for (v = 0; v < n; v++) {
    if (M[u][v] == 0)
      continue;
    if (color[v] == WHITE) {
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++tt;
}

void dfs() {
  int u;
  for (u = 0; u < n; u++)
    color[u] = WHITE;
  tt = 0;

  for (u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }

  for (u = 0; u < n; u++) {
    printf("%d %d %d\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int u, k, v, i, j;

  scanf("%d", n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    u--;
    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}

Fixed submission:
#include <stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[N][N];
int color[N], d[N], f[N], tt;

void dfs_visit(int u) {
  int v;
  color[u] = GRAY;
  d[u] = ++tt;
  for (v = 0; v < n; v++) {
    if (M[u][v] == 0)
      continue;
    if (color[v] == WHITE) {
      dfs_visit(v);
    }
  }
  color[u] = BLACK;
  f[u] = ++tt;
}

void dfs() {
  int u;
  for (u = 0; u < n; u++)
    color[u] = WHITE;
  tt = 0;

  for (u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }

  for (u = 0; u < n; u++) {
    printf("%d %d %d\n", u + 1, d[u], f[u]);
  }
}

int main() {
  int u, k, v, i, j;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    u--;
    for (int j = 0; j < k; j++) {
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}
Problem p02238 (C++)

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

struct StSeq {
  int m_nBgn;
  int m_nEnd;

  StSeq() : m_nBgn(0), m_nEnd(0) {}
};

void fnInput(vector<vector<int>> &rvvnAdjcy, vector<StSeq> &rvoDFSrch) {
  int nMaxSiz;
  cin >> nMaxSiz;

  rvvnAdjcy.resize(nMaxSiz + 1, vector<int>(nMaxSiz + 1, 0));
  for (int i = 0; i < nMaxSiz; ++i) {
    int nx, nSecSiz;
    cin >> nx >> nSecSiz;
    for (int j = 0; j < nSecSiz; ++j) {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StSeq oSeq;
  rvoDFSrch.resize(nMaxSiz + 1, oSeq);
}

void fnDepthFirstSearch(const vector<vector<int>> &cnrvvnAdjcy,
                        vector<StSeq> &rvoDFSrch, int nx) {
  static int stnCnt = 0;

  if (rvoDFSrch[nx].m_nEnd)
    return;
  rvoDFSrch[nx].m_nBgn = ++stnCnt;

  for (int ny = 1; ny < cnrvvnAdjcy.size(); ++ny)
    if (cnrvvnAdjcy[nx][ny] == 1 && nx != ny)
      fnDepthFirstSearch(cnrvvnAdjcy, rvoDFSrch, ny);

  rvoDFSrch[nx].m_nEnd = ++stnCnt;
}

void fnResult(const vector<StSeq> &cnrvoDFSrch) {
  for (int i = 1; i < cnrvoDFSrch.size(); ++i)
    cout << i << " " << cnrvoDFSrch[i].m_nBgn << " " << cnrvoDFSrch[i].m_nEnd
         << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  vector<StSeq> voDFSrch;

  fnInput(vvnAdjcy, voDFSrch);
  for (int nx = 1; nx < voDFSrch.size(); ++nx)
    fnDepthFirstSearch(vvnAdjcy, voDFSrch, nx);
  fnResult(voDFSrch);

  return 0;
}

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

struct StSeq {
  int m_nBgn;
  int m_nEnd;

  StSeq() : m_nBgn(0), m_nEnd(0) {}
};

void fnInput(vector<vector<int>> &rvvnAdjcy, vector<StSeq> &rvoDFSrch) {
  int nMaxSiz;
  cin >> nMaxSiz;

  rvvnAdjcy.resize(nMaxSiz + 1, vector<int>(nMaxSiz + 1, 0));
  for (int i = 0; i < nMaxSiz; ++i) {
    int nx, nSecSiz;
    cin >> nx >> nSecSiz;
    for (int j = 0; j < nSecSiz; ++j) {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StSeq oSeq;
  rvoDFSrch.resize(nMaxSiz + 1, oSeq);
}

void fnDepthFirstSearch(const vector<vector<int>> &cnrvvnAdjcy,
                        vector<StSeq> &rvoDFSrch, int nx) {
  static int stnCnt = 0;

  if (rvoDFSrch[nx].m_nBgn)
    return;
  rvoDFSrch[nx].m_nBgn = ++stnCnt;

  for (int ny = 1; ny < cnrvvnAdjcy.size(); ++ny)
    if (cnrvvnAdjcy[nx][ny] == 1 && nx != ny)
      fnDepthFirstSearch(cnrvvnAdjcy, rvoDFSrch, ny);

  rvoDFSrch[nx].m_nEnd = ++stnCnt;
}

void fnResult(const vector<StSeq> &cnrvoDFSrch) {
  for (int i = 1; i < cnrvoDFSrch.size(); ++i)
    cout << i << " " << cnrvoDFSrch[i].m_nBgn << " " << cnrvoDFSrch[i].m_nEnd
         << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  vector<StSeq> voDFSrch;

  fnInput(vvnAdjcy, voDFSrch);
  for (int nx = 1; nx < voDFSrch.size(); ++nx)
    fnDepthFirstSearch(vvnAdjcy, voDFSrch, nx);
  fnResult(voDFSrch);

  return 0;
}
Problem p02238 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stack>
using namespace std;

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

// uに隣接するvを番号順に取得
int next(int u) {
  for (int v = 0; v < n;
       v++) { // 訪問した場所は探索しなくていいため、v+1にしている？
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

// スタックを用いた深さ優先探索
void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;

  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  // 初期化
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  // 未訪問のuを始点として深さ優先探索
  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main(int argc, char const *argv[]) {
  int u, k, v;

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--; // 0 origin
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}


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

static const int N = 100;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[N][N];
int color[N], d[N], f[N], tt;
int nt[N];

// uに隣接するvを番号順に取得
int next(int u) {
  for (int v = nt[u]; v < n;
       v++) { // 訪問した場所は探索しなくていいため、v+1にしている？
    nt[u] = v + 1;
    if (M[u][v])
      return v;
  }
  return -1;
}

// スタックを用いた深さ優先探索
void dfs_visit(int r) {
  for (int i = 0; i < n; i++)
    nt[i] = 0;

  stack<int> S;
  S.push(r);
  color[r] = GRAY;
  d[r] = ++tt;

  while (!S.empty()) {
    int u = S.top();
    int v = next(u);
    if (v != -1) {
      if (color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = ++tt;
        S.push(v);
      }
    } else {
      S.pop();
      color[u] = BLACK;
      f[u] = ++tt;
    }
  }
}

void dfs() {
  // 初期化
  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    nt[i] = 0;
  }
  tt = 0;

  // 未訪問のuを始点として深さ優先探索
  for (int u = 0; u < n; u++) {
    if (color[u] == WHITE)
      dfs_visit(u);
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << d[i] << " " << f[i] << endl;
  }
}

int main(int argc, char const *argv[]) {
  int u, k, v;

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      M[i][j] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--; // 0 origin
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  dfs();

  return 0;
}
Problem p02238 (C++)

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

using namespace std;

const int max_v = 100;
vector<vector<int>> adj_list;
int d[max_v] = {0};
int f[max_v] = {0};
int discovered[max_v] = {0};
int t = 1;

// Depth-first Search ( Stack implementation)
void DFS(int v) {
  stack<int> S;
  S.push(v);
  discovered[v] = 1;
  d[v] = t++;
  int i = 0;
  while (!S.empty()) {
    v = S.top();
    if (i < adj_list[v - 1].size()) {
      int u = adj_list[v - 1][i++];
      if (discovered[u] == 0) {
        discovered[u] = 1;
        d[u] = t++;
        S.push(u);
        i = 0;
      }
    } else {
      S.pop();
      f[v] = t++;
      i = 0;
    }
  }
}

int main(int argc, char **argv) {
  // Input
  int n;
  int u, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    int v;
    vector<int> adj;
    for (int j = 0; j < k; j++) {
      cin >> v;
      adj.push_back(v);
    }
    adj_list.push_back(adj);
  }

  // Depth-first Search
  for (int i = 1; i <= n; i++) {
    if (discovered[i] == 0)
      DFS(i);
  }

  // Output
  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }

  return 0;
}

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

using namespace std;

const int max_v = 101;
vector<vector<int>> adj_list;
int d[max_v] = {0};
int f[max_v] = {0};
int discovered[max_v] = {0};
int t = 1;

// Depth-first Search ( Stack implementation)
void DFS(int v) {
  stack<int> S;
  S.push(v);
  discovered[v] = 1;
  d[v] = t++;
  int i = 0;
  while (!S.empty()) {
    v = S.top();
    if (i < adj_list[v - 1].size()) {
      int u = adj_list[v - 1][i++];
      if (discovered[u] == 0) {
        discovered[u] = 1;
        d[u] = t++;
        S.push(u);
        i = 0;
      }
    } else {
      S.pop();
      f[v] = t++;
      i = 0;
    }
  }
}

int main(int argc, char **argv) {
  // Input
  int n;
  int u, k;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    int v;
    vector<int> adj;
    for (int j = 0; j < k; j++) {
      cin >> v;
      adj.push_back(v);
    }
    adj_list.push_back(adj);
  }

  // Depth-first Search
  for (int i = 1; i <= n; i++) {
    if (discovered[i] == 0)
      DFS(i);
  }

  // Output
  for (int i = 1; i <= n; i++) {
    cout << i << " " << d[i] << " " << f[i] << endl;
  }

  return 0;
}
Problem p02238 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
using namespace std;
const int MAX_N = 100;

int A[MAX_N + 1][MAX_N + 1];
pair<int, int> TIMESTAMPS[MAX_N + 1];
int N;

void solve() {
  int ts = 0;
  stack<int> S;

  for (int i = N; i > 0; i--) {
    if (TIMESTAMPS[i].first == 0)
      S.push(i);
  }

  while (!S.empty()) {
    int c = S.top();

    if (TIMESTAMPS[c].first == 0)
      TIMESTAMPS[c].first = ++ts;

    for (int i = N; i > 0; i--) {
      if (A[c][i] == 1 && TIMESTAMPS[i].second == 0)
        S.push(i);
    }

    if (S.top() == c) {
      if (TIMESTAMPS[c].second == 0)
        TIMESTAMPS[c].second = ++ts;
      S.pop();
    }
  }
}

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      A[i][j] = 0;
    }
    TIMESTAMPS[i] = make_pair(0, 0);
  }

  for (int i = 0; i < N; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      A[u][t] = 1;
    }
  }

  solve();

  for (int i = 1; i <= N; i++) {
    cout << i << ' ' << TIMESTAMPS[i].first << ' ' << TIMESTAMPS[i].second
         << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
using namespace std;
const int MAX_N = 100;

int A[MAX_N + 1][MAX_N + 1];
pair<int, int> TIMESTAMPS[MAX_N + 1];
int N;

void solve() {
  int ts = 0;
  stack<int> S;

  for (int i = N; i > 0; i--) {
    if (TIMESTAMPS[i].first == 0)
      S.push(i);
  }

  while (!S.empty()) {
    int c = S.top();

    if (TIMESTAMPS[c].first == 0)
      TIMESTAMPS[c].first = ++ts;

    for (int i = N; i > 0; i--) {
      if (A[c][i] == 1 && TIMESTAMPS[i].first == 0)
        S.push(i);
    }

    if (S.top() == c) {
      if (TIMESTAMPS[c].second == 0)
        TIMESTAMPS[c].second = ++ts;
      S.pop();
    }
  }
}

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      A[i][j] = 0;
    }
    TIMESTAMPS[i] = make_pair(0, 0);
  }

  for (int i = 0; i < N; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      A[u][t] = 1;
    }
  }

  solve();

  for (int i = 1; i <= N; i++) {
    cout << i << ' ' << TIMESTAMPS[i].first << ' ' << TIMESTAMPS[i].second
         << endl;
  }

  return 0;
}