Pagini recente » Cod sursa (job #456733) | Cod sursa (job #309163) | Cod sursa (job #1597850) | Cod sursa (job #2339928) | Cod sursa (job #2021506)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int TT[100005], n, Q, cod, x, y;
int root(int nod)
{
int R = nod, next;
while(TT[R] != R) R = TT[R];
while(TT[nod] != nod)
{
next = TT[nod];
TT[nod] = R;
nod = next;
}
return R;
}
void unite(int x, int y)
{
TT[y] = x;
}
int main()
{
f >> n >> Q;
for(int i = 1; i <= n; ++i) TT[i] = i;
for(int i = 1; i <= Q; ++i)
{
f >> cod >> x >> y;
if(cod == 1) unite(root(x), root(y));
else
{
if(root(x) == root(y)) g << "DA\n";
else g << "NU\n";
}
}
return 0;
}