Cod sursa(job #1453803)

Utilizator SmarandaMaria Pandele Smaranda Data 24 iunie 2015 18:36:45
Problema Overlap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.23 kb
#include <cstdio>
#include <unordered_map>
#include <cstring>
#include <vector>

using namespace std;

const int N = 801, B = 100001, MOD = 666013;

struct Point {
    int x, y, ind;

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

class MyComp {
public :
    bool operator () (const Point &A, const Point &B) {
        return A.x < B.x || (A.x == B.x && A.y < B.y);
    }
};

Point P [N], Pinit [N];
unordered_map <int, int> h;
vector <int> graph [N];
int n, num, ok;
int used [N], grad_int [N], grad_ext [N];
int ans [N];

void rotire () {
    int i, aux;

    for (i = 1; i <= n; i ++) {
        aux = P [i].x;
        P [i].x = P [i].y;
        P [i].y = -aux;
    }
}

void translatie (const int &k) {
    int a, b, i;

    a = Pinit [k].x - P [1].x;
    b = Pinit [k].y - P [1].y;
    for (i = 1; i <= n; i ++) {
        P [i].x += a;
        P [i].y += b;
    }
}

void reversetranslatie (const int &k) {
    int a, b, i;

    a = P [k].x - P [1].x;
    b = P [k].y - P [1].y;
    for (i = 1; i <= n; i ++) {
        P [i].x -= a;
        P [i].y -= b;
    }
}

void dfs (int x) {
    vector <int> :: iterator it;

    used [x] = 1;
    num ++;
    for (it = graph [x].begin (); it != graph [x].end (); ++ it)
        if (!used [*it])
            dfs (*it);
}

void dfs2 (int x) {
    vector <int> :: iterator it;

    used [x] = 2;
    num ++;
    for (it = graph [x].begin (); it != graph [x].end (); ++ it)
        if (used [*it] != 2) {
            if (ans [x] == 2)
                ans [*it] = 1;
            else ans [*it] = 2;
            dfs2 (*it);
        }
}

void solve () {
    int i, j, k, s, numg, temp;
    unordered_map <int, int> :: iterator jt;

    // P [1]
    for (i = 2; i <= n; i ++) {
        translatie (i);
        s = 0;
        graph [P [1].ind].push_back (Pinit [i].ind);
        grad_int [Pinit [i].ind] ++;
        grad_ext [P [1].ind] ++;
        for (j = 2; j <= n; j ++) {
            temp = (P [j].x * B + P [j].y) % MOD;
            jt = h.find (temp);
            if (jt != h.end ()) {
                    graph [P [j].ind].push_back ((*jt).second);
                    grad_int [(*jt).second] ++;
                    grad_ext [P [j].ind] ++;
                }
        }
        for (j = 1; j <= n; j ++)
            if (!used [j] && grad_int [j] == 0)  {
                num = 0;
                dfs (j);
                if (num % 2 == 0)
                    s = s + num / 2;
            }
        for (j = 1; j <= n; j ++)
            if (!used [j]) {
                num = 0;
                dfs (j);
                if (num % 2 == 0)
                    s = s + num;
            }
        memset (used, 0, sizeof (used));
        reversetranslatie (i);
        if (s >= n / 2) {
            for (j = 1; j <= n; j ++)
                if (!used [j] && grad_int [j] == 0) {
                    num = 0;
                    dfs (j);
                    if (num % 2 == 0) {
                        ans [j] = 2;
                        dfs2 (j);
                    }
                }
            for (j = 1; j <= n; j ++)
                if (!used [j]) {
                    num = 0;
                    dfs (j);
                    if (num % 2 == 0) {
                        ans [j] = 2;
                        dfs2 (j);
                    }
                }
            for (j = 1; j <= n; j ++) {
                printf ("%d\n", ans [j]);
                ok = 0;
            }
            return;
        }

        for (j = 1; j <= n; j ++) {
            grad_int [j] = grad_ext [j] = 0;
            graph [j].clear ();
        }
    }
}

int main () {
    int i, temp;

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

    scanf ("%d", &n);
    ok = 1;
    for (i = 1; i <= n; i ++) {
        scanf ("%d%d", &P [i].x, &P [i].y);
        P [i].ind = i;
        Pinit [i] = P [i];
        temp = (Pinit [i].x * B + Pinit [i].y) % MOD;
        h.insert (make_pair (temp, Pinit [i].ind));
    }
    for (i = 0; i <= 3; i ++) {
        solve ();
        if (ok == 0)
            break;
        rotire ();
    }
    return 0;
}