Pagini recente » Cod sursa (job #2638022) | Borderou de evaluare (job #2223193) | Cod sursa (job #3131200) | Cod sursa (job #601373) | Cod sursa (job #2062582)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMax = 100000;
int TT[NMax + 5],R[NMax + 5];
int N,M;
int Root(int x)
{
while(TT[x] != x)
x = TT[x];
return x;
}
void Unite(int x, int y)
{
if(R[x] < R[y])
TT[x] = y;
if(R[x] > R[y])
TT[y] = x;
if(R[x] == R[y])
{
TT[y] = x;
R[x]++;
}
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= N; ++i)
TT[i] = i;
while(M--)
{
int op,x,y;
fin >> op >> x >> y;
if(op == 1)
Unite(Root(x),Root(y));
if(op == 2)
if(Root(x) == Root(y))
fout << "DA\n";
else
fout << "NU\n";
}
return 0;
}