Cod sursa(job #2088931)

Utilizator zanugMatyas Gergely zanug Data 16 decembrie 2017 09:01:00
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstdlib>

#define ll long long
#define pb push_back

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int N = 2e5+10;
const int M = 4e5+10;

struct edges
{
    int x, y, v;
};

int n, m;
edges x;

int par[N];
int rang[N];

int findparent(int node)
{
    if(par[node] == node)
        return node;

    par[node] = findparent(par[node]);
    return par[node];
}

void join(int x, int y)
{
    int px = findparent(x);
    int py = findparent(y);

    if(rang[px] > rang[py])
    {
        rang[py] += rang[px];
        par[px] = py;
    }
    else
    {
        rang[px] += rang[py];
        par[py] = px;
    }
}

bool IsInOneGraph(int x, int y)
{
    int px = findparent(x);
    int py = findparent(y);

    if(px == py)
        return 1;

    return 0;
}

int main()
{
    fin >> n >> m;

    for(int i = 0; i < n; ++i)
    {
        par[i] = i;
        rang[i] = 1;
    }


    for(int i = 0; i < m; ++i)
    {
        fin >> x.v >> x.x >> x.y;

        if(x.v == 1)
        {
            join(x.x, x.y);
        }
        else
        {
            bool b = IsInOneGraph(x.x, x.y);

            if(b)
                fout << "DA" << "\n";
            else
                fout << "NU" << "\n";
        }
    }

    return 0;
}