Pagini recente » Cod sursa (job #1699529) | Cod sursa (job #139425) | Cod sursa (job #811482) | Cod sursa (job #964825) | Cod sursa (job #3159737)
#include <iostream>
#include <fstream>
using namespace std;
void unire(int x, int y);
int sef(int x);
int tata[100001];
int main()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int x, y, i, n, m, cod;
in >> n >> m;
for(i = 1; i <= n; i++)
{
tata[i] = i;
}
for(i = 1; i <= m; i++)
{
in >> cod >> x >> y;
if(cod == 1)
{
unire(x, y);
}
if(cod == 2)
{
int a = sef(x);
int b = sef(y);
if(a == b)
{
out << "DA" << endl;
}
else
{
out << "NU" << endl;
}
}
}
return 0;
}
void unire(int x, int y)
{
int sefx = sef(x);
int sefy = sef(y);
tata[sefy] = sefx;
}
int sef(int x)
{
if(tata[x] == x)
{
return x;
}
else
{
return tata[x] = sef(tata[x]);
}
}