Pagini recente » Cod sursa (job #2308487) | Cod sursa (job #2833095) | Cod sursa (job #2567828) | Cod sursa (job #2289384) | Cod sursa (job #2776201)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
const int Nmax = 1e5 + 5;
int parent[Nmax], marime[Nmax];
int cauta(int x)
{
if(x == parent[x])
return x;
return parent[x] = cauta(parent[x]);
}
void unite(int x, int y)
{
int x1 = cauta(x);
int y1 = cauta(y);
if(x1 == y1)
return ;
if(marime[x1] < marime[y1])
swap(x1, y1);
parent[y1] = x1;
marime[x1] += marime[y1];
}
int main()
{
int n, q, i, j, tip, x, y;
cin >> n >> q;
for(i = 1; i <= n; i++){
parent[i] = i;
marime[i] = 1;
}
while(q){
cin >> tip >> x >> y;
if(tip == 1)
unite(x, y);
else
if(cauta(x) == cauta(y))
cout << "DA\n";
else
cout << "NU\n";
q--;
}
return 0;
}