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

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

int dp[11][100001];
int n, d, x;
int p[10][10];

int main() {
  cin >> n >> d >> x;
  rep(i, d) rep(j, n) cin >> p[i][j];
  rep(i, d) rep(j, 100001) dp[i][j] = j;
  for (int i = d - 2; i >= 0; i--) {
    for (int j = 1; j <= 100000; j++) {
      rep(k, n) {
        if (p[i][k] > j || p[i][k] >= p[i + 1][k])
          continue;
        dp[i][j] = max(dp[i][j], p[i + 1][k] + dp[i][j - p[i][k]]);
      }
    }
  }
  int ans = x;
  for (int i = 0; i <= d - 2; i++)
    ans = dp[i][ans];
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

map<int, int> m;
map<int, int>::iterator it;
int n, d, x;
int p[10][10];
int pow6[10];

void init() {
  int A = 1;
  rep(i, 10) {
    pow6[i] = A;
    A *= 6;
  }
}

int main() {
  init();
  cin >> n >> d >> x;
  rep(i, d) rep(j, n) cin >> p[i][j];
  m[0] = x;
  rep(i, d) {
    while (true) {
      bool end = true;
      for (it = m.begin(); it != m.end(); it++) {
        int x = it->first, y = it->second;
        rep(j, n) {
          int Y = y, X = x;
          while (Y >= p[i][j]) {
            if (m[X + pow6[j]] < Y - p[i][j]) {
              end = false;
              m[X + pow6[j]] = Y - p[i][j];
            }
            Y -= p[i][j];
            X += pow6[j];
          }
          Y = y;
          X = x;
          while ((X / pow6[j]) % 6 != 0) {
            if (m[X - pow6[j]] < Y + p[i][j]) {
              m[X - pow6[j]] = Y + p[i][j];
              end = false;
            }
            X -= pow6[j];
            Y += p[i][j];
          }
        }
      }
      if (end)
        break;
    }
  }
  cout << m[0] << endl;
}
Problem p01719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef pair<int, int> Pii;

int main() {
  int n, d, x;
  cin >> n >> d >> x;
  int p[d][n];
  rep(d) repp(j, n) cin >> p[i][j];

  map<int, int> dp;
  int diff = INT_MIN;
  rep(d + 1) {
    if (diff > 0)
      x += diff;

    if (i == d - 1)
      break;
    dp.clear();
    dp[0] = 0;
    diff = INT_MIN;

    repp(j, n) {
      for (auto itr = dp.begin(); itr != dp.end(); ++itr) {
        int pp = itr->first;
        dp[pp] = max(dp[pp], dp[pp]);
        if (pp + p[i][j] > x)
          continue;
        dp[pp + p[i][j]] = max(dp[pp], dp[pp] + (p[i + 1][j] - p[i][j]));
        diff = max({diff, dp[pp], dp[pp + p[i][j]]});
      }
    }
  }
  cout << x << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef pair<int, int> Pii;

int main() {
  int n, d, x;
  cin >> n >> d >> x;
  int p[d][n];
  rep(d) repp(j, n) cin >> p[i][j];

  vector<int> stocks(n, 0);
  rep(d) {
    repp(j, n) {
      cerr << stocks[j];
      if (stocks[j]) {
        x += p[i][j] * stocks[j];
        stocks[j] = 0;
      }
    }
    if (i == d - 1)
      break;

    priority_queue<Pii> que;
    repp(j, n) que.push(Pii(p[i + 1][j] - p[i][j], j));
    while (!que.empty()) {
      Pii pa = que.top();
      que.pop();
      if (pa.first <= 0)
        break;
      if (x < p[i][pa.second])
        continue;
      stocks[pa.second] = x / p[i][pa.second];
      x -= p[i][pa.second] * stocks[pa.second];
    }
  }
  cout << x << endl;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#undef max
#undef min

int dp[11][100001] = {0};
int knapsack(int maxWeight, std::vector<int> cost, std::vector<int> benefit) {
  if (cost.size() != benefit.size())
    return 0;
  std::memset(dp, 0, sizeof(dp));
  int maxBenefit = maxWeight;
  for (int i = 0; i <= 100000; i++) {
    dp[0][i] = i;
  }
  for (int i = 0; i < cost.size(); i++) {
    for (int j = 0; j <= maxWeight; j++) {
      if (cost[i] > j)
        dp[i + 1][j] = dp[i][j];
      else {
        dp[i + 1][j] = std::max(dp[i][j], dp[i + 1][j - cost[i]] + benefit[i]);
        maxBenefit = std::max(maxBenefit, dp[i + 1][j]);
      }
    }
  }
  return maxBenefit;
}

int main() {
  int n, d, x;
  std::vector<std::vector<int>> p;

  std::cin >> n >> d >> x;
  for (int i = 0; i < d; i++) {
    p.push_back(std::vector<int>());
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      p.back().push_back(a);
    }
  }

  for (int i = 0; i < d - 1; i++) {
    x = knapsack(x, p[i], p[i + 1]);
  }
  std::cout << x << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#undef max
#undef min

int dp[11][10001] = {0};
int knapsack(int maxWeight, std::vector<int> cost, std::vector<int> benefit) {
  if (cost.size() != benefit.size())
    return 0;
  std::memset(dp, 0, sizeof(dp));
  int maxBenefit = maxWeight;
  for (int i = 0; i <= 10000; i++) {
    dp[0][i] = i;
  }
  for (int i = 0; i < cost.size(); i++) {
    for (int j = 0; j <= maxWeight; j++) {
      if (cost[i] > j)
        dp[i + 1][j] = dp[i][j];
      else {
        dp[i + 1][j] = std::max(dp[i][j], dp[i + 1][j - cost[i]] + benefit[i]);
        maxBenefit = std::max(maxBenefit, dp[i + 1][j]);
      }
    }
  }
  return maxBenefit;
}

int main() {
  int n, d, x;
  std::vector<std::vector<int>> p;

  std::cin >> n >> d >> x;
  for (int i = 0; i < d; i++) {
    p.push_back(std::vector<int>());
    for (int j = 0; j < n; j++) {
      int a;
      std::cin >> a;
      p.back().push_back(a);
    }
  }

  for (int i = 0; i < d - 1; i++) {
    x = knapsack(x, p[i], p[i + 1]);
  }
  std::cout << x << std::endl;
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
int dp[210000];
int d[11][11];
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  for (int i = 0; i < b; i++)
    for (int j = 0; j < a; j++)
      scanf("%d", &d[i][j]);

  int now = c;
  for (int i = 0; i < b - 1; i++) {
    for (int j = 0; j < 210000; j++)
      dp[j] = -99999999;
    dp[0] = 0;
    int to = now;

    for (int k = 0; k < a; k++) {
      for (int j = 0; j <= now; j++) {
        dp[j + d[i][k]] = max(dp[j + d[i][k]], dp[j] + d[i + 1][k]);
      }
    }
    for (int j = 0; j <= now; j++)
      to = max(to, dp[j] + now - j);
    now = to;
  }
  printf("%d\\n", now);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int dp[210000];
int d[11][11];
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  for (int i = 0; i < b; i++)
    for (int j = 0; j < a; j++)
      scanf("%d", &d[i][j]);
  for (int i = 0; i < b; i++)
    for (int j = 0; j < a; j++) {
      if (!d[i][j])
        return 1;
    }
  int now = c;
  for (int i = 0; i < b - 1; i++) {
    for (int j = 0; j < 110000; j++)
      dp[j] = -99999999;
    dp[0] = 0;
    int to = now;
    for (int j = 0; j <= now; j++) {
      to = max(to, dp[j] + now - j);
      for (int k = 0; k < a; k++) {

        dp[j + d[i][k]] = max(dp[j + d[i][k]], dp[j] + d[i + 1][k]);
      }
    }
    now = to;
  }
  printf("%d\\n", now);
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, d, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I; i < n; i++)
    if (p[day][i] / p[day - 1][i] && x / p[day - 1][i]) {
      f = 0;
      saiki(sum + p[day][i], x - p[day - 1][i], day, i);
    }
  if (f)
    ans = max(ans, sum + x);
}

int main() {
  cin >> n >> d >> ans;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, 0);
  cout << ans << endl;
  return 0;
}

Error: -8

Fixed submission:

#include <iostream>
using namespace std;
int n, d, p[10][10], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I; i < n; i++)
    if (p[day][i] / p[day - 1][i] && x / p[day - 1][i]) {
      f = 0;
      saiki(sum + p[day][i], x - p[day - 1][i], day, i);
    }
  if (f)
    ans = max(ans, sum + x);
}

