Pagini recente » Cod sursa (job #238642) | Cod sursa (job #951094) | Cod sursa (job #2644884) | Cod sursa (job #459945) | Cod sursa (job #749011)
Cod sursa(job #749011)
//Include
#include <fstream>
using namespace std;
//Constante
const int MAX_SIZE = (int)1e5+1;
//Functii
int getRoot(int node);
//Variabile
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int elements, questions;
int node1, node2;
int type;
int root[MAX_SIZE];
//Main
int main()
{
in >> elements >> questions;
while(questions--)
{
in >> type >> node1 >> node2;
if(type == 1)
root[node2] = getRoot(node1);
else
out << (getRoot(node1) == getRoot(node2)? "DA" : "NU") << '\n';
}
in.close();
out.close();
return 0;
}
int getRoot(int node)
{ return root[node]? root[node]=getRoot(root[node]) : node; }