Pagini recente » Cod sursa (job #645109) | Cod sursa (job #1984157)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMax = 100005;
int N,M;
int TT[NMax],R[NMax];
int Root(int x)
{
while(x != TT[x])
x = TT[x];
return x;
}
void Unite(int x, int y)
{
if(R[x] < R[y]){
TT[x] = y;
R[y] += R[x]
}
if(R[x] > R[y]){
TT[y] = x;
R[x] += R[y]
}
if(R[x] == R[y])
{
TT[x] = y;
R[y] += R[y]
}
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= N; ++i){
TT[i] = i;
R[i] = 1;
}
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;
}