Problem p00886 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int d[500];
int sum[100];
int n;
vector<int> ans[1148576];
int cnt;
bool vis[1148576];
int dis[500];
int num[500];
bool check(int l, int r, int id, int val) {
  bool flag = true;
  dis[id] = val;
  for (int i = 1; i <= l; i++) {
    num[dis[id] - dis[i]]--;
    if (num[dis[id] - dis[i]] < 0) {
      flag = false;
    }
  }
  for (int i = r; i <= n; i++) {
    num[dis[i] - dis[id]]--;
    if (num[dis[i] - dis[id]] < 0) {
      flag = false;
    }
  }
  if (flag)
    return true;
  return false;
}
void dfs(int l, int r) {
  if (l == r - 1) {
    for (int i = 1; i <= n; i++) {
      ans[cnt].push_back(dis[i]);
    }
    cnt++;
    return;
  }
  int k = 1;
  while (num[d[k]] == 0)
    k++;
  if (check(l, r, l + 1, dis[n] - d[k]))
    dfs(l + 1, r);
  for (int i = 1; i <= l; i++) {
    num[dis[l + 1] - dis[i]]++;
  }
  for (int i = r; i <= n; i++) {
    num[dis[i] - dis[l + 1]]++;
  }
  if (check(l, r, r - 1, d[k]))
    dfs(l, r - 1);
  for (int i = 1; i <= l; i++) {
    num[dis[r - 1] - dis[i]]++;
  }
  for (int i = r; i <= n; i++) {
    num[dis[i] - dis[r - 1]]++;
  }
}
int main() {
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    cnt = 0;
    memset(vis, 0, sizeof(vis));
    memset(num, 0, sizeof(num));
    for (int i = 0; i < 300000; i++) {
      ans[i].clear();
    }
    for (int i = 0; i < n * (n - 1) / 2; i++) {
      scanf("%d", d + i);
      num[d[i]]++;
    }
    dis[n] = d[0];
    num[d[0]]--;
    memset(sum, 0, sizeof(sum));
    dfs(1, n);
    for (int i = 0; i < cnt; i++) {
      if (vis[i])
        continue;
      for (int j = i + 1; j < cnt; j++) {
        int len = ans[i].size();
        int k;
        for (k = 0; k < len; k++) {
          if (ans[i][k] != ans[j][k])
            break;
        }
        if (k == len) {
          vis[j] = 1;
        }
      }
    }
    // cout<<"~~"<<cnt<<endl;
    for (int i = 0; i < cnt; i++) {
      if (vis[i])
        continue;
      int len = ans[i].size();
      for (int k = 1; k < len; k++) {
        if (k == len - 1)
          printf("%d\\n", ans[i][k] - ans[i][k - 1]);
        else
          printf("%d ", ans[i][k] - ans[i][k - 1]);
      }
    }
    printf("-----\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int d[500];
int sum[100];
int n;
vector<int> ans[1148576];
int cnt;
bool vis[1148576];
bool cmp(int x, int y) { return x > y; }
bool check(int dis[]) {
  for (int i = 1; i < n; i++) {
    if (dis[i] == 0)
      return 0;
  }
  return 1;
}
inline void dfs(int idx, int dis[]) {

  if (idx >= n * (n - 1) / 2)
    return;
  if (idx < 0)
    return;
  int vist[600] = {0};
  if (idx == 0) {
    dis[n - 1] = d[idx];
    dfs(idx + 1, dis);
    return;
  }
  if (check(dis)) {
    // cout<<"??"<<endl;

    int a[500] = {0};

    int op = 0;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        a[op++] = abs(dis[j] - dis[i]);
      }
    }
    if (2 * op != (n - 1) * n)
      return;
    sort(a, a + op, cmp);

    for (int i = 0; i < op; i++) {

      if (a[i] != d[i])
        return;
    }
    for (int i = 0; i < n; i++) {
      ans[cnt].push_back(dis[i]);
    }
    cnt++;
    return;
  }
  int l = 0;
  int r = 0;
  for (int i = 1; i < n; i++) {
    if (dis[i] == 0) {
      l = i;
      break;
    }
  }
  for (int i = n - 1; i >= 1; i--) {
    if (dis[i] == 0) {
      r = i;
      break;
    }
  }
  for (int i = 0; i < l; i++) {
    for (int j = r + 1; j < n; j++) {
      vist[abs(dis[j] - dis[i])]++;
      //  cout<<abs(dis[j]-dis[i])<<" ! ";
    }
  }
  // cout<<endl;
  // cout<<d[idx]<<endl;

  int kk = 0;
  // while(d[kk]==d[idx])kk--;
  while (vist[d[kk]] > 0 && 2 * kk < n * (n - 1)) {
    // cout<<"!"<<endl;
    vist[d[kk]]--;
    kk++;
  }
  idx = kk;
  // cout<<d[idx]<<endl;
  for (int k = n - 1; k >= 1; k--) {
    if (dis[k] == 0) {
      dis[k] = d[idx];
      dfs(idx + 1, dis);
      dis[k] = 0;
      break;
    }
  }
  for (int k = 1; k < n; k++) {
    if (dis[k] == 0) {
      int u;
      dis[k] = d[0] - d[idx];
      dfs(idx + 1, dis);
      dis[k] = 0;
      break;
    }
  }

  return;
}
int main() {
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;
    cnt = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < 1100000; i++) {
      ans[i].clear();
    }
    for (int i = 0; i < n * (n - 1) / 2; i++) {
      scanf("%d", d + i);
    }
    if (d[1] == d[0]) {
      printf("-----\\n");
      continue;
    }
    memset(sum, 0, sizeof(sum));
    dfs(0, sum);
    for (int i = 0; i < cnt; i++) {
      if (vis[i])
        continue;
      for (int j = i + 1; j < cnt; j++) {
        int len = ans[i].size();
        int k;
        for (k = 0; k < len; k++) {
          if (ans[i][k] != ans[j][k])
            break;
        }
        if (k == len) {
          vis[j] = 1;
        }
      }
    }
    // cout<<"~~"<<cnt<<endl;
    for (int i = cnt - 1; i >= 0; i--) {
      if (vis[i])
        continue;
      int len = ans[i].size();
      for (int k = 1; k < len; k++) {
        if (k == len - 1)
          printf("%d\\n", ans[i][k] - ans[i][k - 1]);
        else
          printf("%d ", ans[i][k] - ans[i][k - 1]);
      }
    }
    printf("-----\\n");
  }
}