Pagini recente » Cod sursa (job #2391646) | Cod sursa (job #2281008) | Cod sursa (job #2760882) | Cod sursa (job #629712) | Cod sursa (job #2565238)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax = 100005;
int n, m;
int p[nmax], rnk[nmax];
int root(int nod)
{
int aux = nod;
while (nod != p[nod])
{
nod = p[nod];
}
int y;
while (aux != p[aux])
{
y = p[aux];
p[aux] = nod;
aux = y;
}
return nod;
}
void unite(int x, int y)
{
if (rnk[x] > rnk[y])
p[y] = x;
else
p[x] = y;
if (rnk[x] == rnk[y])
rnk[y]++;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
{
p[i] = i;
rnk[i] = 1;
}
for (int i = 1; i <= m; i++)
{
int cerinta, x, y;
fin >> cerinta >> x >> y;
if (cerinta == 1)
unite(x, y);
else
{
if (root(x) == root(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}