Cod sursa(job #1809724)

Utilizator AndreiDumitrescuAndrei Dumitrescu AndreiDumitrescu Data 19 noiembrie 2016 10:52:54
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int v[100001], ap[100001], nr , n , m;

int schimba(int a , int b)
{
    int i;
    for(i = 1 ; i <= n ; i++)
    {
        if(v[i] == a)
            v[i] = b;
    }

}

int main()
{
   int  i , j , x , y;
   f >> n >> m;

   for(i = 1 ; i <= m ; i++)
   {
       f >> j >> x >> y;
       if(j == 1)
       {
           if(v[x] == 0 && v[y] == 0)
           {
               nr++;
               v[x] = nr;
               v[y] = nr;
               ap[nr] = 2;
           }
           else
           {
               if(v[x] != 0 && v[y] == 0)
               {
                   v[y] = v[x];
                   ap[v[x]]++;
               }
               else
               {
                   if(v[x] == 0 && v[y] != 0)
                   {
                       v[x] = v[y];
                       ap[v[y]]++;
                   }
                   else
                   {
                        if(v[x] != 0 && v[y] != 0)
                        {
                            if(ap[v[x]] > ap[v[y]])
                            {
                                schimba(v[y] , v[x]);
                                ap[v[x]] += ap[v[y]];
                            }
                            else
                            {
                                schimba(v[x], v[y]);
                                ap[v[y]] += ap[v[x]];
                            }
                        }
                   }
               }
           }
       }
       if(j == 2)
       {
            if(v[x] == v[y])
                g << "DA" << '\n';
            else
                g << "NU" << '\n';
       }
   }
}