Cod sursa(job #1468464)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 6 august 2015 10:07:07
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include<fstream>
#include<time.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int *sp;
int findsp(int x)
{
    if(sp[x]==x)
      return x;
    else
      return sp[x]=findsp(sp[x]);
}
void unite(int x,int y)
{
    int tx,ty;
    tx=findsp(x);
    ty=findsp(y);
    sp[tx]=ty;
}
int main()
{
    int n,m,o,x,y,i;
    f>>n;
    f>>m;
    sp=new int[n+1];
    for(i=1;i<=n;i++)
      sp[i]=i;
    while(f>>o>>x>>y)
    {
        if(o==1)
          unite(x,y);
        if(o==2)
        {
            int tx=findsp(x);
            int ty=findsp(y);
            if(tx==ty)
              g<<"DA";
            else
              g<<"NU";
            g<<"\n";
        }
    }
    return 0;
}