Cod sursa(job #3201739)

Utilizator arinaststsArina Stroe arinaststs Data 9 februarie 2024 18:00:57
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, vt[100005];
int main()
{
    fin>>n>>m;
    for(int i=0;i<m; i++)
    {
        int c, x, y;
        fin>>c>>x>>y;
        if(c==2)
            if((vt[x]||vt[y])&&(vt[x]==vt[y]||(vt[x]==y||vt[y]==x)))
            fout<<"DA"<<'\n';
            else fout<<"NU"<<'\n';
        else{
            vt[y]=x;
            for(int j=y; j!=-1; j=vt[j])
            {
                if(vt[j]==0)
                    {
                        vt[y]=j;
                        for(int k=1; k<=n; k++)
                            if(vt[k]==y)
                            vt[k]=j;
                        break;
                    }
            }
        }
    }
    return 0;
}