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

// ALDS1_7_C.cpp
// Tree - Tree Walk

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

struct Node {
  int id;
  int lchild;
  int rchild;
  int parent = -1;
};

void Preorder(vector<Node> &nodes, int id) {
  cout << " " << nodes[id].id;
  if (nodes[id].lchild != -1)
    Preorder(nodes, nodes[id].lchild);
  if (nodes[id].rchild != -1)
    Preorder(nodes, nodes[id].rchild);
}

void Inorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Inorder(nodes, nodes[id].lchild);
  cout << " " << nodes[id].id;
  if (nodes[id].rchild != -1)
    Inorder(nodes, nodes[id].rchild);
}

void Postorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Postorder(nodes, nodes[id].lchild);
  if (nodes[id].rchild != -1)
    Postorder(nodes, nodes[id].rchild);
  cout << " " << nodes[id].id;
}

int main() {
  int n;
  cin >> n;
  vector<Node> nodes(n);
  for (int i = 0; i < n; i++) {
    int id, lc, rc;
    cin >> id >> lc >> rc;
    nodes[id].id = id;
    nodes[id].lchild = lc;
    nodes[id].rchild = rc;
    if (lc != -1) {
      nodes[lc].parent = id;
    }
    if (rc != -1) {
      nodes[rc].parent = id;
    }
  }

  int root_id;
  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      root_id = i;
      break;
    }
  }

  cout << "Preorder" << endl;
  Preorder(nodes, root_id);
  cout << endl << "Inorder" << endl;
  Inorder(nodes, root_id);
  cout << endl << "Postorder" << endl;
  Postorder(nodes, root_id);
  cout << endl;
  return 0;
}

Error: MLE

Fixed submission:

// ALDS1_7_C.cpp
// Tree - Tree Walk

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

struct Node {
  int id;
  int lchild;
  int rchild;
  int parent = -1;
};

void Preorder(vector<Node> &nodes, int id) {
  cout << " " << nodes[id].id;
  if (nodes[id].lchild == -1 && nodes[id].rchild == -1) {
    return;
  }
  Preorder(nodes, nodes[id].lchild);
  Preorder(nodes, nodes[id].rchild);
}

void Inorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Inorder(nodes, nodes[id].lchild);
  cout << " " << nodes[id].id;
  if (nodes[id].rchild != -1)
    Inorder(nodes, nodes[id].rchild);
}

void Postorder(vector<Node> &nodes, int id) {
  if (nodes[id].lchild != -1)
    Postorder(nodes, nodes[id].lchild);
  if (nodes[id].rchild != -1)
    Postorder(nodes, nodes[id].rchild);
  cout << " " << nodes[id].id;
}

