Cod sursa(job #1800744)

Utilizator vladm98Munteanu Vlad vladm98 Data 7 noiembrie 2016 23:48:09
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.67 kb
#include <fstream>

using namespace std;
int tata[65000], n, i, j, card[65000], m,  x, y, verif[251][251], maxim, raspuns[65000];
int stapan (int nod)
{
    int rege = nod, copie;
    while (tata[rege] != rege)
        rege = tata[rege];
    while (tata[nod] != nod)
    {
        copie = nod;
        nod = tata[nod];
        tata[copie] = rege;
    }
    return rege;
}
void unire (int a, int b)
{
    int st1 = stapan(a), st2 = stapan(b);
    if (st2 == st1)
        return ;
    if (card[st1] > card[st2])
    {
        card[st1]+=card[st2];
        tata[st2] = st1;
        if (maxim < card[st1])
            maxim = card[st1];
    }
    else
    {
        card[st2] += card[st1];
        tata[st1] = st2;
        if (maxim < card[st2])
            maxim = card[st2];
    }
}
struct bila
{
    int x, y;
}pereche[65000];
int main()
{
    ofstream fout ("bile.out");
    ifstream fin ("bile.in");
    fin >> n;
    m = n*n;
    for (i=1; i<=m; ++i)
    {
        tata[i] = i;
        card[i] = 1;
        fin >> x >> y;
        pereche[i].x = x;
        pereche[i].y = y;
    }
    for (i = m; i>=1; --i)
    {
        raspuns[i] = maxim;
        x = pereche[i].x;
        y = pereche[i].y;
        verif[x][y] = 1;
        if (card[(x-1)*n+y] > maxim)
            maxim = 1;
        if (verif[x-1][y])
            unire((x-2)*n+y, (x-1)*n+y);
        if (verif[x][y+1])
            unire((x-1)*n+y+1, (x-1)*n+y);
        if (verif[x+1][y])
            unire(x*n+y, (x-1)*n+y);
        if (verif[x][y-1])
            unire((x-1)*n+y-1, (x-1)*n+y);
    }
    for (i = 1; i<=m; ++i)
        fout << raspuns[i] << '\n';
    return 0;
}