Pagini recente » Cod sursa (job #2800520) | Cod sursa (job #1690970) | Cod sursa (job #3040698) | Cod sursa (job #702258) | Cod sursa (job #2811740)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, x, y, t;
int T[100005];
int get_root(int nod)
{
int tmp = nod;
while(T[nod] > 0)
nod = T[nod];
int root = nod;
nod = tmp;
while(nod != root)
{
tmp = T[nod];
T[nod] = root;
nod = tmp;
}
return root;
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; i++)
T[i] = -1;
for(int i = 1; i <= m; i++)
{
fin >> t >> x >> y;
x = get_root(x);
y = get_root(y);
if(t == 1)
{
if(x == y)
continue;
if(T[x] < T[y])
{
T[x] += T[y];
T[y] = x;
}
else
{
T[y] += T[x];
T[x] = y;
}
}
else
{
if(x == y)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}