Cod sursa(job #1399558)

Utilizator rangerChihai Mihai ranger Data 24 martie 2015 20:08:39
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include<fstream>
#include<string>
#include<vector>
using namespace std;

const int N = 100003;

int n,m,op,x,y,p[N],i;

int find(int x)
{
    if (x==p[x]) return x;
    return  (p[x]=find(p[x]));
}

int main()
{
    ifstream cin("disjoint.in");
    ofstream cout("disjoint.out");

    cin>>n>>m;
    for(i=1;i<=n;i++)
        p[i]=i;

    while (m--)
    {
        cin>>op>>x>>y;

        int i=find(x), j=find(y);
        if (op==1) p[i]=j;
         else {
            string str=(i==j)?"DA":"NU";
            cout<<str<<'\n';
         }
    }
    return 0;
}