int main() {
  cin >> n >> d >> ans;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

int dp[100001];

int getMaxProfit(int money, int n, int today[], int next[]) {
  if (dp[money] >= 0)
    return dp[money];

  int profit, max_profit = -1;
  for (int i = 0; i < n; i++) {
    if (money < today[i])
      continue;
    profit =
        (next[i] - today[i]) + getMaxProfit(money - today[i], n, today, next);
    if (max_profit < profit)
      max_profit = profit;
  }
  if (max_profit == -1)
    max_profit = 0;
  dp[money] = max_profit;
  return max_profit;
}

int main(void) {
  int n, d, x;
  int p[10][10];

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

  int money = x;
  for (int i = 0; i < d - 1; i++) {
    for (int j = 0; j < 100001; j++)
      dp[j] = -1;
    money += getMaxProfit(money, n, p[i], p[i + 1]);
  }

  printf("%d\\n", money);
  return 0;
}

Error: 0

Fixed submission:

#include <math.h>
#include <stdio.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

int dp[10001];

int getMaxProfit(int money, int n, int today[], int next[]) {
  if (dp[money] >= 0)
    return dp[money];

  int profit, max_profit = -1;
  for (int i = 0; i < n; i++) {
    if (money < today[i])
      continue;
    profit =
        (next[i] - today[i]) + getMaxProfit(money - today[i], n, today, next);
    if (max_profit < profit)
      max_profit = profit;
  }
  if (max_profit == -1)
    max_profit = 0;
  dp[money] = max_profit;
  return max_profit;
}

int main(void) {
  int n, d, x;
  int p[10][10];

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

  int money = x;
  for (int i = 0; i < d - 1; i++) {
    for (int j = 0; j < 10001; j++)
      dp[j] = -1;
    money += getMaxProfit(money, n, p[i], p[i + 1]);
  }

  printf("%d\\n", money);
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n, p[10][10], dp[10][100010], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I + 1; i < n; i++)
    if (p[day][i] > p[day - 1][i])
      for (int j = 1; x / (p[day - 1][i] * j); j++)
        f = 0, saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i);

  if (f)
    ans = max(ans, sum + x);
}