int main() {
  int n;
  cin >> n;
  vector<Node> nodes(n);
  for (int i = 0; i < n; i++) {
    int id, lc, rc;
    cin >> id >> lc >> rc;
    nodes[id].id = id;
    nodes[id].lchild = lc;
    nodes[id].rchild = rc;
    if (lc != -1) {
      nodes[lc].parent = id;
    }
    if (rc != -1) {
      nodes[rc].parent = id;
    }
  }

  int root_id;
  for (int i = 0; i < n; i++) {
    if (nodes[i].parent == -1) {
      root_id = i;
      break;
    }
  }

  cout << "Preorder" << endl;
  Preorder(nodes, root_id);
  cout << endl << "Inorder" << endl;
  Inorder(nodes, root_id);
  cout << endl << "Postorder" << endl;
  Postorder(nodes, root_id);
  cout << endl;
  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// Created by ?????°?????? on 2016/04/08.
//

#include <iostream>
using namespace std;
const int MAX = 10000;
const int NIL = -1;

struct node {
  int parent;
  int right;
  int left;
};

node T[MAX];

void preParse(int u) {
  if (u == NIL)
    return;
  cout << " " << u;
  preParse(T[u].left);
  preParse(T[u].right);
}

void inParse(int u) {
  if (u == NIL)
    return;
  inParse(T[u].left);
  cout << " " << u;
  inParse(T[u].right);
}

void postParse(int u) {
  if (u == NIL)
    return;
  postParse(T[u].left);
  postParse(T[u].right);
  cout << " " << u;
}

int main() {
  int n, root, v, l, r;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    T[i].parent = T[i].left = T[i].right = NIL;
  }

  for (int i = 0; i < n; ++i) {
    cin >> v >> l >> r;
    T[v].left = l;
    T[v].right = r;
    // what is T[l]'s parent?
    if (l != NIL)
      T[l].parent = v;
    if (r != NIL)
      T[r].parent = v;
  }

  // isRoot
  for (int i = 0; i < n; ++i) {
    if (T[i].parent == NIL)
      root = i;
  }

  // preParse
  cout << "Preorder" << endl;
  preParse(root);
  cout << endl;

  // inParse
  cout << "Inorder" << endl;
  inParse(root);
  cout << endl;

  // postParse
  cout << "Postorder" << endl;
  postParse(root);
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int MAX = 10000;
const int NIL = -1;

struct node {
  int parent;
  int right;
  int left;
};

node T[MAX];

void preParse(int u) {
  if (u == NIL)
    return;
  cout << u << " ";
  preParse(T[u].left);
  preParse(T[u].right);
}

void inParse(int u) {
  if (u == NIL)
    return;
  inParse(T[u].left);
  cout << u << " ";
  inParse(T[u].right);
}

void postParse(int u) {
  if (u == NIL)
    return;
  postParse(T[u].left);
  postParse(T[u].right);
  cout << u << " ";
}

int main() {
  int n;
  int root;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> T[i].parent >> T[i].left >> T[i].right;
  }

  // root???????????????
  for (int i = 0; i < n; ++i) {
    if (T[i].parent == NIL)
      i = root;
  }

  // preParse
  cout << "Preorder" << endl;
  preParse(root);
  cout << endl;

  // inParse
  cout << "Inorder" << endl;
  inParse(root);
  cout << endl;

  // postParse
  cout << "Postorder" << endl;
  postParse(root);
  cout << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
struct fun {
  int a;
  int b;
  int c;
} ff[30];
bool cmp(const fun &x, const fun &y) { return x.a < y.a; }
int f[30];
void qian(int x) {
  if (x != -1)
    printf(" %d", ff[x].a);
  else
    return;
  qian(ff[x].b);
  qian(ff[x].c);
}
void zhong(int x) {
  if (x != -1)
    zhong(ff[x].b);
  else
    return;
  printf(" %d", ff[x].a);
  zhong(ff[x].c);
}
void hou(int x) {
  if (x != -1)
    hou(ff[x].b);
  else
    return;
  if (x != -1)
    hou(ff[x].c);
  else
    return;
  printf(" %d", ff[x].a);
}
int main() {
  int n;
  int q;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &ff[i].a, &ff[i].b, &ff[i].c);
    if (ff[i].b == -1 && ff[i].c == -1) {
      f[ff[i].a] = 1;
      continue;
    }
    if (ff[i].b >= 0)
      f[ff[i].b] = 1;
    if (ff[i].c >= 0)
      f[ff[i].c] = 1;
  }
  sort(ff, ff + n, cmp);
  if (n == 1) {
    printf("Preorder\\n 0\\n");
    printf("Inorder\\n 0\\n");
    printf("Postorder\\n 0\\n");
    return 0;
  }
  for (int i = 0; i < n; i++)
    if (!f[i]) {
      q = i;
      break;
    }
  printf("Preorder\\n");
  qian(q);
  printf("\\n");
  printf("Inorder\\n");
  zhong(q);
  printf("\\n");
  printf("Postorder\\n");
  hou(q);
  printf("\\n");
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int a[2000], b[2000], c[2000];
void qian(int x) {
  if (x != -1)
    printf(" %d", a[x]);
  else
    return;
  qian(b[x]);
  qian(c[x]);
}
void zhong(int x) {
  if (x != -1)
    zhong(b[x]);
  else
    return;
  printf(" %d", a[x]);
  zhong(c[x]);
}
void hou(int x) {
  if (x != -1)
    hou(b[x]);
  else
    return;
  if (x != -1)
    hou(c[x]);
  else
    return;
  printf(" %d", a[x]);
}
int main() {
  int n;
  int q;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
  for (int i = 0; i < n; i++) {
    int flag = 1;
    for (int j = 0; j < n; j++) {
      if (a[i] == b[j] || a[i] == c[j]) {
        flag = 0;
        break;
      }
    }
    if (flag)
      q = i;
  }
  printf("Preorder\\n");
  qian(q);
  printf("\\n");
  printf("Inorder\\n");
  zhong(q);
  printf("\\n");
  printf("Postorder\\n");
  hou(q);
  printf("\\n");
  return 0;
}
Problem p02281 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
int rv[30];
struct node {
  int id, left, right, par;
  node() { par = -1; }
};
node t[30];
void dfs(int v, int p) {
  if (p == 0) {
    cout << " " << t[v].id;
    if (~t[v].left)
      dfs(rv[t[v].left], p);
    if (~t[v].right)
      dfs(rv[t[v].right], p);
  }
  if (p == 1) {
    if (~t[v].left)
      dfs(rv[t[v].left], p);
    cout << " " << t[v].id;
    if (~t[v].right)
      dfs(rv[t[v].right], p);
  }
  if (p == 2) {
    if (~t[v].left)
      dfs(rv[t[v].left], p);
    if (~t[v].right)
      dfs(rv[t[v].right], p);
    cout << " " << t[v].id;
  }
}
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> t[i].id >> t[i].left >> t[i].right;
  for (int i = 0; i < n; i++)
    rv[t[i].id] = i;
  for (int i = 0; i < n; i++) {
    // cout<<t[i].id<<":"<<t[i].left<<":"<<t[i].right<<endl;
    if (~t[i].left)
      t[rv[t[i].left]].par = t[i].id;
    if (~t[i].right)
      t[rv[t[i].right]].par = t[i].id;
  }
  for (int i = 0; i < n; i++) {
    if (t[i].par == -1) {
      cout << "Preorder" << endl;
      dfs(i, 0);
      cout << endl;
      cout << "Inorder" << endl;
      dfs(i, 1);
      cout << endl;
      cout << "Postorder" << endl;
      dfs(i, 2);
      cout << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int pre[50], in[50], post[50];
int n, pos, idx;
void dfs(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = -1;
  for (int i = l; i < r; i++)
    if (in[i] == root)
      m = i;
  dfs(l, m);
  dfs(m + 1, r);
  post[idx++] = root;
}
signed main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> pre[i];
  for (int i = 0; i < n; i++)
    cin >> in[i];
  pos = idx = 0;
  dfs(0, n);
  for (int i = 0; i < n; i++)
    cout << post[i] << " \\n"[i == n - 1];
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

vector<int> postord;
int cur, n;
int pre[41], in[41];
void dfs(int l, int r) {
  if (l >= r)
    return;
  int vi = pre[cur];
  ++cur;
  int m = distance(in, find(in, in + n, vi));
  dfs(l, m);
  dfs(m + 1, r);
  postord.pb(vi);
}
int main() {
  int root;
  cin >> n;
  rep(i, n) cin >> pre[i];
  rep(i, n) cin >> in[i];
  root = pre[0];
  dfs(0, n);
  rep(i, n) cout << postord[i] << (i == n - 1 ? "" : " ");
  cout << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

struct node {
  int l, r;
};
node v[41];
vector<int> postord;
void dfs(int vi) {
  if (v[vi].l > 0)
    dfs(v[vi].l);
  if (v[vi].r > 0)
    dfs(v[vi].r);
  postord.pb(vi);
}
int pre[41], in[41];
int main() {
  int n, root;
  cin >> n;
  rep(i, n) cin >> pre[i];
  rep(i, n) cin >> in[i];
  rep(i, 41) v[i].l = v[i].r = -1;
  root = pre[0];
  int cp = 0, ci = 0;
  while (cp < n - 1 && pre[cp] > 0) {
    v[pre[cp]].l = pre[cp + 1];
    ++cp;
    while (cp < n && pre[cp] == in[ci]) {
      ++ci;
      while (ci < n - 1 && in[ci] > 0 && v[in[ci]].l == in[ci + 1])
        ++ci;
      if (ci >= n - 1) {
        cp = n;
        break;
      }
      v[in[ci]].r = in[ci + 1];
      ++ci;
      ++cp;
    }
  }
  dfs(root);
  rep(i, n) cout << postord[i] << (i == n - 1 ? "" : " ");
  cout << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, s[50], ans[50], a;
  vector<int> t;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a, s[a] = i, ans[i] = a;
  for (int i = 0; i < n; i++)
    cin >> a, t.push_back(s[a]);
  t.push_back(-1);
  while (t.size() != 2) {
    for (int i = 0; i < t.size() - 1; i++) {
      if (t[i] > t[i + 1]) {
        cout << ans[t[i]] << " ";
        t.erase(t.begin() + i);
        break;
      }
    }
  }
  cout << ans[0] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, s[50], ans[50], a;
  vector<int> t;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a, s[a] = i, ans[i] = a;
  for (int i = 0; i < n; i++)
    cin >> a, t.push_back(s[a]);
  t.push_back(100);
  while (1) {
    if (t.size() == 2) {
      cout << ans[0] << endl;
      break;
    }
    for (int i = 0; i < t.size() - 1; i++) {
      if (t[i] > t[i + 1]) {
        cout << ans[t[i]] << " ";
        t.erase(t.begin() + i);
        break;
      }
    }
  }
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define NIL -1
struct Node {
  int parent, left, right;
};
struct Node T[41];
int n, P[41], I[41];
int search(int x[], int target, int size) {
  for (int i = 0; i < size; i++) {
    if (x[i] == target)
      return i;
  }
  return 0;
}
bool tansaku(int target, int x[], int size) {
  for (int i = 0; i < size; i++) {
    if (x[i] == target)
      return true;
  }
  return false;
}
void Postorder(int x) {
  if (x != NIL) {
    Postorder(T[x].left);
    Postorder(T[x].right);
    if (x == P[0])
      cout << x;
    else
      cout << x << " ";
  }
}
void saisei(int x[], int s, int p, int size) {
  int l = 0, r = 0, mid = search(x, s, size);
  int L[40], R[40];

  for (int i = 0; i < size; i++) {
    if (i < mid) {
      L[l] = x[i];
      l++;
    } else if (i > mid) {
      R[r] = x[i];
      r++;
    }
  }
  if (l != 0) {
    if (tansaku(P[p + 1], L, l)) {
      T[P[p]].left = P[p + 1];
      T[P[p + 1]].parent = P[p];
      saisei(L, P[p + 1], p + 1, l);
    }
  }
  if (r != 0) {
    if (tansaku(P[p + l + 1], R, r)) {
      T[P[p]].right = P[p + l + 1];
      T[P[p + l + 1]].parent = P[p];
      saisei(R, P[p + l + 1], p + l + 1, r);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> P[i];
  for (int i = 0; i < n; i++) {
    cin >> I[i];
    T[i + 1].parent = T[i + 1].left = T[i + 1].right = NIL;
  }
  saisei(I, P[0], 0, n);
  Postorder(P[0]);
  cout << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define NIL -1
struct Node {
  int parent, left, right;
};
struct Node T[41];
int n, P[41], I[41];
int search(int x[], int m, int size) {
  for (int i = 0; i < size; i++) {
    if (x[i] == m)
      return i;
  }
  return 0;
}
bool tansaku(int target, int x[], int size) {
  for (int i = 0; i < size; i++) {
    if (x[i] == target)
      return true;
  }
  return false;
}
void Postorder(int x) {
  if (x != NIL) {
    Postorder(T[x].left);
    Postorder(T[x].right);
    if (x == P[0])
      cout << x;
    else
      cout << x << " ";
  }
}
void saisei(int x[], int s, int p, int n) {
  int l = 0, r = 0, mid = search(x, s, n + 1);
  int L[40], R[40];
  for (int i = 0; i < n + 1; i++) {
    if (i < mid) {
      L[l] = x[i];
      l++;
    } else if (i > mid) {
      R[r] = x[i];
      r++;
    }
  }
  if (tansaku(P[p + 1], L, l)) {
    T[P[p]].left = P[p + 1];
    T[P[p + 1]].parent = P[p];
    saisei(L, P[p + 1], p + 1, l);
  }
  if (tansaku(P[p + l + 1], R, r)) {
    T[P[p]].right = P[p + l + 1];
    T[P[p + l + 1]].parent = P[p];
    saisei(R, P[p + l + 1], p + l + 1, r);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> P[i];
  for (int i = 0; i < n; i++) {
    cin >> I[i];
    T[i + 1].parent = T[i + 1].left = T[i + 1].right = NIL;
  }
  saisei(I, P[0], 0, n - 1);
  Postorder(P[0]);
  cout << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

struct node {
  int left, right;
} nodes[100];

int n, pre[105], in[105], inloc[105], cnt = 0;

int rec(int s, int e) {
  if (s < e) {
    int m = inloc[pre[cnt++]];
    int l = rec(s, m - 1);
    int r = rec(m + 1, e);
    nodes[in[m]].left = l;
    nodes[in[m]].right = r;
    return in[m];
  } else if (s == e) {
    cnt++;
    return in[s];
  } else {
    return 0;
  }
}

void post(int r) {
  if (nodes[r].left != 0)
    post(nodes[r].left);
  if (nodes[r].right != 0)
    post(nodes[r].right);
  cout << r;
  if (r != pre[0])
    cout << " ";
}

int main() {
  memset(nodes, 0, sizeof(nodes));
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> pre[i];
  }
  for (i = 0; i < n; i++) {
    cin >> in[i];
    inloc[in[i]] = i;
  }
  int mm = rec(0, n - 1);
  post(pre[0]);
  cout << endl;
  return 0;
}
//
// int main(){
//    int a[10]={1,2,3,4,5,32,62,23,8,33};
//    int *b=find(a,a+10,62);
//    int c=distance(a,b);
//    cout<<c;
//}

Error: 0

Fixed submission:

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

struct node {
  int left, right;
} nodes[100];

int n, pre[105], in[105], inloc[105], cnt = 0;

int rec(int s, int e) {
  if (s < e) {
    int m = inloc[pre[cnt++]];
    int l = rec(s, m - 1);
    int r = rec(m + 1, e);
    nodes[in[m]].left = l;
    nodes[in[m]].right = r;
    return in[m];
  } else if (s == e) {
    return in[s];
  } else {
    return 0;
  }
}

void post(int r) {
  if (nodes[r].left != 0)
    post(nodes[r].left);
  if (nodes[r].right != 0)
    post(nodes[r].right);
  cout << r;
  if (r != pre[0])
    cout << " ";
}

int main() {
  memset(nodes, 0, sizeof(nodes));
  cin >> n;
  int i;
  for (i = 0; i < n; i++) {
    cin >> pre[i];
  }
  for (i = 0; i < n; i++) {
    cin >> in[i];
    inloc[in[i]] = i;
  }
  int mm = rec(0, n - 1);
  post(pre[0]);
  cout << endl;
  return 0;
}
//
// int main(){
//    int a[10]={1,2,3,4,5,32,62,23,8,33};
//    int *b=find(a,a+10,62);
//    int c=distance(a,b);
//    cout<<c;
//}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int preorder[40], inorder[40];

void print_postorder(int lp, int rp, int li, int ri);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> preorder[i];
  for (int i = 0; i < n; ++i)
    cin >> inorder[i];

  print_postorder(0, n, 0, n);

  return 0;
}

// lp<= i&&i<rp
void print_postorder(int lp, int rp, int li, int ri) {
  if (lp == rp)
    return;
  // cout << "[" << lp << "," << rp << "] [" << li << "," << ri << "]" << endl;
  int root = preorder[lp];
  int num = 0;
  for (int i = li; i < ri && inorder[i] != root; ++i) {
    num++;
  }
  print_postorder(lp + 1, lp + 1 + num, li, li + num);
  print_postorder(lp + 1 + num, rp, li + num + 1, ri);

  cout << preorder[lp];
  if (lp == 0) {
    cout << endl;
  } else {
    cout << " ";
  }
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int preorder[40], inorder[40];

void print_postorder(int lp, int rp, int li, int ri);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> preorder[i];
  for (int i = 0; i < n; ++i)
    cin >> inorder[i];

  print_postorder(0, n, 0, n);

  return 0;
}

// lp<= i&&i<rp
void print_postorder(int lp, int rp, int li, int ri) {
  int root = preorder[lp];
  int num = 0;
  for (int i = li; inorder[i] != root; ++i) {
    num++;
  }
  if (num != 0) {
    print_postorder(lp + 1, lp + 1 + num, li, li + num);
    print_postorder(lp + 1 + num, rp, li + num + 1, ri);
  }
  cout << preorder[lp];
  if (lp == 0) {
    cout << endl;
  } else {
    cout << " ";
  }
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

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

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
  if (l >= r)
    return;
  int root = pre[pos++];
  int m = distance(in.begin(), find(in.begin(), in.end(), root));
  rec(l, m);
  rec(m + 1, r);
  post.push_back(root);
}

void solve() {
  pos = 0;
  rec(0, pre.size());
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << post[i];
  }
  cout << endl;
}

int main(void) {
  int k;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> k;
    pre.push_back(k);
  }

  for (int i = 0; i < n; i++) {
    cin >> k;
    in.push_back(k);
  }

  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define MAX 10000
#define NIL -1

struct Node {
  int parent, left, right;
};
Node node[MAX];
int n, pre[100], in[100], nowroot = -1;
vector<int> post;

int getRootPos(int nowroot) {
  for (int i = 0; i < n; i++) {
    if (pre[nowroot] == in[i])
      return i;
  }
}

int makeTree(int l, int r) {
  if (r == l)
    return in[l];
  if (r < l)
    return NIL;

  int rootpos = getRootPos(++nowroot);
  node[in[rootpos]].left = makeTree(l, rootpos - 1);
  node[in[rootpos]].right = makeTree(rootpos + 1, r);
  node[node[in[rootpos]].left].parent = in[rootpos];
  node[node[in[rootpos]].right].parent = in[rootpos];
  return in[rootpos];
}

/*
void printNodeInf(int n){
  cout << "node : " << n << ", ";
  cout << "left : " << node[n].left << ", ";
  cout << "right : " << node[n].right << ", ";
  cout << "parent : " << node[n].parent << endl;
}
*/

void postParse(int u) {
  if (u == NIL)
    return;
  postParse(node[u].left);
  postParse(node[u].right);
  post.push_back(u);
}

int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++)
    node[i].parent = node[i].left = node[i].right = NIL;
  for (int i = 0; i < n; i++)
    cin >> pre[i];
  for (int i = 0; i < n; i++)
    cin >> in[i];
  makeTree(0, n - 1);
  // for(int i = 1; i <= n; i++) printNodeInf(i);
  postParse(pre[0]);
  for (int i = 0; i < post.size(); i++) {
    cout << post[i];
    if (i < post.size() - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}
Problem p02282 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
using namespace std;

vi pre, in, post;
int root;

void rec(int l, int r) {
  if (r - l < 1)
    return;
  int m;
  Rep(i, l, r) {
    if (in[i] == pre[root]) {
      m = i;
      root++;
      break;
    }
  }
  rec(l, m);
  rec(m + 1, r);
  post.pb(in[m]);
}

signed main() {
  int n;
  cin >> n;
  int v;
  rep(i, n) {
    cin >> v;
    pre.pb(v);
  }
  rep(i, n) {
    cin >> v;
    in.pb(v);
  }
  rec(0, n);
  rep(i, n) { cout << post[i] << (i != n - 1 ? ' ' : '\\n'); }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
using namespace std;

struct Node {
  int p, l, r;
};

Node t[40];
vi pre, in, post;
int root;

void rec(int l, int r) {
  if (r - l < 1)
    return;
  if (r - l == 1) {
    post.pb(in[l]);
    return;
  }
  int m;
  Rep(i, l, r) {
    if (in[i] == pre[root]) {
      m = i;
      root++;
      break;
    }
  }
  rec(l, m);
  rec(m + 1, r);
  post.pb(in[m]);
}

signed main() {
  int n;
  cin >> n;
  int v;
  rep(i, n) {
    cin >> v;
    pre.pb(v);
  }
  rep(i, n) {
    cin >> v;
    in.pb(v);
  }
  rec(0, n);
  rep(i, n) { cout << post[i] << (i != n - 1 ? ' ' : '\\n'); }
  return 0;
}