Pagini recente » Cod sursa (job #145063) | Cod sursa (job #1405218) | Cod sursa (job #3179146) | Cod sursa (job #1995062) | Cod sursa (job #1168292)
#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){
int tata = x;
while ( T[ tata ] != tata )
tata = T[tata];
while ( T[x] != x ){
x = T[x];
T[x] = tata;
}
return tata;
}
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;
}