Pagini recente » Cod sursa (job #1713648) | Cod sursa (job #2103651) | Cod sursa (job #1373755) | Cod sursa (job #1222718) | Cod sursa (job #1168288)
#include <fstream>
using namespace std;
const int N = 1 + 1e6;
const char feedback[][3] = { "NU", "DA" };
class Pmd{
int T[N];
public:
void init(int sizeMax){
for (int i = 1 ; i <= sizeMax ; i++)
T[i] = i;
}
inline int root(int x){
while (T[x] != x)
x = T[x];
return x;
}
inline bool inSameSet(int x, int y){
return root(x) == root(y);
}
void merge(int x, int y){
T[ root(y) ] = root(x);
}
};
Pmd P;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int main(){
int n, nrQ, tip, x, y;
in >> n >> nrQ;
P.init(n);
while (nrQ--){
in >> tip >> x >> y;
if (tip == 1)
P.merge(x, y);
else
out << feedback[ P.inSameSet(x, y) ] << '\n';
}
return 0;
}