Problem p01567 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define BEGIN_STACK_EXTEND(size)                                               \\
  void *stack_extend_memory_ = malloc(size);                                   \\
  void *stack_extend_origin_memory_;                                           \\
  char *stack_extend_dummy_memory_ = (char *)alloca(                           \\
      (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16);              \\
  *stack_extend_dummy_memory_ = 0;                                             \\
  asm volatile("mov %%rsp, %%rbx\\nmov %%rax, %%rsp"                            \\
               : "=b"(stack_extend_origin_memory_)                             \\
               : "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_));         \\
  free(stack_extend_memory_);
using namespace std;
int N;
vector<int> G[200001];
int par[200001], dep[200001], num[200001];
void makegraph() {
  string s;
  cin >> s;
  int v = -1;
  for (char c : s) {
    if (c == '(') {
      if (v >= 0)
        G[v].pb(N), dep[N] = dep[v] + 1;
      par[N] = v;
      v = N;
      N++;
    } else {
      num[v] = 1;
      for (int u : G[v])
        num[v] += num[u];
      v = par[v];
    }
  }
}
bool dfs(int v, int w, int s) {
  if (G[v].size() == 0 && G[w].size() == 2)
    return false;
  if (G[v].size() == 0 && G[w].size() == 0)
    return true;
  if (G[v].size() == 2 && G[w].size() == 2)
    return dfs(G[v][0], G[w][0], s) && dfs(G[v][1], G[w][1], s);
  return dfs(v, s, s);
}
bool ok(int v) { return dfs(0, v, v); }
int main() {
  BEGIN_STACK_EXTEND(128 * 1024 * 1024);
  makegraph();
  vector<int> ds;
  int v = 0;
  rep(i, N) if (dep[i] > dep[v]) v = i;
  v = par[v];
  while (v >= 0) {
    if ((N - 1) % (num[v] - 1) == 0) {
      if (ok(v))
        ds.pb(num[v] - 1);
    }
    v = par[v];
  }
  int K = ds.size();
  int dp[161] = {};
  rep(i, K) dp[i] = 1e9;
  dp[0] = 0;
  rep(i, K) {
    rep(j, i) {
      if (ds[i] % ds[j] == 0) {
        chmin(dp[i], dp[j] + ds[i] / ds[j] - 1);
      }
    }
  }
  cout << dp[K - 1] << endl;
  END_STACK_EXTEND;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define BEGIN_STACK_EXTEND(size)                                               \\
  void *sem = malloc(size);                                                    \\
  void *seom;                                                                  \\
  char *sedm = (char *)alloca((1 + (int)(((long long)sem) & 127)) * 16);       \\
  *sedm = 0;                                                                   \\
  asm volatile("mov %%esp, %%ebx\\nmov %%eax, %%esp"                            \\
               : "=b"(seom)                                                    \\
               : "a"((char *)sem + (size)-1024));
#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%eax, %%esp" ::"a"(seom));                                \\
  free(sem);
using namespace std;
int N;
vector<int> G[200001];
int par[200001], dep[200001], num[200001];
void makegraph() {
  string s;
  cin >> s;
  int v = -1;
  for (char c : s) {
    if (c == '(') {
      if (v >= 0)
        G[v].pb(N), dep[N] = dep[v] + 1;
      par[N] = v;
      v = N;
      N++;
    } else {
      num[v] = 1;
      for (int u : G[v])
        num[v] += num[u];
      v = par[v];
    }
  }
}
bool dfs(int v, int w, int s) {
  if (G[v].size() == 0 && G[w].size() == 2)
    return false;
  if (G[v].size() == 0 && G[w].size() == 0)
    return true;
  if (G[v].size() == 2 && G[w].size() == 2)
    return dfs(G[v][0], G[w][0], s) && dfs(G[v][1], G[w][1], s);
  return dfs(v, s, s);
}
bool ok(int v) { return dfs(0, v, v); }
int main() {
  BEGIN_STACK_EXTEND(1024 * 1024);
  makegraph();
  vector<int> ds;
  int v = 0;
  rep(i, N) if (dep[i] > dep[v]) v = i;
  v = par[v];
  while (v >= 0) {
    if ((N - 1) % (num[v] - 1) == 0) {
      if (ok(v))
        ds.pb(num[v] - 1);
    }
    v = par[v];
  }
  int K = ds.size();
  int dp[161] = {};
  rep(i, K) dp[i] = 1e9;
  dp[0] = 0;
  rep(i, K) {
    rep(j, i) {
      if (ds[i] % ds[j] == 0) {
        chmin(dp[i], dp[j] + ds[i] / ds[j] - 1);
      }
    }
  }
  cout << dp[K - 1] << endl;
  END_STACK_EXTEND;
}