Pagini recente » Cod sursa (job #3287398) | Cod sursa (job #1408391) | Cod sursa (job #1960984) | Cod sursa (job #3223975) | Cod sursa (job #3003381)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX = 1e5 + 5;
const char nl = '\n';
int father[NMAX], n, m;
int root(int x){
if(x == father[x])
return x;
return father[x] = root(father[x]);
}
void unite(int x, int y){
father[root(x)] = root(y);
}
int main()
{
in >> n >> m;
for(int i = 1;i <= n; ++i)
father[i] = i;
for(int i = 1; i <= m; ++i){
int q, x, y;
in >> q >> x >> y;
if(q == 1){
if(root(x) != root(y))
unite(x, y);
}
else{
if(root(x) != root(y))
out << "NU" << nl;
else
out << "DA" << nl;
}
}
return 0;
}