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

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

typedef pair<int, int> P;
typedef vector<P> Vec;

int v, e;
Vec Edge[15];
int memo[1 << 16][15];
int solve(int bit, int now);

int main() {
  memset(memo, -1, sizeof(memo));
  int s, t, d;
  cin >> v >> e;
  for (int i = 0; i < e; i++) {
    cin >> s >> t >> d;
    Edge[s].push_back(P(t, d));
  }
  int ans = solve(0, 0);
  if (ans == (1 << 30))
    cout << "-1" << endl;
  else
    cout << ans << endl;
}

int solve(int bit, int now) {
  int cost = 1 << 30;
  if (memo[bit][now] != -1)
    return memo[bit][now];
  // ->goal
  if (bit == (1 << v) - 1)
    return 0;
  else if (bit & 1)
    return cost;
  // <-goal

  for (int i = 0; i < Edge[now].size(); i++) {
    if ((bit & 1 << Edge[now][i].first) == 0) {
      cost =
          min(cost, solve(bit | 1 << Edge[now][i].first, Edge[now][i].first) +
                        Edge[now][i].second);
    }
  }
  return memo[bit][now] = cost;
}

Error: TLE

Fixed submission:

// 巡回セールスマン
#include <bits/stdc++.h>
using namespace std;

typedef struct {
  int cost, to;
} Edge;

int v, e;
vector<Edge> G[16];
int ans = -1;
int starts;

void bitdp(int bit, int cnt, int now);

int main() {
  cin >> v >> e;
  for (int i = 0, a, b, c; i < e; i++) {
    cin >> a >> b >> c;
    G[a].push_back((Edge){c, b});
    //  G[b].push_back((Edge){c,a});
  }
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      int a = 0;
      starts = i;
      bitdp(0, G[i][j].cost, G[i][j].to);
      a = 0;
    }
    //    cout<<"OUOU"<<endl;
  }
  cout << ans << endl;
}

void bitdp(int bit, int cnt, int now) {
  int temp;
  int co = 0;
  if (bit & 1 << now)
    return;
  bit = bit | 1 << now;
  bitset<5> bs((long)bit);
  //  cout<<cnt<<" "<<now<<" " <<bs<<endl;

  // カウント
  for (int i = 0; i < v; i++) {
    if (bit & 1 << i)
      co++;
    else
      break;
  }
  if (co == v && now == starts) {
    // cout<<cnt<<endl;
    if (ans == -1)
      ans = cnt;
    else
      ans = min(ans, cnt);
    return;
  }

  // そのエッジからの道
  for (int i = 0; i < G[now].size(); i++) {
    temp = bit;
    bitdp(bit, cnt + G[now][i].cost, G[now][i].to);
    bit = temp;
  }
  return;
}
Problem p02323 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int to;
  int value;
};

struct Node {
  int value;
  vector<Edge> edge;
};

int v, e;
int s, t, d;
Node node[15];

int dp[15][1 << 16];

int solve(int pos, int stat) {

  if (dp[pos][stat] != -1)
    return dp[pos][stat];

  int cost = 1 << 24;

  if (stat == (1 << v) - 1)
    return 0;
  else if (stat & 1)
    return cost;

  for (int i = 0; i < node[pos].edge.size(); ++i) {
    if (!(stat & 1 << node[pos].edge[i].to)) {
      cost = min(cost,
                 solve(node[pos].edge[i].to, stat | 1 << node[pos].edge[i].to) +
                     node[pos].edge[i].value);
    }
  }

  return dp[pos][stat] = cost;
}

