Cod sursa(job #1168261)

Utilizator giminis96Pavel Stefan Cristian giminis96 Data 7 aprilie 2014 18:44:47
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <iostream>

using namespace std;

int n,m;
int *v;

inline void adauga(int x,int y)
{
    int c;
    int d;
    if(x < y)
    {
        c = v[y];
        d = v[x];
        v[y] = v[x];
    }
    else
    {
        c = v[x];
        d = v[y];
        v[x] = v[y];
    }
    int i;
    for(i = 1; i <= n; i++)
    {
        if(v[i] == c)
            v[i] = d;
    }
}

bool gaseste(int x,int y)
{
    return (v[x] == v[y]);

}

void afis()
{
    for(int i = 1; i <= n; i++)
        cout<<v[i]<<" ";
    cout<<'\n';
}

inline void citeste()
{
    ifstream in("disjoint.in");
    ofstream out("disjoint.out");
    in>>n>>m;
    v = new int[n+1];
    int i;
    for(i = 1; i <= n; i++)
        v[i] = i;
    //afis();
    //cout<<endl;
    int a,b,c;
    for(i = 0; i < m; i++)
    {
        in>>a>>b>>c;
        if(a == 1)
        {
            adauga(b,c);
            //afis();
            //cin.get();
        }
        else
        {
            if(gaseste(b,c))
                out<<"DA"<<'\n';
            else
                out<<"NU"<<'\n';
        }


    }
    in.close();
    out.close();
}

int main()
{
    citeste();
    return 0;
}