Pagini recente » Cod sursa (job #2927258) | Cod sursa (job #2714379) | Cod sursa (job #3141498) | Cod sursa (job #1115386) | Cod sursa (job #1479405)
#include <cstdio>
#include <vector>
using namespace std;
const int nmx = 100005;
int n, m, parinte[nmx], rang[nmx];
vector <int> g[nmx];
int gaseste(int nod){
int aux = nod;
while(parinte[aux] != aux)
aux = parinte[aux];
while(parinte[nod] != nod){
int xx = parinte[nod];
parinte[nod] = aux;
nod = xx;
}
return aux;
}
void uneste(int nod1, int nod2){
if(rang[nod1] > rang[nod2])
parinte[nod2] = nod1;
else
parinte[nod1] = nod2;
if(rang[nod2] == rang[nod1])
++rang[nod2];
}
int main(){
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i){
parinte[i] = i;
rang[i] = 1;
}
for(int i = 1; i <= m; ++i){
int nod1, nod2, cerinta;
scanf("%d %d %d", &cerinta, &nod1, &nod2);
if(cerinta == 1)
uneste(gaseste(nod1),gaseste(nod2));
else
printf("%s", gaseste(nod1) == gaseste(nod2) ? "DA\n" : "NU\n");
}
return 0;
}