Cod sursa(job #1744864)

Utilizator ajeccAjechiloae Eugen ajecc Data 20 august 2016 16:58:28
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.93 kb
#include <bits/stdc++.h>
#define for0(i,n) for(int i=0; i<n; i++)
#define for1(i,n) for(int i=1; i<=n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A,x) memset(A, x, sizeof(A))
#define cpy(A,B) memcpy(A, B, sizeof(B))
#define g(s) getline(cin, s) ///ai grija la fin/cin ///
#define FASTIO ios_base::sync_with_stdio(0)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
/*template <typename T>
string to_string(const T& n){
    ostringstream os;
    os << n;
    return os.str();
}
*/

/*struct coord
{
    int x,y;

};
bool operator<(const coord &l, const coord &r)
{
    return (l.x<r.x || (l.x==r.x && l.y<r.y));
}*/

/*void invers_modular(int a, int b, int& d, int& x, int & y)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
        return ;
    }
    int x0, y0;
    invers_modular(b, a%b, d, x0, y0);
    x=y0;
    y=x0-a/b*y0;
} // daca x<0 se aduna cu mod pana e mai mare, x fiind rezultatul*/

/*ull p(int baze, int exponent)
{
    if(exponent==0)
        return 1;

    if(exponent%2==0)
        return p(baze*baze, exponent/2);
    else return baze*p(baze, exponent-1);
}*/
//ifstream fin("bellmanford.in"); /// modifica cu numele corespunzator
//ofstream fout("bellmanford.out"); /// modifica cu numele corespunzator
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int v[100001], lungime[100001];
int radacina;

void compresie(int k)
{
    while(v[k])
    {
        int aux = v[k];
        v[k] = radacina;
        k = aux;
    }
}

int main()
{
    FASTIO;
    int n, m;
    fin>>n>>m;
    for1(i, m)
    {
        int o, x, y;
        fin>>o>>x>>y;
        int mlcx = x, mlcy = y;
        if(o == 1)
        {
            while(v[x])
                x = v[x];
            while(v[y])
                y = v[y];
            if(!lungime[x])
                lungime[x] = 1;
            if(!lungime[y])
                lungime[y] = 1;

            if(lungime[x] >= lungime[y])
            {
                radacina = x;
                v[y] = x;
                lungime[x] += lungime[y];
                lungime[y] = 0;
            }
            else
            {
                radacina = y;
                v[x] = y;
                lungime[y] += lungime[x];
                lungime[x] = 0;
            }
            x = mlcx; y = mlcy;
            compresie(x); compresie(y);
        }
        else
        {
            while(v[x])
                x = v[x];
            while(v[y])
                y = v[y];
            if(x == y)
                fout<<"DA\n";
            else fout<<"NU\n";
            radacina = x;
            x = mlcx;
            compresie(x);
            radacina = y;
            y = mlcy;
            compresie(y);
        }
    }



    return 0;
}