int main() {
  int d, x;
  cin >> n >> d >> x;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  dp[0][x] = 1;
  ans = x;
  for (int i = 0; i < d - 1; i++)
    saiki(0, ans, i + 1, -1);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, p[10][10], dp[10][100010], ans;

void saiki(int sum, int x, int day, int I) {
  int f = 1;
  for (int i = I + 1; i < n; i++)
    if (p[day][i] > p[day - 1][i])
      for (int j = 1; x / (p[day - 1][i] * j); j++)
        f = 0, saiki(sum + p[day][i] * j, x - p[day - 1][i] * j, day, i);

  if (f) {
    dp[day][sum + x] = 1;
    ans = max(ans, sum + x);
  }
}

int main() {
  int d, x;
  cin >> n >> d >> x;
  for (int i = 0; i < d; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  dp[0][x] = 1;
  ans = x;
  for (int i = 0; i < d - 1; i++)
    for (int j = 0; j < 100010; j++) {
      if (dp[i][j])
        saiki(0, j, i + 1, -1);
    }
  cout << ans << endl;
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, d, x;
  int p[10][10];
  int dp[100001];

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      p[i][j] = 0;
    }
  }

  for (int i = 0; i < 100001; i++) {
    dp[i] = 0;
  }

  cin >> n >> d >> x;

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

  for (int day = 0; day < d - 1; day++) {
    for (int i = 0; i <= x; i++) {
      dp[i] = i;
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= x; j++) {
        if (j - p[day][i] >= 0) {
          dp[j] = max(dp[j], dp[j - p[day][i]] + p[day + 1][i]);
        }
      }
    }

    int nx = 0;
    for (int i = 0; i <= x; i++) {
      nx = max(nx, dp[i]);
    }

    x = nx;
  }

  cout << x << endl;

  return 0;
}

Error: -11

Fixed submission:

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

int n, d, x;
int p[10][10];
int dp[10][100001];

