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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF 0x3f3f3f3f
#define PI 3.141592653579
#define FRER() freopen("input.txt", "r", stdin);
#define FREW() freopen("output.txt", "w", stdout);
#define QIO std::ios::sync_with_stdio(false)
#define mem(a, b) memset(a, b, sizeof(a))
const double eps = 1e-8;
typedef long long ll;
const int maxn = 30;
int cnt, n;
ll ans;
int x[maxn], y[maxn];
bool vis[maxn];
struct Vec {
  int x, y;
} vec[maxn * maxn];
int res, cntl;

bool isping(Vec a, Vec b) {
  if (a.x * b.y - b.x * a.y == 0)
    return true;
  return false;
}

void dfs(int cur) {
  //    while(cur < n && vis[cur]) cur++;
  if (cur == n) {
    int cntp = 0;
    for (int i = 0; i < cntl; i++) {
      for (int j = i + 1; j < cntl; j++) {
        //                cout << /"===" << endl;
        if (isping(vec[i], vec[j])) {
          //                    cout << "yes" << endl;
          cntp++;
        }
      }
    }
    res = max(res, cntp);
    return;
  }
  if (vis[cur])
    dfs(cur + 1);
  else {
    vis[cur] = true;
    for (int i = cur + 1; i < n; i++) {
      if (vis[i] || i == cur)
        continue;
      vis[i] = true;
      vec[cntl].x = x[i] - x[cur];
      vec[cntl++].y = y[i] - y[cur];
      dfs(cur + 1);
      vis[i] = false;
      cntl--;
    }
    vis[cur] = false;
  }
}

int main() {
  //    FRER();
  mem(vis, false);
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &x[i], &y[i]);
  res = 0;
  cntl = 0;
  dfs(0);
  printf("%d\\n", res);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF 0x3f3f3f3f
#define PI 3.141592653579
#define FRER() freopen("input.txt", "r", stdin);
#define FREW() freopen("output.txt", "w", stdout);
#define QIO std::ios::sync_with_stdio(false)
#define mem(a, b) memset(a, b, sizeof(a))
const double eps = 1e-8;
typedef long long ll;
const int maxn = 30;
int cnt, n;
ll ans;

struct Point {
  int x, y;
  int cur;
} point[maxn];

struct Vec {
  Point p[2]; // 起始点和终点
  int hav;
} vec[maxn * maxn];

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int _hash(Point a, Point b) {
  int x = a.x - b.x;
  int y = a.y - b.y;
  if (x < 0) {
    x = -x;
    y = -y;
  }
  int gc = gcd(abs(x), abs(y));
  x = x / gc;
  y = y / gc + 2000;
  return x * 10000 + y;
}

map<int, int> m;
bool vis[maxn * maxn];
bool visp[maxn];
set<int> st; // 在一个集合的所有点的idx

void dfs(int cur) {
  //    cout << "nnn" <<  n << endl;
  //    cout << "==========" << endl;
  //    cout << cur << n/2 << endl;
  if (cur == n / 2) {
    //        cout << "===fsgdgsdg=======" << endl;
    ll sum = 0;
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
      sum += (it->second) * (it->second - 1) / 2;
    }
    ans = max(ans, sum);
    return;
  }
  for (int i = 0; i < cnt; i++) {
    if (!vis[i] && !visp[vec[i].p[0].cur] && !visp[vec[i].p[1].cur]) {
      vis[i] = true;
      visp[vec[i].p[0].cur] = true;
      visp[vec[i].p[1].cur] = true;
      m[vec[i].hav]++;
      dfs(cur + 1);
      visp[vec[i].p[0].cur] = false;
      visp[vec[i].p[1].cur] = false;
      m[vec[i].hav]--;
      vis[i] = false;
    }
  }
}

int main() {
  //    FRER();

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &point[i].x, &point[i].y);
    point[i].cur = i;
  }
  cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      vec[cnt].hav = _hash(point[i], point[j]);
      vec[cnt].p[0] = point[i];
      vec[cnt].p[1] = point[j];
      cnt++;
    }
  }
  //    cout <<n <<"fsafsadfas" << endl;
  ans = -134566;
  memset(vis, false, sizeof(vis));
  memset(visp, false, sizeof(visp));
  dfs(0);
  cout << ans << endl;
  return 0;
}