Cod sursa(job #1328948)

Utilizator smaraldaSmaranda Dinu smaralda Data 28 ianuarie 2015 21:42:56
Problema Overlap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.34 kb
#include<stdio.h>
#include<set>
#include<string.h>
using namespace std;

const int NMAX = 805;

struct POINT {
    int x, y, ind;

    POINT (int x = 0, int y = 0, int ind = 0) {
        this->x = x;
        this->y = y;
        this->ind = ind;
    }

    void read(int ind) {
        scanf("%d%d", &x, &y);
        this->ind = ind;
    }

    bool operator < (POINT other) const {
        if(other.x == x)
            return y < other.y;
        return x < other.x;
    }

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

set <POINT> p;
set <POINT>::iterator it;
POINT orig[NMAX], rotated[NMAX];
int xShift, yShift, cnt, n, edge[NMAX], subset[NMAX], degIn[NMAX];

void build (int pt, int val) {
    if(pt == 0 || subset[pt])
        return;
    ++ cnt;
    subset[pt] = val;
    build(edge[pt], 3 - val);
}

bool ok () {
    int i, num;
    POINT pt;

    memset(edge, 0, sizeof(edge));
    memset(subset, 0, sizeof(subset));
    memset(degIn, 0, sizeof(degIn));
    for(i = 1; i <= n; ++ i) {
        it = p.find(POINT(rotated[i].x + xShift, rotated[i].y + yShift));
        if(it != p.end()) {
            edge[i] = it->ind;
            degIn[it->ind] = 1;
        }
    }

    for(i = 1; i <= n; ++ i)
        if(!degIn[i]) {
            cnt = 0;
            build(i, 1);

            if(cnt % 2 == 1)
                return 0;
        }

    for(i = 1; i <= n; ++ i)
        if(!subset[i]) {
            cnt = 0;
            build(i, 1);

            if(cnt % 2 == 1)
                return 0;
        }

    return 1;
}

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

    scanf("%d", &n);
    for(i = 1; i <= n; ++ i) {
        orig[i].read(i);
        p.insert(orig[i]);

        rotated[i] = orig[i];
    }

    for(nRot = 0; nRot <= 3; ++ nRot) {
        for(i = 1; i <= n; ++ i)
            rotated[i] = POINT(-rotated[i].y, rotated[i].x, i);

        for(i = 2; i <= n; ++ i) {
            xShift = orig[i].x - rotated[1].x;
            yShift = orig[i].y - rotated[1].y;

            if(ok()) {
                for(i = 1; i <= n; ++ i)
                    printf("%d\n", subset[i]);
                return 0;
            }
        }
    }
    return 0;
}