int main() {
  cin >> n >> d >> x;

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

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 100001; j++) {
      dp[i][j] = 0;
    }
  }

  for (int day = 0; day < d - 1; day++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= x; j++) {
        if (j - p[day][i] >= 0) {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - p[day][i]] + p[day + 1][i]);
        }
      }
    }
  }

  cout << dp[d - 1][x] << endl;
  ;

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

#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int main() {
  int N, D, X;
  cin >> N >> D >> X;
  int p[D][N];
  for (int i = 0; i < D; ++i)
    for (int j = 0; j < N; ++j)
      cin >> p[i][j];
  int val;
  for (int d = 1; d < D; ++d) {
    val = 0;
    int dp[N + 1][X + 1];
    for (int i = 0; i <= N; ++i)
      fill(dp[i], dp[i] + X + 1, 0);

    for (int i = 1; i <= N; ++i) {
      for (int j = 0; j <= X; ++j) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j]);
        if (j >= p[d - 1][i - 1])
          dp[i][j] = max(dp[i][j], dp[i][j - p[d - 1][i - 1]] + p[d][i - 1]);
      }
    }

    for (int i = 0; i <= X; ++i) {
      val = max(val, dp[N][i] + X - i);
    }

    X = val;
  }
  cout << val << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int main() {
  int N, D, X;
  cin >> N >> D >> X;
  int p[D][N];
  for (int i = 0; i < D; ++i)
    for (int j = 0; j < N; ++j)
      cin >> p[i][j];
  map<vector<int>, int> M;
  vector<int> init(N + 1, 0);
  M[init] = X + 1;
  stack<vector<int>> st;
  // priority_queue< vector<int> > st;
  st.push(init);
  while (!st.empty()) {
    vector<int> t = st.top();
    // vector<int> t = st.front();
    st.pop();

    int d = t[0], cur = M[t];

    for (int i = 0; i < N; ++i) {
      if (d > 0 && t[i + 1]) {
        --t[i + 1];
        if (cur + p[d][i] > M[t]) { //???????£????
          M[t] = cur + p[d][i];
          st.push(t);
        }
        ++t[i + 1];
      }
      if (d < D - 1 && cur > p[d][i] && p[d][i] < p[d + 1][i]) { //????????????
        ++t[i + 1];
        if (M[t] < cur - p[d][i]) {
          M[t] = cur - p[d][i];
          st.push(t);
        }
        --t[i + 1];
      }
    }
    if (d < D - 1) { //???????????????
      ++t[0];
      if (M[t] < cur) {
        st.push(t);
        M[t] = cur;
      }
      --t[0];
    }
  }
  init[0] = (D - 1);
  cout << M[init] - 1 << endl;
  return 0;
}
Problem p01719 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n, d, x;
  cin >> n >> d >> x;
  vector<int> p(n);
  rep(i, n) cin >> p[i];

  rep(azu, d - 1) {
    vector<int> q(n);
    rep(i, n) cin >> q[i];
    vector<int> dp(x + 1, 0);

    rep(i, n) {
      rep(j, x - p[i] + 1) { dp[j + p[i]] = max(dp[j + p[i]], dp[j] + q[i]); }
    }

    int maxv = x;
    rep(i, x + 1) maxv = max(maxv, dp[i] + x - i);
    x = maxv;
    p = q;
  }
  cout << x << endl;
}

Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

int main() {
  int n, d, x;
  cin >> n >> d >> x;
  vector<int> p(n);
  rep(i, n) {
    cin >> p[i];
    assert(p[i] > 0);
  }

  rep(azu, d - 1) {
    vector<int> q(n);
    rep(i, n) {
      cin >> q[i];
      assert(q[i] > 0);
    }
    vector<int> dp(x + 1, 0);

    rep(i, n) {
      rep(j, x - p[i] + 1) { dp[j + p[i]] = max(dp[j + p[i]], dp[j] + q[i]); }
    }

    int maxv = x;
    rep(i, x) maxv = max(maxv, dp[i + 1] + x - i - 1);
    x = maxv;
    p = q;
  }
  cout << x << endl;
}
Problem p01719 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

#define MAX_N 20
#define MAX_M 6000000
#define MAX_P 300000

