Pagini recente » Cod sursa (job #492664) | Cod sursa (job #11485) | Cod sursa (job #1134393) | Cod sursa (job #2420957) | Cod sursa (job #3181719)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int maxDim = 1e5 + 5;
int root[maxDim] , sizeS[maxDim];
int N , M;
class DSU
{
public:
void Init()
{
for(int i = 1; i <= maxDim; ++i)
{
root[i] = i;
sizeS[i] = 1;
}
}
int Find (int N)
{
if(root[N] == N)
return N;
else
{
int x = Find (root[N]);
root[N] = x;
return x;
}
}
void Union (int A , int B)
{
int rootA = Find(A);
int rootB = Find(B);
if(rootA != rootB)
{
if(sizeS[rootA] > sizeS[rootB])
root[rootB] = rootA;
else
{
root[rootA] = rootB;
if(sizeS[rootA] == sizeS[rootB])
++sizeS[rootB];
}
}
}
}Sol;
int main()
{
int op , x , y;
fin >> N >> M;
Sol.Init();
while(M --)
{
fin >> op >> x >> y;
if(op == 1)
Sol.Union(x , y);
else
{
bool ans = (Sol.Find(x) == Sol.Find(y));
if(ans)
fout << "DA\n";
else
fout << "NU\n";
}
}
}