Cod sursa(job #2064894)

Utilizator ARobertAntohi Robert ARobert Data 12 noiembrie 2017 23:26:51
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;

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

int parent[100001];
priority_queue<int> p,p1,p2;

int main()
{
    int n,m,x,y,c,i;
    fin>>n>>m;
    for (i=1;i<=n;i++)
        parent[i]=i;
    for (i=1;i<=m;i++)
    {
        fin>>c>>x>>y;
        if (c==1)
        {
            while (parent[x]!=x)
            {
                p.push(x);
                x=parent[x];
            }
            while (parent[y]!=y)
            {
                p.push(y);
                y=parent[y];
            }
            p.push(x);
            p.push(y);
            int ma=min(x,y);
            while (!p.empty())
            {
                int element=p.top();
                parent[element]=ma;
                p.pop();
            }
        }
        if (c==2)
        {
            while (parent[x]!=x)
            {
                p1.push(x);
                x=parent[x];
            }
            while (parent[y]!=y)
            {
                p2.push(y);
                y=parent[y];
            }
            if (x==y)
            fout<<"DA"<<'\n';
        else fout<<"NU"<<'\n';
        while (!p1.empty())
        {
            int element=p.top();
            parent[element]=x;
            p1.pop();
        }
        while (!p2.empty())
        {
            int element=p.top();
            parent[element]=y;
            p2.pop();
        }
        }
    }
    return 0;
}