int d[MAX_N][MAX_N];
int dp[MAX_M];
int dp2[MAX_M];
int DP[MAX_N];
int main() {
  int n, m, p;
  cin >> m >> n >> p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> d[i][j];
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    DP[i] = 0;
  }
  DP[0] = p;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < MAX_M; j++) {
      dp[j] = 0;
      dp2[j] = 0;
    }
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < MAX_M - MAX_P; k++) {
        dp[k + d[i][j]] = max(dp[k + d[i][j]], dp[k] + d[i + 1][j]);
      }
    }
    for (int j = 1; j < MAX_M; j++) {
      dp2[j] = max(dp2[j], max(dp[j] - j, dp2[j - 1]));
    }
    DP[i + 1] = DP[i] + dp2[DP[i]];
  }
  cout << DP[n - 1] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX_N 20
#define MAX_M 20000000
#define MAX_P 300000

int d[MAX_N][MAX_N];
int dp[MAX_M];
int dp2[MAX_M];
int DP[MAX_N];
int main() {
  int n, m, p;
  cin >> m >> n >> p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> d[i][j];
    }
  }
  for (int i = 0; i < MAX_N; i++) {
    DP[i] = 0;
  }
  DP[0] = p;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < MAX_M; j++) {
      dp[j] = 0;
      dp2[j] = 0;
    }
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < MAX_M - MAX_P; k++) {
        dp[k + d[i][j]] = max(dp[k + d[i][j]], dp[k] + d[i + 1][j]);
      }
    }
    for (int j = 1; j < MAX_M; j++) {
      dp2[j] = max(dp2[j], max(dp[j] - j, dp2[j - 1]));
    }
    DP[i + 1] = DP[i] + dp2[DP[i]];
  }
  cout << DP[n - 1] << endl;
  return 0;
}
Problem p01720 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

int N, M, S, T;

int shortest_path(vector<vector<int>> G) {
  vector<int> d(N, -1);

  queue<int> que;
  que.push(S);
  d[S] = 0;

  while (!que.empty()) {
    auto u = que.front();
    que.pop();
    for (auto v : G[u]) {
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        if (v == T)
          return d[v];
        que.push(v);
      }
    }
  }
  return -1;
}

vector<ll> bfs(vector<vector<int>> G, int s) {
  vector<int> d(N, -1);

  queue<int> que;
  que.push(s);
  d[s] = 0;

  while (!que.empty()) {
    auto u = que.front();
    que.pop();
    for (auto v : G[u]) {
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        que.push(v);
      }
    }
  }

  vector<ll> res(N);
  for (int i = 0; i < N; i++)
    if (d[i] >= 0)
      res[d[i]]++;
  return res;
}

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

  cin >> N >> M >> S >> T;
  S--;
  T--;

  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  int sp = shortest_path(G);
  auto st = bfs(G, S);
  auto tt = bfs(G, T);

  ll ans = 0;
  for (int i = 0; i <= sp - 2; i++)
    ans += st[i] * tt[sp - 2 - i];

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

Error: 0

Fixed submission:

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

typedef long long ll;
const int INF = 0x3f3f3f3f;

int N, M, S, T;

int shortest_path(vector<vector<int>> G) {
  vector<int> d(N, -1);

  queue<int> que;
  que.push(S);
  d[S] = 0;

  while (!que.empty()) {
    auto u = que.front();
    que.pop();
    for (auto v : G[u]) {
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        if (v == T)
          return d[v];
        que.push(v);
      }
    }
  }
  return -1;
}

vector<ll> bfs(vector<vector<int>> G, int s) {
  vector<int> d(N, -1);

  queue<int> que;
  que.push(s);
  d[s] = 0;

  while (!que.empty()) {
    auto u = que.front();
    que.pop();
    for (auto v : G[u]) {
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        que.push(v);
      }
    }
  }

  vector<ll> res(N);
  for (int i = 0; i < N; i++)
    res[d[i]]++;
  return res;
}

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

  cin >> N >> M >> S >> T;
  S--;
  T--;

  vector<vector<int>> G(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  int sp = shortest_path(G);
  auto st = bfs(G, S);
  auto tt = bfs(G, T);

  ll ans = 0;
  for (int i = 0; i <= sp - 2; i++)
    ans += st[i] * tt[sp - 2 - i];

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