Pagini recente » Cod sursa (job #2511474) | Cod sursa (job #3140543) | Cod sursa (job #1949205) | Cod sursa (job #2754254) | Cod sursa (job #2588851)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin;
ofstream fout;
int task, number_of_tasks, n, i, j, k;
vector <int> parent (100001);
void create_disjoint(){
for (i=1; i<=n; i++)
parent[i] = i;
}
void union_of_partition (){
if (parent[k] == k)
parent[j] = parent[k];
else {
k = parent[k];
union_of_partition();
}
}
int find_element(int element){
if (parent[element] == element)
return element;
else return find_element(parent[element]);
}
int main (void){
fin.open("disjoint.in");
fout.open("disjoint.out");
fin>>n>>number_of_tasks;
create_disjoint();
for (i=1; i<=number_of_tasks; i++){
fin>>task;
switch (task)
{
case 1:
fin>>j>>k;
union_of_partition();
break;
case 2:
fin>>j>>k;
if (find_element(j) == find_element(k)) fout<<"DA\n";
else fout<<"NU\n";
break;
}
}
fin.close();
fout.close();
return 0;
}