Pagini recente » Cod sursa (job #2976553) | Cod sursa (job #3316400) | Cod sursa (job #3312105) | Cod sursa (job #1283506) | Cod sursa (job #3326815)
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m;
int tata[100001];
int root(int nod){
while(tata[nod]>0)
nod = tata[nod];
return nod;
}
void unite(int x, int y){
if(-tata[x] < -tata[y])
swap(x,y); // y trb sa fie arborele mai mic
tata[x]+=tata[y]; // dimensiunea arborelui
tata[y] = x; // schimb tatal radacinii lui y
}
int main(){
fin>>n>>m;
for(int i = 1; i <=n; i++){
tata[i] = -1;
}
while(m--){
int op ,x ,y;
fin>>op>>x>>y;
if(op == 1){
// leg x de y
int rootx = root(x);
int rooty = root(y);
if(rootx != rooty){
unite(rootx,rooty);
}
} else {
int rootx = root(x);
int rooty = root(y);
if(rootx == rooty)
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}