Cod sursa(job #1478946)

Utilizator tudi98Cozma Tudor tudi98 Data 30 august 2015 02:29:44
Problema Overlap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <map>
#include <utility>
using namespace std;

const int Nmax = 800;

struct Point
{
	int x,y;

    Point(){}

	Point(int a,int b)
	{
		x = a;
		y = b;
	}

	Point rotate90(int times)
	{
		Point Ret = {x,y};
		for (int t = 0; t < times; ++t)
			Ret = {-Ret.y,Ret.x};
		return Ret;
	}

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

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

	bool operator<(const Point& Rhs) const
	{
	    if (Rhs.x == x)
            return y < Rhs.y;
        return x < Rhs.x;
	}
};

int N;
Point p[Nmax+1];
map<Point,int> mp;
int To[Nmax+1],From[Nmax+1];
bool seen[Nmax+1],type[Nmax+1];

bool dfs(int node,bool parity)
{
	parity ^= 1;
	type[node] = parity;
	seen[node] = 1;
	if (To[node] == -1 || seen[To[node]])
		return parity;
	dfs(To[node],parity);
}

bool Sol(int times,Point sh)
{
	for (int i = 1; i <= N; i++)
	{
		From[i] = -1;
		To[i] = -1;
	}

	map<Point,int>::iterator it;
	for (int i = 1; i <= N; i++)
	{
		it = mp.find(p[i].rotate90(times) + sh);
		if (it != mp.end())
		{
			To[i] = it->second;
			From[it->second] = i;
		}
	}

	for (int i = 1; i <= N; i++)
		seen[i] = 0;

	for (int i = 1; i <= N; i++)          // chains
		if (From[i] == -1)
		{
			bool odd = dfs(i,0);
			if (odd) return 0;
		}

	for (int i = 1; i <= N; i++)             // cycles
		if (!seen[i])
		{
			bool odd = dfs(i,0);
			if (odd) return 0;	
		}

	return 1;
}

int main()
{
	freopen("overlap.in", "r", stdin);
	freopen("overlap.out", "w", stdout);

	scanf("%d", &N);
	for (int i = 1; i <= N; i++)
	{
		scanf("%d %d", &p[i].x, &p[i].y);
		mp.insert(make_pair(p[i],i));
	}

	for (int times = 0; times < 4; times++)
	{
		for (int i = 2; i <= N; i++)
		{
			Point sh = p[i] - p[1].rotate90(times);
			if (Sol(times,sh))
			{
			    for (int i = 1; i <= N; i++)
			    	printf("%d\n", type[i]+1);
				return 0;
			}
		}
	}

}