int main() {

  cin >> v >> e;

  for (int i = 0; i < e; ++i) {
    cin >> s >> t >> d;
    node[s].edge.push_back((Edge){t, d});
  }

  memset(dp, -1, sizeof(dp));

  int dst = solve(0, 0);

  if (dst == 1 << 24) {
    cout << -1 << endl;
  } else {
    cout << dst << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int to;
  int value;
};

struct Node {
  int value;
  vector<Edge> edge;
};

int v, e;
int s, t, d;
Node node[15];

int solve(int pos, int stat) {

  int cost = 1 << 24;

  if (stat == (1 << v) - 1)
    return 0;
  else if (stat & 1)
    return cost;

  for (int i = 0; i < node[pos].edge.size(); ++i) {
    if (!(stat & 1 << node[pos].edge[i].to)) {
      cost = min(cost,
                 solve(node[pos].edge[i].to, stat | 1 << node[pos].edge[i].to) +
                     node[pos].edge[i].value);
    }
  }

  return cost;
}

int main() {

  cin >> v >> e;

  for (int i = 0; i < e; ++i) {
    cin >> s >> t >> d;
    node[s].edge.push_back((Edge){t, d});
  }

  int dst = solve(0, 0);

  if (dst == 1 << 24) {
    cout << -1 << endl;
  } else {
    cout << dst << endl;
  }
}
Problem p02323 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 99999999
#define INF_M 2147483647
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int dist[15][15];
int dp[1 << 15][15];
int V, E;
int s, t, d;

int rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << V) - 1 && v == 0)
    return dp[S][v] = 0;

  int res = INF;
  for (int r = 0; r < V; r++) {
    if (!(S & 1 << r)) {
      res = min(res, rec(S | 1 << r, r) + dist[v][r]);
    }
  }
  return dp[S][v] = res;
}

int main() {
  cin >> V >> E;
  rep(i, 0, 20) rep(j, 0, 20) dist[i][j] = INF;

  rep(i, 0, E) {
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  memset(dp, -1, sizeof(dp));

  int ans = rec(0, 0);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 99999999
#define INF_M 2147483647
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

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

int dist[20][20];
int dp[1 << 20][20];
int V, E;
int s, t, d;

int rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << V) - 1 && v == 0)
    return dp[S][v] = 0;

  int res = INF;
  for (int r = 0; r < V; r++) {
    if (!(S & 1 << r)) {
      res = min(res, rec(S | 1 << r, r) + dist[v][r]);
    }
  }
  return dp[S][v] = res;
}

