Pagini recente » Cod sursa (job #381251) | Cod sursa (job #2879847) | Cod sursa (job #2583802) | Cod sursa (job #3287551) | Cod sursa (job #2968008)
#include <fstream>
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int n, m, tata[1010];
int gaseste (int nod)
{
if (nod != tata[nod])
tata[nod] = gaseste(tata[nod]);
return tata[nod];
}
void unire(int nod1, int nod2) {
int tata1 = gaseste(nod1);
int tata2 = gaseste(nod2);
tata[tata1] = tata2;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
tata[i] = i;
for (int i = 1; i <= m; i++)
{
int tip, x, y;
cin >> tip >> x >> y;
if (tip == 1) {
unire(x, y);
}
else {
if (gaseste(x) == gaseste(y)) cout << "DA" << endl;
else cout << "NU" << endl;
}
}
return 0;
}