Cod sursa(job #3378768)

Utilizator bogdanvladmihaiBogdan Vlad-Mihai bogdanvladmihai Data 28 noiembrie 2026 04:19:11
Problema Overlap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.43 kb
#include <cassert>
#include <ios>
#include <iostream>
#include <optional>
#include <unordered_map>
#include <vector>

struct Point {
  int x, y;

  void rotate() {
    std::swap(x, y);
    x = -x;
  }

  Point operator-(const Point& other) const {
    return {x - other.x, y - other.y};
  }
  Point operator+(const Point& other) const {
    return {x + other.x, y + other.y};
  }

  bool operator==(const Point& other) const {
    return x == other.x && y == other.y;
  }
};

std::optional<std::vector<int>>
check_solution(const std::vector<Point>& initial,
               const std::vector<Point>& points) {
  const auto n = static_cast<int>(points.size());

  struct PointHash {
    std::size_t operator()(const Point& p) const {
      return std::hash<int>()(p.x) ^ (std::hash<int>()(p.y) << 1);
    }
  };
  std::unordered_map<Point, int, PointHash> index_of;

  for (int i = 0; i < n; i++) {
    index_of[initial[i]] = i;
  }

  for (int p0 = 1; p0 < n; p0++) {
    const auto delta = initial[p0] - points[0];
    std::vector<int> next(n, -1), prev(n, -1);
    for (int i = 0; i < n; i++) {
      const auto shifted = points[i] + delta;
      const auto it = index_of.find(shifted);
      if (it != index_of.end()) {
        next[i] = it->second;
        prev[it->second] = i;
      }
    }

    std::vector<int> color(n, -1);
    bool is_bipartite = true;
    for (int i = 0; i < n; i++) {
      if (color[i] == -1) {
        int start = i;
        while (prev[start] != -1 && prev[start] != i) {
          start = prev[start];
        }
        int length = 0;
        while (start != -1 && color[start] == -1) {
          color[start] = length % 2;
          start = next[start];
          length++;
        }
        is_bipartite &= (length % 2 == 0);
      }
    }

    if (is_bipartite) {
      return color;
    }
  }

  return std::nullopt;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  assert(freopen("overlap.in", "r", stdin));
  assert(freopen("overlap.out", "w", stdout));

  int n;
  std::cin >> n;
  std::vector<Point> points(n);
  for (auto& [x, y] : points) {
    std::cin >> x >> y;
  }

  const auto initial = points;
  for (int rot = 0; rot < 4; rot++) {
    for (auto& p : points) {
      p.rotate();
    }
    const auto solution = check_solution(initial, points);
    if (solution) {
      for (int i = 0; i < n; i++) {
        std::cout << solution.value()[i] + 1 << "\n";
      }
      break;
    }
  }
  return 0;
}