Cod sursa(job #1132804)

Utilizator romircea2010FMI Trifan Mircea Mihai romircea2010 Data 3 martie 2014 22:12:36
Problema Overlap Scor 76
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.76 kb
#include <fstream>
#include <vector>

using namespace std;

const int sinus[] = {0, 1, 0, -1};
const int cosinus[] = {1, 0, -1, 0};
const int NMax = 810, Xhash = 13, Yhash = 17, MOD = 666013;
int ans[NMax];
int viz[NMax];
int G[NMax];
int gradext[NMax], gradint[NMax];;
int n;
struct punct
{
    int x, y, ind;
    punct() {x = y = 0;}
    punct(const int x, const int y, const int ind)
    {
        this -> x = x;
        this -> y = y;
        this -> ind = ind;
    }
    bool operator < (const punct &other) const
    {
        if (x == other.x)
            return y < other.y;
        return x < other.x;
    }
};
vector <punct> H[MOD];
punct a[NMax];

void Read()
{
    ifstream f ("overlap.in");
    f>>n;
    for (int i = 1; i<=n; ++i)
    {
        int x, y; f>>x>>y;
        a[i] = punct(x, y, i);
        int codh = ((x < 0 ? -x : x) * Xhash + (y < 0 ? -y : y) * Yhash) % MOD;
        H[codh].push_back(a[i]);
    }
    f.close();
}

void Rotate90(punct &other)
{
    int newx = other.x * cosinus[1] - other.y * sinus[1];
    int newy = other.x * sinus[1] + other.y * cosinus[1];
    other = punct(newx, newy, other.ind);
}

inline int DFS(const int &node, const int &type)
{
    if (node == 0 || viz[node])
        return 0;
    viz[node] = 1;
    ans[node] = type;
    return 1 + DFS(G[node], 3 - type);
}

void Solve()
{
    for (int k = 0; k < 4; ++k)
    {
        punct start = a[1];
        for (int ind = 1; ind <= k; ++ind) Rotate90(start);
        for (int i = 2; i<=n; ++i)
        {
            int shift_x = a[i].x - start.x, shift_y = a[i].y - start.y;
            gradext[1] = G[1] = 0;
            ++gradext[1];
            G[1] = i;
            ++gradint[i];
            for (int j = 2; j <= n; ++j)
            {
                punct now = a[j];
                for (int ind = 1; ind <= k; ++ind) Rotate90(now);
                now.x += shift_x; now.y += shift_y;
                int codh = ((now.x < 0 ? -now.x : now.x) * Xhash + (now.y < 0 ? -now.y : now.y) * Yhash) % MOD;
                gradext[j] = G[j] = 0;
                for (vector <punct> :: iterator it = H[codh].begin(); it!=H[codh].end(); ++it)
                    if ((*it).x == now.x && (*it).y == now.y)
                    {
                        G[j] = (*it).ind;
                        ++gradext[j];
                        ++gradint[(*it).ind];
                        break;
                    }
            }
            bool stop = false;
            for (int j = 1; j<=n && !stop; ++j)
            {
                if(!viz[j] && gradext[j] != 0 && gradint[j] != 0)
                {
                    int nr = DFS(j, 1);
                    if (nr & 1)
                    {
                        stop = true;
                        break;
                    }
                }
            }
            for (int j = 1; j<=n && !stop; ++j)
            {
                if(!viz[j] && gradext[j] != 0 && gradint[j] == 0)
                {
                    int nr = DFS(j, 1);
                    if (nr & 1)
                    {
                        stop = true;
                        break;
                    }
                }
            }
            for (int j = 1; j <= n && !stop; ++j)
                if (!viz[j])
                    stop = true;
            if (!stop)
            {
                ofstream g("overlap.out");
                for (int i = 1; i<=n; ++i)
                    g<<ans[i]<<"\n";
                g.close();
                return ;
            }
            for (int ind = 1; ind <= n; ++ind)
                gradint[ind] = ans[ind] = viz[ind] = gradext[ind] = G[ind] = 0;
        }
    }
}

int main()
{
    Read();
    Solve();
    return 0;
}