Pagini recente » Cod sursa (job #2545331) | Cod sursa (job #1660076) | Cod sursa (job #2072052) | Cod sursa (job #2468740) | Cod sursa (job #3192232)
#pragma GCC optimize("unroll-loops")
#include <stream>
using namespace std;
#define NMAX 100001
int tatic[NMAX];
int getRoot(int node){
if(tatic[node] == node)return node;
else return getRoot(tatic[node]);
}
void uniteTrees(int left, int right){
int x = getRoot(left), y = getRoot(right);
if(x != y){
tatic[x] = y;
}
}
int main(void){
ofstream cout("disjoint.out");
ifstream cin("disjoint.in");
int n, Q;
cin >> n >> Q;
for(int i = 1;i<=n;i++)tatic[i] = i;
while(Q--){
int x,y,z;
cin >> x >> y >> z;
if(x == 1){
/// we need to unite y, z
uniteTrees(y,z);
}else{
int root1 = getRoot(y), root2 = getRoot(z);
if(root1 == root2)cout << "DA" << '\n'; /// apartine aceluiasi arbore avand acelasi tatic
else cout << "NU" << '\n'; /// e negru
}
}
}