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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

#include <complex>
typedef complex<double> P;
#define EQ(x, y) (abs((x) - (y)) < EPS)
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }
int is_intersected_l_ls(P a1, P a2, P b1, P b2) {
  if (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS)
    return true;
  return is_point_on_line(a1, a2, b1) || is_point_on_line(a1, a2, b2);
}
int is_intersected_l_ls_strict(P a1, P a2, P b1, P b2) {
  return (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS);
}
int is_point_on_ls(P a, P b, P c) {
  // |a-c| + |c-b| <= |a-b| then true
  return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}

typedef vector<P> vp;

bool can_separate(int edge1, int edge2, int parent_o, int parent_t, vp &ours,
                  vp &theirs) {
  if (parent_o != -1) {
    if (!is_intersected_l_ls(ours[edge1], ours[edge2], ours[parent_o],
                             theirs[parent_t])) {
      return false;
    }
  }
  if (is_point_on_line(ours[edge1], ours[edge2], theirs[parent_t])) {
    return false;
  }
  if (parent_o != -1) {
    REP(i, ours.size()) {
      if (i != parent_o && i != edge1 && i != edge2) {
        if (is_intersected_l_ls_strict(ours[edge1], ours[edge2], ours[i],
                                       ours[parent_o])) {
          return false;
        }
      }
    }
  }
  REP(i, theirs.size()) {
    if (i != parent_t) {
      if (is_intersected_l_ls(ours[edge1], ours[edge2], theirs[i],
                              theirs[parent_t])) {
        return false;
      }
    }
  }
  return true;
}

namespace std {
bool operator<(const P &l, const P &r) {
  if (l.real() == r.real()) {
    return l.real() < r.real();
  } else {
    return l.imag() < r.imag();
  }
}
} // namespace std