int main() {
  cin >> V >> E;
  rep(i, 0, 20) rep(j, 0, 20) dist[i][j] = INF;

  rep(i, 0, E) {
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  memset(dp, -1, sizeof(dp));

  int ans = rec(0, 0);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p02323 (C++) — buggy submission, verdict: Runtime Error

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

const int INF = 10000000;

int solve(vector<vector<int>> &dists) {
  const int v = dists.size();

  vector<vector<int>> dp(1 << v, vector<int>(v, INF));
  dp[1][0] = 0;

  const int all = (1 << v) - 1;

  for (std::size_t S = 1; S <= all; S++) {
    std::size_t tS = S;
    while (tS > 0) {
      int bs = tS & ~(tS - 1);
      int s = __builtin_ctzll(bs);
      for (int e = 0; e < v; e++) {
        if (dists[s][e] != INF && (~S & (1 << e)) != 0) {
          const int nS = S | (1 << e);
          if (nS == all) {
            dp[nS][e] = min(dp[nS][e], dp[S][s] + dists[s][e] + dists[e][0]);
          } else {
            dp[nS][e] = min(dp[nS][e], dp[S][s] + dists[s][e]);
          }
        }
      }
      tS -= bs;
    }
  }

  int ans = INF;
  for (int i = 0; i < v; i++) {
    ans = min(ans, dp[all][i]);
  }
  return ans == INF ? -1 : ans;
}

int main() {
  int v, e;
  cin >> v >> e;

  vector<vector<int>> dists(v, vector<int>(v, INF));
  for (int i = 0; i < e; i++) {
    int s, e, d;
    cin >> s >> e >> d;
    dists[s][e] = d;
  }

  cout << solve(dists) << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const int INF = 10000000;

int solve(vector<vector<int>> &dists) {
  const int v = dists.size();

  vector<vector<int>> dp(1 << v, vector<int>(v, INF));
  dp[1][0] = 0;

  const int all = (1 << v) - 1;

  for (int S = 1; S <= all; S++) {
    int tS = S;
    while (tS > 0) {
      int bs = tS - (tS & ~(tS - 1));
      int s = __builtin_ctzll(bs);
      for (int e = 0; e < v; e++) {
        if (dists[s][e] != INF && (~S & (1 << e)) != 0) {
          const int nS = S | (1 << e);
          if (nS == all) {
            dp[nS][e] = min(dp[nS][e], dp[S][s] + dists[s][e] + dists[e][0]);
          } else {
            dp[nS][e] = min(dp[nS][e], dp[S][s] + dists[s][e]);
          }
        }
      }
      tS -= bs;
    }
  }

  int ans = INF;
  for (int i = 0; i < v; i++) {
    ans = min(ans, dp[all][i]);
  }
  return ans == INF ? -1 : ans;
}

int main() {
  int v, e;
  cin >> v >> e;

  vector<vector<int>> dists(v, vector<int>(v, INF));
  for (int i = 0; i < e; i++) {
    int s, e, d;
    cin >> s >> e >> d;
    dists[s][e] = d;
  }

  cout << solve(dists) << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 20
#define INF 2000000001
int d[MAX_V][MAX_V];
int V;
int edge[20];

void init() {
  for (int i = 0; i < V; i++)
    for (int j = 0; j < V; j++)
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
}

void warshall_floyd() {
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        if (d[i][k] != INF && d[k][j] != INF)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int sum = INF;
int dp[1 << 20];
vector<int> v;

int rec(int s = 0) {
  if (dp[s] >= 0)
    return dp[s];
  if (s == (1 << v.size()) - 1)
    return 0;
  int res = INF;
  for (int i = 0; i < v.size(); i++) {
    if (s >> i & 1)
      continue;
    for (int j = 0; j < v.size(); j++) {
      if (s >> j & 1)
        continue;
      if (i == j)
        continue;
      res = min(res, rec((s | 1 << i) | 1 << j) + d[v[i]][v[j]]);
    }
  }
  return dp[s] = res;
}

int main() {
  int E, ans = 0;
  cin >> V >> E;
  init();
  for (int i = 0; i < E; i++) {
    int s, t, k;
    cin >> s >> t >> k;
    edge[s]++, edge[t]++;
    d[s][t] = min(k, d[s][t]);
    d[t][s] = d[s][t];
    ans += k;
  }
  warshall_floyd();
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < V; i++)
    if (edge[i] % 2)
      v.push_back(i);
  cout << rec() + ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 20
#define INF 2000000001
int d[MAX_V][MAX_V];
int V;
int edge[20];

void init() {
  for (int i = 0; i < V; i++)
    for (int j = 0; j < V; j++)
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
}

void warshall_floyd() {
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        if (d[i][k] != INF && d[k][j] != INF)
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int sum = INF;
int used[20];
vector<int> v;

void dfs(int x = 0, int s = 0) {
  if (x == v.size()) {
    sum = min(s, sum);
    return;
  }
  for (int i = 0; i < v.size(); i++) {
    if (used[i])
      continue;
    for (int j = 0; j < v.size(); j++) {
      if (used[j])
        continue;
      if (i == j)
        continue;
      used[i] = used[j] = 1;
      dfs(x + 2, s + d[v[i]][v[j]]);
      used[i] = used[j] = 0;
    }
  }
}

int main() {
  int E, ans = 0;
  cin >> V >> E;
  init();
  for (int i = 0; i < E; i++) {
    int s, t, k;
    cin >> s >> t >> k;
    edge[s]++, edge[t]++;
    d[s][t] = min(k, d[s][t]);
    d[t][s] = d[s][t];
    ans += k;
  }
  warshall_floyd();
  for (int i = 0; i < V; i++)
    if (edge[i] % 2)
      v.push_back(i);
  dfs();
  cout << sum + ans << endl;
  return 0;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int mat[1404][1404], dp[1404][1404];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> mat[i][j];
    }
  }
  int ans = 0;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (mat[i][j] == 0)
        dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
      else
        dp[i][j] = 0;
      if (dp[i][j] > ans)
        ans = dp[i][j];
    }
  }
  cout << ans * ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int G[1410][1410], dp[1410][1410];

int MaxZFX(int H, int W) {
  int maxWidth = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (G[i][j] == 1) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
        maxWidth = max(maxWidth, dp[i][j]);
      }
    }
  }
  return maxWidth;
}

int main(void) {
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> G[i][j];
    }
  }
  int maxWidth = MaxZFX(H, W);
  cout << maxWidth * maxWidth << endl;

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

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

