Pagini recente » Cod sursa (job #35641) | Cod sursa (job #2486700) | Cod sursa (job #1870712) | Cod sursa (job #598625) | Cod sursa (job #2039007)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAX = 100005;
int p[MAX], n, m;
bool ok;
int parinte(int nod){
if(p[nod] == nod)
return nod;
return p[nod] = parinte(p[nod]);
}
int unite(int x, int y){
p[parinte(x)] = parinte(y);
}
int querry(int x, int y){
return (p[parinte(x)] == p[parinte(y)]);
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; ++i)
p[i] = i;
int c, x, y;
for(int i = 1; i <= m; ++i){
fin >> c >> x >> y;
if(c == 1)
unite(x, y);
else{
ok = querry(x, y);
if(ok == 1)
fout << "DA" << '\n';
else
fout << "NU" << '\n';
}
}
return 0;
}