Pagini recente » Clasamentul arhivei de probleme | Clasamentul arhivei de probleme | Cod sursa (job #754215) | Cod sursa (job #599297) | Cod sursa (job #2062578)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMax = 100000;
int TT[NMax + 5];
int N,M;
int Root(int x)
{
while(TT[x] != x)
x = TT[x];
return x;
}
void Unite(int x, int y)
{
TT[x] = y;
}
int main()
{
in >> N >> M;
for(int i = 1; i <= N; ++i)
TT[i] = i;
while(M--)
{
int op,x,y;
in >> op >> x >> y;
if(op == 1)
Unite(Root(x),Root(y));
if(op == 2)
if(Root(x) == Root(y))
out << "DA\n";
else
out << "NU\n";
}
return 0;
}