int H;
int W;
int main() {

  vector<vector<int>> map;

  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    vector<int> line;
    for (int j = 0; j < W; j++) {
      int cleanState;
      cin >> cleanState;
      if (H == 1 && W == 1) {
        cout << (cleanState == 1 ? 0 : 1) << endl;
        return 0;
      } else if (H == 1 || W == 1) {
        if (cleanState == 1) {
          cout << 1 << endl;
          return 0;
        }
      }
      line.push_back(cleanState == 1 ? 0 : 1);
    }
    map.push_back(line);
  }
  if (H == 1 || W == 1) {
    cout << 0 << endl;
    return 0;
  }
  int max = 0;
  for (int i = 1; i < H; i++) {
    for (int j = 1; j < W; j++) {
      if (map[i][j] == 0) {
        continue;
      }
      int min = 1500;
      min = min < map[i - 1][j] ? min : map[i - 1][j];
      min = min < map[i][j - 1] ? min : map[i][j - 1];
      min = min < map[i - 1][j - 1] ? min : map[i - 1][j - 1];
      map[i][j] = min + 1;
      max = max < min + 1 ? min + 1 : max;
    }
  }
  cout << max * max << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int maxCreanTileChain(vector<vector<int>> map, int row, int column);
int maxSumLength(vector<vector<int>> map, int maxLength, int row, int column);
int H;
int W;
int main() {

  vector<vector<int>> map;

  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    vector<int> line;
    for (int j = 0; j < W; j++) {
      int cleanState;
      cin >> cleanState;
      line.push_back(cleanState);
    }
    map.push_back(line);
  }

  int maximamSizeLength = 0;

  for (int i = 0; i < H; i++) {
    if (i >= H - maximamSizeLength) {
      break;
    }

    for (int j = 0; j < W; j++) {
      if (map[i][j] == 1) {

      } else if (j > W - maximamSizeLength) {
        break;
      } else {
        int tempLength;
        tempLength = maxCreanTileChain(map, i, j);

        if (maximamSizeLength < tempLength) {
          int tempSumLength;
          tempSumLength = maxSumLength(map, tempLength, i, j);
          if (maximamSizeLength < tempSumLength) {
            maximamSizeLength = tempSumLength;
          }
        }
      }
    }
  }

  cout << maximamSizeLength * maximamSizeLength << endl;
}

int maxCreanTileChain(vector<vector<int>> map, int row, int column) {
  int count = 0;
  while (1) {
    if (column + count == W) {
      break;
    }
    if (map[row][column + count] == 0) {
      count++;
    } else {
      break;
    }
  }
  return count;
}

