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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    long long int nowmap[51] = {};
    vector<vector<int>> schedule(31);
    for (int i = 1; i < n + 1; i++) {
      int f;
      cin >> f;
      for (int j = 0; j < f; j++) {
        int d;
        cin >> d;
        schedule[d].push_back(i);
      }
    }
    for (int i = 1; i < n + 1; i++) {
      nowmap[i] = (long long int)1 << (i - 1);
      // cout << nowmap[i] << endl;
    }
    // cout << n << endl;
    long long int maxmap = (long long int)1 << n;
    maxmap--;
    // cout << maxmap << endl;
    for (int i = 1; i < 31; i++) {
      // cout << "day : " << i << endl;
      long long int gatheredmap = 0;
      for (int j = 0; j < schedule[i].size(); j++) {
        // cout << schedule[i][j] << endl;
        gatheredmap |= (long long int)nowmap[schedule[i][j]];
      }
      // cout << gatheredmap << endl;
      // cout << (long long int)1 << n << endl;
      if (gatheredmap == maxmap) {
        cout << i << endl;
        break;
      }
      for (int j = 0; j < schedule[i].size(); j++) {
        nowmap[schedule[i][j]] = gatheredmap;
      }
      if (i == 30)
        cout << -1 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool canmeet[51][31];
int n;
bool gatherd[51];
int result = 31;

void solve(int who, int day) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (gatherd[i])
      ans++;
  }
  if (ans == n) {
    result = min(result, day - 1);
    return;
  }
  if (!canmeet[who][day])
    return;
  for (int i = 0; i < n; i++) {
    if (canmeet[i][day])
      gatherd[i] = true;
  }
  for (int i = 0; i < n; i++) {
    if (canmeet[i][day])
      solve(i, day + 1);
  }
  for (int i = 0; i < n; i++) {
    if (canmeet[i][day])
      gatherd[i] = false;
  }
  return;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    result = 31;
    for (int i = 0; i < n; i++) {
      gatherd[i] = false;
      for (int j = 1; j < 31; j++) {
        canmeet[i][j] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int l;
        cin >> l;
        canmeet[i][l] = true;
      }
    }
    for (int i = 0; i < n; i++) {
      solve(i, 1);
    }
    if (result != 31)
      cout << result << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;

int N, F, S, D[31][50];
ll dp[31][50];