int main() {
  int n, m;
  int cnt = 0;
  while (cin >> n >> m, n | m) {
    cnt++;
    vp black, white;
    REP(i, n) {
      int x, y;
      cin >> x >> y;
      black.push_back(P(x, y));
    }
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      white.push_back(P(x, y));
    }

    bool ok = false;
    bool all_on_line = true;
    bool scrumbled = false;
    if (n == 1 && m == 1) {
      ok = true;
      goto show;
    } else if (n == 2 && m == 1) {
      ok = !is_point_on_ls(black[0], black[1], white[0]);
      goto show;
    } else if (n == 1 && m == 2) {
      ok = !is_point_on_ls(white[0], white[1], black[0]);
      goto show;
    }

    sort(ALL(black));
    sort(ALL(white));
    REP(i, n + m) {
      P ip = (i < n ? black[i] : white[i - n]);
      if (!is_point_on_line(black[0], white[0], ip)) {
        all_on_line = false;
      }
    }
    REP(i, m) {
      if (black.size() >= 2 &&
          is_point_on_ls(black[0], black[black.size() - 1], white[i])) {
        scrumbled = true;
      }
    }
    REP(i, n) {
      if (white.size() >= 2 &&
          is_point_on_ls(white[0], white[white.size() - 1], black[i])) {
        scrumbled = true;
      }
    }
    if (all_on_line) {
      ok = !scrumbled;
      goto show;
    }

    REP(i1, n) {
      FOR(i2, i1 + 1, n) {
        int parent_o = -1;
        REP(j, n) {
          if (j != i1 && j != i2 &&
              !is_point_on_line(black[i1], black[i2], black[j])) {
            parent_o = j;
            break;
          }
        }
        if (can_separate(i1, i2, parent_o, 0, black, white)) {
          ok = true;
          goto show;
        }
      }
    }
    REP(i1, m) {
      FOR(i2, i1 + 1, m) {
        int parent_o = -1;
        REP(j, m) {
          if (j != i1 && j != i2 &&
              !is_point_on_line(white[i1], white[i2], white[j])) {
            parent_o = j;
            break;
          }
        }
        if (can_separate(i1, i2, parent_o, 0, white, black)) {
          ok = true;
          goto show;
        }
      }
    }
  show:
    cout << (ok ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

#include <complex>
typedef complex<double> P;
#define EQ(x, y) (abs((x) - (y)) < EPS)
double dot(P a, P b) { return (a.real() * b.real() + a.imag() * b.imag()); }
double cross(P a, P b) { return (a.real() * b.imag() - a.imag() * b.real()); }
int is_point_on_line(P a, P b, P c) { return EQ(cross(b - a, c - a), 0.0); }
int is_intersected_l_ls(P a1, P a2, P b1, P b2) {
  if (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < -EPS)
    return true;
  return is_point_on_line(a1, a2, b1) || is_point_on_line(a1, a2, b2);
}
int is_point_on_ls(P a, P b, P c) {
  // |a-c| + |c-b| <= |a-b| then true
  return (abs(a - c) + abs(c - b) < abs(a - b) + EPS);
}

typedef vector<P> vp;

bool can_separate(int edge1, int edge2, int parent_o, int parent_t, vp &ours,
                  vp &theirs) {
  if (parent_o != -1) {
    if (!is_intersected_l_ls(ours[edge1], ours[edge2], ours[parent_o],
                             theirs[parent_t])) {
      return false;
    }
  }
  if (parent_o != -1) {
    REP(i, ours.size()) {
      if (i != parent_o && i != edge1 && i != edge2) {
        if (is_intersected_l_ls(ours[edge1], ours[edge2], ours[i],
                                ours[parent_o])) {
          return false;
        }
      }
    }
  }
  REP(i, theirs.size()) {
    if (i != parent_t) {
      if (is_intersected_l_ls(ours[edge1], ours[edge2], theirs[i],
                              theirs[parent_t])) {
        return false;
      }
    }
  }
  return true;
}

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    vp black, white;
    REP(i, n) {
      int x, y;
      cin >> x >> y;
      black.push_back(P(x, y));
    }
    REP(i, m) {
      int x, y;
      cin >> x >> y;
      white.push_back(P(x, y));
    }

    bool ok = false;
    bool all_on_line = true;
    bool scrumbled = false;
    if (n == 1 && m == 1) {
      ok = true;
      goto show;
    } else if (n == 2 && m == 1) {
      ok = !is_point_on_ls(black[0], black[1], white[0]);
      goto show;
    } else if (n == 1 && m == 2) {
      ok = !is_point_on_ls(white[0], white[1], black[0]);
      goto show;
    }

    REP(i, n + m) {
      P ip = (i < n ? black[i] : white[i - n]);
      REP(j, n + m) {
        P jp = (j < n ? black[j] : white[j - n]);
        REP(k, n + m) {
          P kp = (k < n ? black[k] : white[k - n]);
          if (!is_point_on_line(ip, jp, kp)) {
            all_on_line = false;
          }
          if (is_point_on_ls(ip, jp, kp)) {
            if ((i < n) == (j < n) && (i < n) != (k < n)) {
              scrumbled = true;
            }
          }
        }
      }
    }
    if (all_on_line) {
      ok = !scrumbled;
      goto show;
    }

    REP(i1, n) {
      FOR(i2, i1 + 1, n) {
        int parent_o = -1;
        REP(j, n) {
          if (j != i1 && j != i2) {
            parent_o = j;
            break;
          }
        }
        if (can_separate(i1, i2, parent_o, 0, black, white)) {
          ok = true;
          goto show;
        }
      }
    }
    REP(i1, m) {
      FOR(i2, i1 + 1, m) {
        int parent_o = -1;
        REP(j, m) {
          if (j != i1 && j != i2) {
            parent_o = j;
            break;
          }
        }
        if (can_separate(i1, i2, parent_o, 0, white, black)) {
          ok = true;
          goto show;
        }
      }
    }
  show:
    cout << (ok ? "YES" : "NO") << endl;
  }
}