Cod sursa(job #2455664)

Utilizator AndreiJJIordan Andrei AndreiJJ Data 12 septembrie 2019 11:52:27
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.27 kb
#include <fstream>
#include <vector>
#include <iostream>

#define pb push_back
using namespace std;

ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");

const int N = 255;
int t[N * N], c[N * N], n, maxc;
vector <int> sol;
pair <int, int> b[N];

void Read ()
{
    fin >> n;
    for (int i = 1; i <= n * n; i++)
    {
        int x, y;
        fin >> x >> y;
        b[i] = {x, y};
    }
}

inline int Convert (int x, int y)
{
    return n * (x - 1) + y;
}

void Union (int x, int y)
{
    t[y] = x;
    c[x] += c[y];
    maxc = max(maxc, c[x]);
}

int Find (int x)
{
    int root, y;
    root = x;
    while (t[root] != 0)
        root = t[root];
    /// comprimare drum
    while (x != root)
    {
        y = t[x];
        t[x] = root;
        x = y;
    }
    return root;

}

void Solve ()
{
    int i, x;
    for (i = n * n; i >= 1; i--)
    {
        bool OK = true;
        sol.pb(maxc);
        x = Convert(b[i].first, b[i].second);
        cout << "8\n";
        if (x <= n)
        {
            if (t[x + 1] || t[x - 1] || t[x + n])
                OK = false;
        }
        else if (x > n && x <= n * (n - 1))
        {
            if (t[x + 1] || t[x - 1] || t[x + n] || t[x - n])
                OK = false;
        }
        else
            if (t[x + 1] || t[x - 1] || t[x - n])
                OK = false;
        if (OK)
        {
            maxc = max(maxc, 1);
            c[x] = 1;
            t[x] = x;
        }
        else
        {
            int k, j;
            if (x - n > 0)
            {
                j = Find (x - n);
                Union(x, j);
            }
            k = Find (x);
            if (x % n != 1)
            {
                j = Find (x - 1);
                Union(k, j);
            }
            k = Find (x);
            if (x % n)
            {
                j = Find (x + 1);
                Union(k, j);
            }
            k = Find (x);
            if (x + n <= n * n)
            {
                j = Find (x + n);
                Union(k, j);
            }
        }
    }
    for (int i = sol.size() - 1; i >= 0; i--)
        fout << sol[i] << "\n";
}

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