Pagini recente » Cod sursa (job #2502068) | Cod sursa (job #249943) | Cod sursa (job #1979571) | Cod sursa (job #2000779) | Cod sursa (job #1340238)
using namespace std;
#include <fstream>
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
#define Nmax 100001
int tata[Nmax];
int find(int) ;
int main()
{
int n, m, x, y, tip;
bool rand;
fin >> n >> m;
for(; m; --m)
{
fin >> tip >> x >> y;
if(tip == 1)
{
rand = (x + y) & 1;
if(rand)
{
x = find(x); //x - radacina arborelui cu nodul x
tata[x] = y;
}
else
{
y = find(y); //nod1 - radacina arborelui cu nodul y
tata[y] = x;
}
}
else
{
if(find(x) == find(y)) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}
int find(int x)
{
if(tata[x] == 0) return x;
tata[x] = find(tata[x]);
return tata[x];
}