Cod sursa(job #1021333)

Utilizator florin.elfusFlorin Elfus florin.elfus Data 3 noiembrie 2013 18:15:55
Problema Overlap Scor 44
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <stdio.h>
#include <algorithm>
#include <map>
#include <string.h>
#define x first
#define y second
#define point pair <int, int>

using namespace std;

point x[811];
map <point, int> M;
int go[811], deg[811], vis[811], sol[811];

void rot90(point &X) {
    swap(X.x, X.y);
    X.x = -X.x;
}

int dfs(int nod, int c) {
    if (nod == 0 || vis[nod])
        return 0;
    vis[nod] = 1;
    sol[nod] = c;
    return 1 + dfs(go[nod], 1 - c);
}

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

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

    for (int rot = 0; rot < 4; ++rot)
        for (int i = 1; i <= N; ++i)
            if (x[i] != x[1]) {
                point tmp = x[1];
                for (int r = 1; r <= rot; ++r)
                    rot90(tmp);
                int dx = x[i].x - tmp.x, dy = x[i].y - tmp.y;
                memset(go, 0, sizeof(go));
                memset(deg, 0, sizeof(deg));
                memset(vis, 0, sizeof(vis));
                for (int j = 1; j <= N; ++j) {
                    point tmp = x[j];
                    for (int r = 1; r <= rot; ++r)
                        rot90(tmp);
                    tmp.x += dx; tmp.y += dy;
                    go[j] = M[tmp];
                    ++deg[M[tmp]];
                }
                bool solution = true;
                for (int j = 1; j <= N && solution; ++j)
                    if (deg[j] == 0 && dfs(j, 1) % 2)
                        solution = false;
                for (int j = 1; j <= N && solution; ++j)
                    if (deg[j] > 0 && dfs(j, 1) % 2)
                        solution = false;
                if (solution) {
                    for (int j = 1; j <= N; ++j)
                        printf("%d\n", sol[j] + 1);
                    return 0;
                }
            }

    return 0;
}