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

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

bool isprime[100001];

void eratos(int n) {

  for (int i = 0; i <= n; i++) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}
int main() {

  eratos(100000);

  int m, a, b;

  while (1) {

    cin >> m >> a >> b;
    if (m == 0 && a == 0 && b == 0)
      break;

    for (int i = m; i > 0; i--) {
      bool flag = false;
      for (int j = 1; j <= sqrt(i); j++) {
        if (i % j != 0)
          continue;
        if (isprime[j] == 0 || isprime[i / j] == 0)
          continue;
        if ((double)a / b > (double)j / (i / j) || (double)j / (i / j) > 1)
          continue;
        cout << j << " " << i / j << endl;
        flag = true;
        break;
      }
      if (flag)
        break;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

bool isprime[100001];

void eratos(int n) {

  for (int i = 0; i <= n; i++) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
}
int main() {

  eratos(100000);

  int m, a, b;

  while (1) {

    cin >> m >> a >> b;
    if (m == 0 && a == 0 && b == 0)
      break;

    for (int i = m; i > 0; i--) {
      bool flag = false;
      for (int j = 1; j < i; j++) {
        if (i % j != 0)
          continue;
        if (isprime[j] == 0 || isprime[i / j] == 0)
          continue;
        if ((double)a / b > (double)j / (i / j) || (double)j / (i / j) > 1)
          continue;
        cout << j << " " << i / j << endl;
        flag = true;
        break;
      }
      if (flag)
        break;
    }
  }

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

#include <iostream>

using namespace std;

#define PRIMENUM 200100

// その数は素数かどうか判定する変数
int isPrime[PRIMENUM];

void makePrimeNumber() {

  isPrime[2] = 1;
  isPrime[3] = 1;

  for (int i = 4; i < PRIMENUM; i++) {
    int flag = 0;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      isPrime[i] = 1;
      // cout<<i<<endl;
    }
  }
}

// 素数列、上の関数を使ってから発動する。
int primeLine[PRIMENUM];

void makePrimeLine() {

  int pos = 0;

  for (int i = 0; i < PRIMENUM; i++) {
    if (isPrime[i] == 1) {
      primeLine[pos] = i;
      pos++;
    }
  }
}

int main() {

  makePrimeNumber();
  makePrimeLine();

  int m, a, b;

  while (cin >> m >> a >> b) {
    if (m == 0 && a == 0 && b == 0)
      break;

    int maxP = 0, maxQ = 0;
    for (int i = 0; primeLine[i] <= m; i++) {
      int nextNum = m / primeLine[i];
      // cout<<"p="<<primeLine[i];
      for (int j = nextNum; j >= 0; j--) {
        int flag = 0;
        if (primeLine[i] > j)
          break;
        if (isPrime[j] == 1) {
          //	cout<<"q="<<j<<endl;
          if (j * primeLine[i] <= m && maxP * maxQ <= j * primeLine[i] &&
              j >= primeLine[i] && a * j <= primeLine[i] * b) {
            maxP = primeLine[i];
            maxQ = j;
            // cout<<"unko"<<endl;
            flag = 1;
          }
        }
        if (flag == 1)
          break;
      }
    }
    //	cout<<endl;

    cout << maxP << " " << maxQ << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define PRIMENUM 200100

// その数は素数かどうか判定する変数
int isPrime[PRIMENUM];

void makePrimeNumber() {

  isPrime[2] = 1;
  isPrime[3] = 1;

  for (int i = 4; i < PRIMENUM; i++) {
    int flag = 0;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) {
      isPrime[i] = 1;
      // cout<<i<<endl;
    }
  }
}

// 素数列、上の関数を使ってから発動する。
int primeLine[PRIMENUM];

void makePrimeLine() {

  int pos = 0;

  for (int i = 0; i < PRIMENUM; i++) {
    if (isPrime[i] == 1) {
      primeLine[pos] = i;
      pos++;
    }
  }
}

int main() {

  makePrimeNumber();
  makePrimeLine();

  int m, a, b;

  while (cin >> m >> a >> b) {
    if (m == 0 && a == 0 && b == 0)
      break;

    int maxP = 0, maxQ = 0;
    for (int i = 0; primeLine[i] <= m; i++) {
      for (int j = i; primeLine[i] * primeLine[j] < m; j++) {
        if (primeLine[i] * primeLine[j] <= m &&
            (double)a / (double)b <=
                (double)primeLine[i] / (double)primeLine[j] &&
            primeLine[i] * primeLine[j] >= maxP * maxQ) {
          maxP = primeLine[i];
          maxQ = primeLine[j];
        }
      }
    }

    cout << maxP << " " << maxQ << endl;
  }

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

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

int m, a, b;

vector<int> prime;
void solve() {
  int x = 0, y = 0;
  int ub = prime.size(), lb = 0;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    if (m / prime[mid] < prime[mid])
      ub = mid;
    else
      lb = mid;
  }
  for (int i = lb; i < prime.size(); i++) {
    int q = prime[i];
    ub = i;
    lb = 0;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      int p = prime[mid];
      if (b * p < a * q)
        lb = mid;
      else
        ub = mid;
    }
    if (prime[ub] > (m + q - 1) / q)
      continue;
    for (int j = ub; j <= i; j++) {
      int p = prime[j];
      if (p * q > m)
        break;
      if (p * q > x * y) {
        x = p;
        y = q;
      }
    }
  }
  printf("%d %d\\n", x, y);
}

int main() {
  bool f[50000];
  fill_n(f, 50000, true);
  f[0] = f[1] = true;
  for (int i = 2; i < 50000; i++) {
    if (!f[i])
      continue;
    for (int j = i + i; j < 50000; j += i)
      f[j] = false;
  }
  for (int i = 2; i < 50000; i++)
    if (f[i])
      prime.push_back(i);

  while (scanf("%d%d%d", &m, &a, &b), m || a || b)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

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

int m, a, b;

vector<int> prime;
void solve() {
  int x = 0, y = 0;
  int ub = prime.size(), lb = 0;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    if (m / prime[mid] < prime[mid])
      ub = mid;
    else
      lb = mid;
  }
  for (int i = lb; i < prime.size(); i++) {
    int q = prime[i];
    for (int j = i; j >= 0; j--) {
      int p = prime[j];
      if (b * p < a * q)
        break;
      if (p * q > m)
        continue;
      if (p * q > x * y) {
        x = p;
        y = q;
      }
    }
  }
  printf("%d %d\\n", x, y);
}

int main() {
  bool f[50000];
  fill_n(f, 50000, true);
  f[0] = f[1] = true;
  for (int i = 2; i < 50000; i++) {
    if (!f[i])
      continue;
    for (int j = i + i; j < 50000; j += i)
      f[j] = false;
  }
  for (int i = 2; i < 50000; i++)
    if (f[i])
      prime.push_back(i);

  while (scanf("%d%d%d", &m, &a, &b), m || a || b)
    solve();
  return 0;
}
Problem p00811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

#define MMAX 100000
#define YES 0
#define NO 1

struct S {
  int p, q, pq;
};
int main() {

  int m, a, b;
  int hurui[MMAX + 1] = {YES};
  int prime[MMAX + 1];
  int cnt = 0;
  S ans = {-1, -1, -1};

  hurui[0] = NO, hurui[1] = NO;
  for (int i = 2; i < MMAX; i++) {
    if (hurui[i] == YES) {
      prime[cnt] = i;
      cnt++;
      for (int j = i * 2; j < MMAX; j += i) {
        hurui[j] = NO;
      }
    }
  }

  while (1) {

    cin >> m >> a >> b;
    if (a == 0 && b == 0 && m == 0)
      break;

    for (int i = 0; m >= prime[i] * prime[0]; i++) {
      for (int j = 0; m >= prime[i] * prime[j]; j++) {
        if ((double)prime[i] / prime[j] >= (double)a / b &&
            (double)prime[i] / prime[j] <= 1) {
          if (ans.pq < prime[i] * prime[j]) {
            ans.p = prime[i], ans.q = prime[j], ans.pq = prime[i] * prime[j];
          }
        }
      }
    }
    cout << ans.p << " " << ans.q << endl;
    ans.p = -1, ans.q = -1, ans.pq = -1;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define MMAX 100000
#define YES 0
#define NO 1

struct S {
  int p, q, pq;
};
int main() {

  int m, a, b;
  int hurui[MMAX + 1] = {YES};
  int prime[MMAX + 1];
  int cnt = 0;
  S ans = {-1, -1, -1};

  hurui[0] = NO, hurui[1] = NO;
  for (int i = 2; i < MMAX; i++) {
    if (hurui[i] == YES) {
      prime[cnt] = i;
      cnt++;
      for (int j = i * 2; j < MMAX; j += i) {
        hurui[j] = NO;
      }
    }
  }

  while (1) {

    cin >> m >> a >> b;
    if (a == 0 && b == 0 && m == 0)
      break;

    for (int i = 0; i < cnt; i++) {
      for (int j = 0; j < cnt; j++) {
        if (prime[i] * prime[j] <= m &&
            (double)prime[i] / prime[j] >= (double)a / b &&
            (double)prime[i] / prime[j] <= 1) {
          if (ans.pq < prime[i] * prime[j]) {
            ans.p = prime[i], ans.q = prime[j], ans.pq = prime[i] * prime[j];
          }
        }
      }
    }
    cout << ans.p << " " << ans.q << endl;
    ans.p = -1, ans.q = -1, ans.pq = -1;
  }
  return 0;
}
Problem p00811 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;
int aa[1000000];
bool isprime(long long int a) {
  if (a == 1 || a == 0)
    return false;
  for (long long int i = 2; i * i <= a; ++i) {
    if ((a % i)) {
    } else {
      return false;
    }
  }
  return true;
}
int main() {
  for (int i = 0; i < 1000000; ++i) {
    aa[i] = isprime(i);
  }
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (!m)
      break;

    int ans = 0;
    int p;
    int q;

    for (int i = 2; i < sqrt(m) + 2; ++i) {
      if (!aa[i])
        continue;
      for (int j = i; j < m / i + 2; ++j) {

        if (i * j <= m && aa[j]) {
          if (double(a) / b <= double(i) / j + 1e-8) {
            if (ans < i * j) {
              ans = i * j;
              p = i;
              q = j;
            }
          }
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;
int aa[10000];
bool isprime(long long int a) {
  if (a == 1 || a == 0)
    return false;
  for (long long int i = 2; i * i <= a; ++i) {
    if ((a % i)) {
    } else {
      return false;
    }
  }
  return true;
}
int main() {
  for (int i = 0; i < 10000; ++i) {
    aa[i] = isprime(i);
  }
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (!m)
      break;

    int ans = 0;
    int p;
    int q;

    for (int i = 2; i < sqrt(m) + 2; ++i) {
      if (aa[i])
        continue;
      for (int j = i; j < m / i + 2; ++j) {

        if (i * j <= m && aa[j]) {
          if (double(a) / b <= double(i) / j + 1e-8) {
            if (ans < i * j) {
              ans = i * j;
              p = i;
              q = j;
            }
          }
        }
      }
    }
    cout << p << " " << q << endl;
  }
  return 0;
}
Problem p00811 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  bool isPrime[100001];
  for (int i = 0; i <= 100000; i++)
    isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i <= 100000; i++) {
    if (isPrime[i]) {
      for (int j = i + i; j <= 100000; j += i)
        isPrime[j] = false;
    }
  }
  int m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    int P = 0, Q = 0;
    for (int p = 0; p <= m / 2; p++) {
      if (!isPrime[p])
        continue;
      for (int q = 0; q <= m / p; q++) {
        if (!isPrime[q])
          continue;
        if (p * q > m)
          continue;
        if (p > q)
          continue;
        if (a * q > p * b)
          continue;
        if (p * q > P * Q)
          P = p, Q = q;
      }
    }
    cout << P << " " << Q << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  bool isPrime[100001];
  for (int i = 0; i <= 100000; i++)
    isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i <= 100000; i++) {
    if (isPrime[i]) {
      for (int j = i + i; j <= 100000; j += i)
        isPrime[j] = false;
    }
  }
  int m, a, b;
  while (cin >> m >> a >> b, m || a || b) {
    int P = 0, Q = 0;
    for (int p = 0; p <= m / 2; p++) {
      if (!isPrime[p])
        continue;
      for (int q = p; q <= m / 2; q++) {
        if (!isPrime[q])
          continue;
        if (p * q > m)
          continue;
        if (a * q > p * b)
          continue;
        if (p * q > P * Q)
          P = p, Q = q;
      }
    }
    cout << P << " " << Q << endl;
  }
  return 0;
}
Problem p00811 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
int SIZE = 1e5;
int main() {
  int prime[SIZE + 1];
  vector<int> V;
  memset(prime, 1, sizeof(prime));
  prime[0] = prime[1] = 0;
  REP(i, SIZE + 1) {
    if (prime[i]) {
      for (int j = i * 2; j <= SIZE; j += i)
        prime[j] = 0;
      V.PB(i);
    }
  }
  REP(i, SIZE) {
    if (prime[i + 1])
      prime[i + 1] = i + 1;
    else
      prime[i + 1] = prime[i];
  }
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    pair<int, int> ans;
    ans = MP(-1, 1);
    REP(i, V.size()) {
      if (m / V[i] <= 1)
        break;
      int p = V[i];
      int q[3];
      q[0] = p;
      q[1] = prime[m / V[i]];
      q[2] = prime[min(SIZE, p * b / a)];
      REP(j, 3) {
        if (q[j] >= p && p * b >= q[j] * a && q[j] * p <= m) {
          if (ans.fir * ans.sec < p * q[j])
            ans = MP(p, q[j]);
        }
      }
    }
    cout << ans.fir << " " << ans.sec << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
int SIZE = 1e5;
int main() {
  int prime[SIZE + 1];
  vector<int> V;
  memset(prime, 1, sizeof(prime));
  prime[0] = prime[1] = 0;
  REP(i, SIZE + 1) {
    if (prime[i]) {
      for (int j = i * 2; j <= SIZE; j += i)
        prime[j] = 0;
      V.PB(i);
    }
  }
  REP(i, SIZE) {
    if (prime[i + 1])
      prime[i + 1] = i + 1;
    else
      prime[i + 1] = prime[i];
  }
  while (1) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m + a + b == 0)
      break;
    pair<int, int> ans;
    ans = MP(-1, 1);
    REP(i, V.size()) {
      if (m / V[i] <= 1)
        break;
      int p = V[i];
      int q[3];
      q[0] = p;
      q[1] = prime[m / V[i]];
      q[2] = prime[p * b / a];
      REP(i, 3) {
        if (q[i] >= p && p * b >= q[i] * a && q[i] * p <= m) {
          if (ans.fir * ans.sec < p * q[i])
            ans = MP(p, q[i]);
        }
      }
    }
    cout << ans.fir << " " << ans.sec << endl;
  }
}
Problem p00815 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

vector<int> g[1001];
int sz[1001], num[1001];

void Latte(int &idx, int cnt) {
  int now = idx, dd;
  num[cnt] = now;
  for (int i = 0; i < sz[now] - 1; i++) {
    cin >> dd;
    if (dd < 0) {
      --sz[num[dd + cnt]];
      g[num[dd + cnt]].push_back(now);
      g[now].push_back(num[dd + cnt]);
    } else if (dd > 0) {
      sz[++idx] = dd;
      g[now].push_back(idx);
      g[idx].push_back(now);
      Latte(idx, cnt + 1);
    } else {
      throw(0);
    }
  }
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    memset(sz, 0, sizeof(sz));
    memset(num, 0, sizeof(num));
    for (int i = 0; i < 1001; i++)
      g[i].clear();

    cin >> sz[0];
    int idx = 0;
    try {
      Latte(idx, 0);
      cin >> sz[0];
    } catch (int x) {
    };
    for (int i = 0; i <= idx; i++) {
      sort(begin(g[i]), end(g[i]));
      cout << i + 1;
      for (int j : g[i])
        cout << " " << j + 1;
      cout << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> g[1001];
int sz[1001], num[1001];

void Latte(int &idx, int cnt) {
  int now = idx, dd;
  num[now] = cnt;
  for (int i = 0; i < sz[now] - 1; i++) {
    cin >> dd;
    if (dd < 0) {
      for (int j = 0; j < now; j++) {
        if (num[j] - cnt == dd && sz[j] > 1) {
          --sz[j];
          g[j].push_back(now);
          g[now].push_back(j);
          break;
        }
      }
    } else if (dd > 0) {
      sz[++idx] = dd;
      g[now].push_back(idx);
      g[idx].push_back(now);
      Latte(idx, cnt + 1);
    } else {
      break;
    }
  }
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    memset(sz, 0, sizeof(sz));
    memset(num, 0, sizeof(num));
    for (int i = 0; i < 1001; i++)
      g[i].clear();

    cin >> sz[0];
    int idx = 0;
    Latte(idx, 0);
    for (int i = 0; i <= idx; i++) {
      sort(begin(g[i]), end(g[i]));
      cout << i + 1;
      for (int j : g[i])
        cout << " " << j + 1;
      cout << endl;
    }
    cin >> sz[0];
  }
}
Problem p00815 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> record;
vector<int> dist;
vector<int> deg;
vector<vector<int>> edges;
int idx;

void dfs(int v, int p, int dep) {
  dist.push_back(dep);
  int r = record[idx];
  if (0 <= p) {
    r--;
    edges.push_back({p});
    deg[p]--;
    edges[p].push_back(v);
  } else {
    edges.push_back({});
  }
  deg.push_back(r);

  while (deg[v]) {
    idx++;
    if (record[idx] < 0) {
      for (int i = 0; i < dist.size(); i++) {
        if (dist[i] == dist[v] + record[idx] && 0 < deg[i]) {
          edges[i].push_back(v);
          edges[v].push_back(i);
          deg[i]--;
          deg[v]--;
        }
      }
    } else {
      dfs(deg.size(), v, dep + 1);
    }
  }
}

void solve() {
  idx = 0;
  dfs(0, -1, 0);
  for (int i = 0; i < edges.size(); i++) {
    auto edge = edges[i];
    sort(edge.begin(), edge.end());
    cout << i + 1;
    for (auto v : edge)
      cout << " " << v + 1;
    cout << endl;
  }
}

int main() {
  int N;
  cin >> N;
  int cnt = 1;
  while (N--) {
    record.clear();
    dist.clear();
    deg.clear();
    edges.clear();

    int r;
    while (cin >> r, r)
      record.push_back(r);
    // cout << "Case:" << cnt << endl;
    solve();
    cnt++;
  }

  return 0;
}

Error: 0

Fixed submission:

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

vector<int> record;
vector<int> dist;
vector<int> deg;
vector<vector<int>> edges;
int idx;

void dfs(int v, int p, int dep) {
  dist.push_back(dep);
  int r = record[idx];
  if (0 <= p) {
    r--;
    edges.push_back({p});
    deg[p]--;
    edges[p].push_back(v);
  } else {
    edges.push_back({});
  }
  deg.push_back(r);

  while (deg[v]) {
    idx++;
    if (record[idx] < 0) {
      for (int i = 0; i < dist.size(); i++) {
        if (dist[i] == dist[v] + record[idx]) {
          edges[i].push_back(v);
          edges[v].push_back(i);
          deg[i]--;
          deg[v]--;
        }
      }
    } else {
      dfs(deg.size(), v, dep + 1);
    }
  }
}

void solve() {
  idx = 0;
  dfs(0, -1, 0);
  for (int i = 0; i < edges.size(); i++) {
    auto edge = edges[i];
    sort(edge.begin(), edge.end());
    cout << i + 1;
    for (auto v : edge)
      cout << " " << v + 1;
    cout << endl;
  }
}

int main() {
  int N;
  cin >> N;
  while (N--) {
    record.clear();
    dist.clear();
    deg.clear();
    edges.clear();

    int r;
    while (cin >> r, r)
      record.push_back(r);
    solve();
  }

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

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

const int MAX_ROOM = 110;
int N;
vector<int> E[MAX_ROOM];
queue<int> Q[MAX_ROOM];

void addEdge(int a, int b) {
  E[a].push_back(b);
  E[b].push_back(a);
}

void rec(int p, int t, int num_door, queue<int> &que) {
  for (int i = 0; i < num_door; ++i) {
    if (!Q[t].empty()) {
      addEdge(p, Q[t].front());
      Q[t].pop();
    } else {
      int a;
      a = que.front();
      que.pop();
      if (a > 0) {
        addEdge(p, ++N);
        rec(N, t + 1, a - 1, que);
      } else {
        Q[t + a].push(p);
      }
    }
  }
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    for (int i = 0; i < MAX_ROOM; ++i) {
      E[i].clear();
      while (!Q[i].empty())
        Q[i].pop();
    }
    int a;
    queue<int> que;
    while (cin >> a && a)
      que.push(a);

    a = que.front();
    que.pop();
    N = 1;
    rec(1, 0, a, que);

    for (int i = 1; i <= N; ++i) {
      cout << i;
      sort(E[i].begin(), E[i].end());
      for (int j = 0; j < E[i].size(); ++j)
        cout << " " << E[i][j];
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int MAX_ROOM = 110;
int N;
vector<int> E[MAX_ROOM];
queue<int> Q[MAX_ROOM];

void addEdge(int a, int b) {
  E[a].push_back(b);
  E[b].push_back(a);
}

void rec(int p, int t, int num_door, stringstream &ss) {
  for (int i = 0; i < num_door; ++i) {
    if (!Q[t].empty()) {
      addEdge(p, Q[t].front());
      Q[t].pop();
    } else {
      int a;
      if (!(ss >> a) || a == 0)
        return;
      if (a > 0) {
        addEdge(p, ++N);
        rec(N, t + 1, a - 1, ss);
      } else {
        Q[t + a].push(p);
      }
    }
  }
}

int main() {
  int t;
  cin >> t;
  getchar();
  while (t--) {
    for (int i = 0; i < MAX_ROOM; ++i) {
      E[i].clear();
      while (!Q[i].empty())
        Q[i].pop();
    }
    string line;
    getline(cin, line);
    stringstream ss(line);
    int a;
    ss >> a;
    N = 1;
    rec(1, 0, a, ss);
    for (int i = 1; i <= N; ++i) {
      cout << i;
      sort(E[i].begin(), E[i].end());
      for (int j = 0; j < E[i].size(); ++j)
        cout << " " << E[i][j];
      cout << endl;
    }
  }
  return 0;
}