int main(void) {
  while (cin >> N, N) {
    REP(i, 0, 31) REP(j, 0, N) D[i][j] = 0;
    REP(i, 0, N) {
      cin >> F;
      REP(j, 0, F) {
        cin >> S;
        D[S][i] = 1;
      }
    }

    REP(i, 0, 31) REP(j, 0, N) dp[i][j] = 0;
    REP(i, 0, N) dp[0][i] = (1LL << i);

    int ans = -1;
    REP(i, 1, 31) {
      REP(j, 0, N) dp[i][j] = dp[i - 1][j];
      REP(j, 0, N) REP(k, 0, N) {
        if (D[i][j] == 1 && D[i][k] == 1) {
          dp[i][j] |= dp[i - 1][k];
        }
      }
      REP(j, 0, N) if (dp[i][j] == (1LL << N) - 1) {
        ans = i;
        goto ANS;
      }
    }

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

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;

int N, F, S, D[31][50];
ll dp[31][50];

int main(void) {
  while (cin >> N, N) {
    REP(i, 0, N) REP(j, 0, 30) D[i][j] = 0;
    REP(i, 0, N) {
      cin >> F;
      REP(j, 0, F) {
        cin >> S;
        D[S][i] = 1;
      }
    }

    REP(i, 0, 31) REP(j, 0, N) dp[i][j] = 0;
    REP(i, 0, N) dp[0][i] = (1 << i);

    int ans = -1;
    REP(i, 1, 31) {
      REP(j, 0, N) dp[i][j] = dp[i - 1][j];
      REP(j, 0, N) REP(k, 0, N) {
        if (D[i][j] == 1 && D[i][k] == 1) {
          dp[i][j] |= dp[i - 1][k];
        }
      }
      REP(j, 0, N) if (dp[i][j] == (1 << N) - 1) {
        ans = i;
        goto ANS;
      }
    }

  ANS:
    cout << ans << endl;
  }
  return 0;
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n && n) {
    vector<int> ok[31];
    for (int i = 1; i <= n; i++) {
      int f;
      cin >> f;
      for (int j = 0; j < f; j++) {
        int t;
        cin >> t;
        ok[t].push_back(i);
      }
    }
    vector<set<int>> met(n + 1);
    for (int i = 1; i <= n; i++) {
      met[i].insert(i);
    }

    int ans = -1;
    for (int i = 1; i <= 30; i++) {
      vector<set<int>> nmet(n + 1);
      for (int j = 1; j <= n; j++) {
        nmet[j] = met[j];
      }
      set<int> s;
      for (int j = 0; j < ok[i].size(); j++) {
        for (int x : met[ok[i][j]]) {
          s.insert(x);
        }
      }
      for (int j = 0; j < ok[i].size(); j++) {
        nmet[ok[i][j]] = s;
      }

      // for(int j=1;j<=n;j++){
      //     printf(" met %d: ",j);
      //     for(int x: nmet[j]){
      //         cout << x << " ";
      //     }
      //     cout << endl;
      // }

      for (int j = 1; j <= n; j++) {
        if (nmet[j].size() == n) {
          ans = i;
          break;
        }
      }
      if (ans != -1)
        break;
      met = nmet;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n;
  while (cin >> n && n) {
    UnionFind uf(n);
    vector<vector<int>> s(31);
    for (int i = 0; i < n; i++) {
      int f;
      cin >> f;
      for (int j = 0; j < f; j++) {
        int t;
        cin >> t;
        s[t].push_back(i + 1);
      }
    }
    int ans = -1;
    for (int i = 1; i <= 30; i++) {
      for (int x : s[i]) {
        uf.unionSet(x, s[i][0]);
      }
      if (uf.size(1) == n) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Time Limit Exceeded

#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 vi vector<int>
#define pb push_back
#define INF 999999999

int n;

bool solve(vector<int> data, set<int> dp[31][51], int day) {
  set<int> tmp;
  rep(i, data.size()) {
    for (auto &elm : dp[day - 1][data[i]]) {
      tmp.insert(elm);
    }
  }
  if (tmp.size() == n)
    return true;

  rep(i, n) dp[day][i] = dp[day - 1][i];

  rep(i, data.size()) dp[day][data[i]] = tmp;

  return false;
}

int main() {
  while (cin >> n && n) {
    vector<int> data[31];
    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int a;
        cin >> a;
        data[a].pb(i);
      }
    }

    set<int> dp[31][51];
    rep(i, n) dp[0][i].insert(i);

    int ans = INF;
    for (int i = 1; i <= 30; i++) {
      if (solve(data[i], dp, i)) {
        ans = i;
        break;
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

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 vi vector<int>
#define pb push_back
#define INF 999999999

void same(set<int> st[51], vector<int> &data) {
  rep(i, data.size()) {
    rep(j, data.size()) {
      int a = data[i], b = data[j];
      if (i == j)
        continue;
      for (auto &elem : st[a]) {
        st[b].insert(elem);
      }
    }
  }
}

int n;

int main() {
  while (cin >> n && n) {
    vector<int> data[31];
    rep(i, n) {
      int m;
      cin >> m;
      rep(j, m) {
        int a;
        cin >> a;
        data[a].pb(i);
      }
    }
    set<int> st[51];
    rep(i, n) { st[i].insert(i); }
    int ans = INF;
    rep(i, 31) {
      rep(j, data[i].size()) {
        same(st, data[i]);
        rep(k, n) {
          if (st[k].size() == n) {
            ans = i;
            goto next;
          }
        }
      }
    }
  next:;
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

bool f(int m, bool a[][100], int n) {

  bool b[100] = {};
  queue<P> q;

  rep(i, n) {
    if (a[i][m]) {
      b[i] = true;
      q.push(P(i, m));
    }
  }

  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (!p.second)
      continue;

    if (a[p.first][p.second]) {
      rep(i, n) {
        if (a[i][p.second] && !b[i]) {
          b[i] = true;
          q.push(P(i, p.second - 1));
        }
      }
    }
    q.push(P(p.first, p.second - 1));
  }

  rep(i, n) {
    if (!b[i]) {
      return false;
    }
  }

  return true;
}

void search(int n) {
  bool a[100][100] = {};

  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int m;
      cin >> m;
      a[i][m] = true;
    }
  }

  rep(i, 31) {
    if (f(i, a, n)) {
      cout << i << endl;
      return;
    }
  }

  cout << -1 << endl;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    search(n);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

void g(int m, bool a[][100], int k, int n, int b[]) {

  rrep(j, m) {
    if (!a[k][j])
      continue;
    rep(i, n) {
      if (a[i][j]) {
        b[i] = max(b[i], j - 1);
      }
    }
    rep(i, n) {
      if (i == k)
        continue;
      if (a[i][j] && b[i] >= j - 1) {
        g(j - 1, a, i, n, b);
      }
    }
  }
}

bool f(int m, bool a[][100], int n) {

  int b[100];

  rep(i, n) {
    if (!a[i][m])
      continue;
    rep(k, n) b[k] = -1;
    b[i] = m;
    g(m, a, i, n, b);
    int j;
    for (j = 0; j < n; j++) {
      if (b[j] == -1)
        break;
    }
    if (j == n)
      return true;
    return false;
  }
  return false;
}

void search(int n) {
  bool a[100][100] = {};

  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int m;
      cin >> m;
      a[i][m] = true;
    }
  }

  rep(i, 31) {
    if (f(i, a, n)) {
      cout << i << endl;
      return;
    }
  }

  cout << -1 << endl;
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    search(n);
  }

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

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<int> date[30];
    vector<set<int>> dat(n);
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int f;
        cin >> f;
        f--;
        date[f].push_back(i);
      }
    }
    for (int i = 0; i < n; i++)
      dat[i].insert(i);
    int ret = 0;
    while (ret < 30) {
      for (int i = 0; i < date[ret].size(); i++) {
        int from = date[ret][i];
        for (int j = 0; j < date[ret].size(); j++) {
          int to = date[ret][j];
          for (int it : dat[from])
            dat[to].insert(it);
        }
      }
      bool end = false;
      for (int i = 0; i < n; i++) {
        if (dat[i].size() == n) {
          end = true;
          break;
        }
      }
      if (end)
        break;
      ret++;
    }
    cout << (ret == 30 ? -1 : ret + 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<set<int>> s(n);
    set<int> dat[32];
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      while (m--) {
        int f;
        cin >> f;
        f--;
        dat[f].insert(i);
      }
    }
    for (int i = 0; i < n; i++)
      dat[i].insert(i);
    int ret = 0;
    for (; ret < 30; ret++) {
      for (auto it = dat[ret].begin(); it != dat[ret].end(); it++) {
        s[*it].insert(dat[ret].begin(), dat[ret].end());
      }
      bool end = false;
      for (int i = 0; i < n; i++) {
        if (s[i].size() == n) {
          end = true;
          break;
        }
      }
      if (end)
        break;
    }
    cout << (ret == 30 ? -1 : ret + 1) << endl;
  }
  return 0;
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
int main() {
  for (; scanf("%d", &N), N;) {
    set<int> dp[99];
    vector<int> D[99];
    for (int i = 0; i < N; i++) {
      int t, d;
      dp[i].insert(i);
      scanf("%d", &t);
      for (int j = 0; j < t; j++) {
        scanf("%d", &d);
        D[d].push_back(i);
      }
    }
    for (int i = 0; i < 31; i++) {
      set<int> a;
      for (int j = 0; j < D[i].size(); j++) {
        for (set<int>::iterator it = dp[D[i][j]].begin();
             it != dp[D[i][j]].end(); it++)
          a.insert(*it);
      }
      if (a.size() == N) {
        printf("%d\\n", i);
        goto e;
      }
      for (int j = 0; j < D[i].size(); j++)
        dp[D[i][j]] = a;
    }
    puts("-1");
  e:;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
int main() {
  for (; scanf("%d", &N), N;) {
    set<int> dp[35];
    vector<int> D[35];
    for (int i = 0; i < N; i++) {
      int t, d;
      dp[i].insert(i);
      scanf("%d", &t);
      for (int j = 0; j < t; j++) {
        scanf("%d", &d);
        D[d].push_back(i);
      }
    }
    for (int i = 0; i < 31; i++) {
      set<int> a;
      for (int j = 0; j < D[i].size(); j++) {
        for (set<int>::iterator it = dp[D[i][j]].begin();
             it != dp[D[i][j]].end(); it++)
          a.insert(*it);
      }
      if (a.size() == N) {
        printf("%d\\n", i);
        goto e;
      }
      for (int j = 0; j < D[i].size(); j++)
        dp[D[i][j]] = a;
    }
    puts("-1");
  e:;
  }
}
Problem p01136 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int n;
vector<int> vec[35];
bool have[55][55];

void exchange(int s, int t) {
  for (int i = 1; i <= n; i++) {
    have[s][i] = have[t][i] = (have[s][i] | have[t][i]);
  }
}

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    int f, d;
    for (int i = 1; i <= 30; i++)
      vec[i].clear();
    for (int i = 1; i <= n; i++) {
      cin >> f;
      for (int j = 0; j < f; j++) {
        cin >> d;
        vec[d].push_back(i);
      }
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        have[i][j] = false;
        if (i == j)
          have[i][j] = true;
      }
    }
    for (int i = 1; i <= 30; i++) {
      for (int j = 0; j < vec[i].size(); j++) {
        for (int k = 0; k < vec[i].size(); k++) {
          if (j >= k)
            continue;
          exchange(vec[i][j], vec[i][k]);
        }
      }
      for (int j = 1; j <= n; j++) {
        bool flag = true;
        for (int k = 1; k <= n; k++)
          flag &= have[j][k];
        if (flag) {
          cout << i << endl;
          goto end;
        }
      }
    }

    cout << -1 << endl;
  end:;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int n;
vector<int> vec[35];

int parent[55];
void init() {
  for (int i = 1; i <= n; i++)
    parent[i] = i;
}
int root(int i) {
  if (parent[i] == i)
    return i;
  return parent[i] = root(parent[i]);
}
void unite(int i, int j) {
  int root_i = root(i), root_j = root(j);
  if (root_i == root_j)
    return;
  parent[root_i] = root_j;
}
int Size() {
  set<int> s;
  for (int i = 1; i <= n; i++) {
    s.insert(root(i));
  }
  return (int)s.size();
}

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = 1; i <= 30; i++)
      vec[i].clear();
    for (int i = 1; i <= n; i++) {
      int f, d;
      cin >> f;
      for (int j = 0; j < f; j++) {
        cin >> d;
        vec[d].push_back(i);
      }
    }

    init();
    for (int i = 1; i <= 30; i++) {
      for (int j = 0; j < vec[i].size() - 1; j++) {
        unite(vec[i][j], vec[i][j + 1]);
      }
      if (Size() == 1) {
        cout << i << endl;
        break;
      }
    }
    if (Size() != 1)
      cout << -1 << endl;
  }

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

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n, f0, f, day;
  int i, j, k;
  while (cin >> n, n) {
    vector<vector<bool>> v(n, vector<bool>(31, false));
    for (i = 0; i < n; i++) {
      for (cin >> f0; f0-- > 0;) {
        cin >> f;
        v[i][f] = true;
      }
    }

    day = -1;
    vector<vector<set<int>>> dp(n, vector<set<int>>(31));
    set<int>::iterator it;
    for (i = 0; i < n; i++)
      dp[i][0].insert(i);
    for (j = 1; j < 31; j++) {
      for (i = 0; i < n; i++) {
        for (k = i; k < n; k++)
          dp[k][j] = dp[k][j - 1];

        if (v[i][j]) {
          for (k = i; k < n; k++) {
            if (v[k][j]) {
              for (it = dp[k][j].begin(); it != dp[k][j].end(); it++) {
                dp[i][j].insert(*it);
              }
            }
          }
          for (k = i; k < n; k++) {
            if (v[k][j]) {
              for (it = dp[i][j].begin(); it != dp[i][j].end(); it++) {
                dp[k][j].insert(*it);
              }
            }
          }
          if (dp[i][j].size() == n)
            day = j;
          break;
        }

        if (day >= 0)
          break;
      }
      if (day >= 0)
        break;
    }

    cout << day << endl;
  }
}

Error: 0

Fixed submission:


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

int main() {
  int n, i, f0, f;
  map<int, int> m;
  vector<vector<int>> v(31, vector<int>());
  vector<int>::iterator it;
  while (cin >> n, n) {
    v.clear();
    m.clear();
    for (i = 0; i < n; i++) {
      cin >> f0;
      while (f0-- > 0) {
        cin >> f;
        v[f].push_back(i);
      }
    }
    for (i = 0; i < n; i++) {
      for (it = v[i].begin(); it != v[i].end(); it++) {
        m[*it] = 1;
      }
      if (m.size() == n)
        break;
    }
    if (m.size() == n)
      cout << i << endl;
    else
      cout << "-1" << endl;
    ;
  }
}
Problem p01137 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for (LL i = 0; i < n; ++i)
#define FOR(i, a, n) for (LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

// #define int long long

/*************** using variables ***************/
int e;
int m = INF;
/**********************************************/

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

  while (cin >> e, e) {
    m = INF;
    for (int z = 0; z * z * z <= e; z++) {
      for (int y = 0; y * y <= e - z * z * z; y++) {
        int x = e - y * y - z * z * z;
        if (x + y * y + z * z * z == e) {
          // cout << x << " " << y << " " << z << endl;
          m = min(m, x + y + z);
        }
      }
    }

    cout << m << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for (LL i = 0; i < n; ++i)
#define FOR(i, a, n) for (LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

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

// #define int long long

/*************** using variables ***************/
int e;
int m = INF;
/**********************************************/

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

  while (cin >> e, e) {
    m = INF;
    for (int x = 0; x <= e; x++) {
      for (int y = 0; y * y <= e - x; y++) {
        int z3 = e - x - y * y;
        int z = cbrt(z3);
        if (z3 == z * z * z) {
          if (x + y * y + z * z * z == e) {
            // cout << x << " " << y << " " << z << endl;
            m = min(m, x + y + z);
          }
        }
      }
    }

    cout << m << endl;
  }
}
Problem p01137 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int e, x, y, z, ans;
  while (cin >> e, e) {
    ans = 1e6;
    for (z = 0; z * z * z <= e; ++z) {
      for (y = 0; z * z * z + y * y <= e; ++y) {
        ans = min(ans, e - (z * z * z + y * y) + y + z);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int e;
  while (cin >> e, e) {
    // x+y^2+z^3=e<=10^6
    // x<=10^6,y<=10^3,z<=10^2
    // x+y+z=m

    int z = (int)pow(e, 1.0 / 3.0) + 1;
    while (z * z * z > e)
      --z;
    int y = (int)sqrt(e) + 1;
    while (y * y > e)
      --y;
    int x = 1000;
    while (x > e)
      --x;

    int X, Y, Z, ans = 1e6;
    for (Z = z; Z >= 0; --Z) {
      for (Y = y; Y >= 0; --Y) {
        for (X = x; X >= 0; --X) {
          if (X + Y * Y + Z * Z * Z < e)
            break;
          if (X + Y * Y + Z * Z * Z == e) {
            ans = min(ans, X + Y + Z);
            // cerr<<X<<","<<Y<<","<<Z<<endl;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}