Cod sursa(job #1808861)

Utilizator loo_k01Luca Silviu Catalin loo_k01 Data 18 noiembrie 2016 11:48:18
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;
int n, m, opt;
int t[100001];
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

inline void Union(int x, int y)
{
    t[y] = x;
}

inline int Find(int x)
{
    int rad;
    int y;
    rad = x;
    while(t[rad] != 0)
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

int main()
{
    int k, x, y;
    fin >> n >> m;

    for(k = 1; k<=m; k++)
    {
        fin >> opt;
        fin >> x >> y;
        x = Find(x);
        y = Find(y);
        if(opt == 2)
        {
            if(x == y) fout << "DA\n";
            else fout << "NU\n";
        }
        else if (x != y) Union(x, y);
    }

    return 0;
}