Cod sursa(job #2651062)

Utilizator tudorbuhniaTudor Buhnia tudorbuhnia Data 21 septembrie 2020 15:47:43
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
using namespace std;
int t[100005],h[100005];
int ft(int x)
{
    int c=t[x],res=0;
    while(c!=0)
    {
        res=c;
        c=t[c];
    }
    t[x]=res;
    return res;
}
void connect(int x,int y)
{
    int n,m;
    n=ft(x);
    m=ft(y);
    t[m]=n;
}
int main()
{
    ifstream cin("disjoint.in");
    ofstream cout("disjoint.out");
    int n,m,cd,x,y;
    cin >> n >> m;
    for(int i=0;i<m;i++)
    {
        cin >> cd >> x >> y;
        if(cd==1)
        {
            connect(x,y);
        }
        else
        {
            if(ft(x)==ft(y))
                cout << "DA" << '\n';
            else
                cout << "NU" << '\n';
        }
    }
    return 0;
}