int maxSumLength(vector<vector<int>> map, int maxLength, int row, int column) {

  if (maxLength == 1) {
    return 1;
  }

  bool CrearFlag = true;
  for (int count = 1; count <= maxLength; count++) {

    for (int i = 0; i < count; i++) {
      for (int j = 0; j < count; j++) {
        if (map[row + i][column + j] == 1) {
          CrearFlag = false;
        }
      }
    }
    if (CrearFlag == false) {
      return count - 1;
    } else if (row + count >= H) {
      return count;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 1400 + 10;

int row, column;
int maze[MAXN][MAXN], pre[MAXN][MAXN], dp[MAXN][MAXN];
int ans;

bool isVaild(int r0, int c0, int r1, int c1) {
  return !(pre[r1][c1] - pre[r0 - 1][c1] - pre[r1][c0 - 1] +
           pre[r0 - 1][c0 - 1]);
}

int main(void) {
  scanf("%d%d", &row, &column);
  for (int r = 1; r <= row; r++)
    for (int c = 1; c <= column; c++)
      scanf("%d", &maze[r][c]), pre[r][c] = pre[r][c - 1] + pre[r - 1][c] +
                                            maze[r][c] - pre[r - 1][c - 1];
  for (int r = 1; r <= row; r++)
    for (int c = 1; c <= column; c++)
      for (int i = sqrt(ans); i < min(r, c); i++) {
        if (!isVaild(r - i, c - i, r, c))
          break;
        ans = max(ans, dp[r][c] = (i + 1) * (i + 1));
        // printf("dp[%d][%d] = %d\\n", r, c, dp[r][c]);
      }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

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

const int N(1400 + 10);

int g[N][N];
int dp[N][N];

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

int main() {
  int h, w;
  cin >> h >> w;
  int i, j;
  f(i, h) f(j, w) cin >> g[i][j];
  int mm = 0;
  f(i, h) f(j, w) {
    dp[i][j] = (g[i][j] + 1) % 2;
    mm = max(dp[i][j], mm);
  }
  f(i, h) f(j, w) if (!g[i][j]) {
    dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
    mm = max(dp[i][j], mm);
  }
  cout << mm * mm << endl;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

//?????¢??????????????????
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int H, W;
  cin >> H >> W;

  int DP[2][W];
  int Max = 0;
  int tmp;

  for (int j = 0; j < W; j++) {
    cin >> tmp;
    DP[0][j] = 1 - tmp;
    Max = max(Max, DP[0][j]);
  }

  int row = 1;

  for (int i = 1; i < H; i++) {
    cin >> tmp;
    DP[row][0] = 1 - tmp;
    Max = max(Max, DP[row][0]);
    for (int j = 1; j < W; j++) {
      cin >> tmp;
      if (tmp == 0) {
        DP[row][j] =
            min(min(DP[1 - row][j], DP[row][j - 1]), DP[1 - row][j - 1]) + 1;
        Max = max(Max, DP[row][j]);
      } else {
        DP[row][j] = 0;
      }
    }
    row = 1 - row;
  }

  cout << Max * Max << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int H, W;
  cin >> H >> W;
  int T[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> T[i][j];
    }
  }

  int DP[H][W];
  int Max = 0;

  for (int i = 0; i < H; i++) {
    DP[i][0] = 1 - T[i][0];
    Max = max(Max, DP[i][0]);
  }
  for (int j = 0; j < W; j++) {
    DP[0][j] = 1 - T[0][j];
    Max = max(Max, DP[0][j]);
  }

  for (int i = 1; i < H; i++) {
    for (int j = 1; j < W; j++) {
      if (T[i][j] == 0) {
        DP[i][j] = min(min(DP[i - 1][j], DP[i][j - 1]), DP[i - 1][j - 1]) + 1;
        Max = max(Max, DP[i][j]);
      } else {
        DP[i][j] = 0;
      }
    }
  }

  cout << Max * Max << endl;
  return 0;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 1414;
int n, m, answer = 0, matrix[N][N], dp[N][N];
int isval(int i, int j, int len) {
  if (len == 0) {
    return 1;
  }
  int z = len;
  for (int q = 1; q <= len; q++) {
    if (matrix[i - q][j] == 1 || matrix[i][j - q] == 1) {
      z = q - 1;
      break;
    }
  }
  return z + 1;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> matrix[j][i];
      if (matrix[j][i] == 0) {
        dp[j][i] = 1;
        answer = 1;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (matrix[j][i] == 0) {
        dp[j][i] = isval(j, i, dp[j - 1][i - 1]);
      }
      answer = max(answer, dp[j][i]);
    }
  }
  answer *= answer;
  cout << answer << endl;
  /*cerr << "*********************************" << endl ;
  for (int i = 0 ; i < n ; i ++)
  {
          for (int j = 0 ; j < m ; j ++)
          {
                  cerr << dp[j][i] << " " ;
          }
          cerr << endl ;
  }*/
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define MAX 1400
using namespace std;
int dp[MAX][MAX], G[MAX][MAX];
int getLargestSquare(int H, int W) {
  int maxWidth = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dp[i][j] = (G[i][j] + 1) % 2;
      maxWidth = dp[i][j];
    }
  }
  for (int i = 1; i < H; i++) {
    for (int j = 1; i < W; j++) {
      if (G[i][j]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1]));
        maxWidth = max(maxWidth, dp[i][j]);
      }
    }
  }
  return maxWidth * maxWidth;
}
int main() {
  int H, W;
  scanf("%d %d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &G[i][j]);
    }
  }
  printf("%d\\n", getLargestSquare(H, W));
  return 0;
}
Problem p02326 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define HMAX 1400
#define WMAX 1400
using namespace std;
int main() {
  int H, W, ms = 0;
  int dp[HMAX + 10][WMAX + 10] = {};
  cin >> H >> W;
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      cin >> dp[i][j];
      if (dp[i][j] == 0) {
        dp[i][j] = 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      if (dp[i][j] != 0) {
        int m = min(dp[i - 1][j - 1], dp[i - 1][j]);
        dp[i][j] += min(m, dp[i][j - 1]);
        ms = max(ms, dp[i][j]);
      }
    }
  }
  cout << ms * ms << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define HMAX 140
#define WMAX 140
using namespace std;
int main() {
  int H, W, ms = 0;
  int dp[HMAX + 10][WMAX + 10] = {};
  cin >> H >> W;
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      cin >> dp[i][j];
      if (dp[i][j] == 0) {
        dp[i][j] = 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      if (dp[i][j] != 0) {
        int m = min(dp[i - 1][j - 1], dp[i - 1][j]);
        dp[i][j] += min(m, dp[i][j - 1]);
        ms = max(ms, dp[i][j]);
      }
    }
  }
  cout << ms * ms << "\\n";
  return 0;
}