Pagini recente » Cod sursa (job #718706) | Clasament a_short_round | Cod sursa (job #1618915) | Cod sursa (job #530385) | Cod sursa (job #996026)
Cod sursa(job #996026)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, p[100010];
bool sameset(int x, int y)
{
int t;
while (p[x] != p[y]) {
if (p[x] < p[y]) {
if (x == p[x])
return false;
t = p[x];
p[x] = p[t];
x = t;
} else {
if (y == p[y])
return false;
t = p[y];
p[y] = p[t];
y = t;
}
}
return true;
}
void unite(int x, int y)
{
int t;
while (p[x] != p[y])
{
if (p[x] < p[y])
{
if (x == p[x])
{
p[x] = p[y];
break;
}
t = p[x];
p[x] = p[y];
x = t;
}
else
{
if (y == p[y])
{
p[y] = p[x];
return;
}
t = p[y];
p[y] = p[x];
y = t;
}
}
}
int main()
{
int m;
fin >> n >> m;
for (int i = 1; i <= n; ++i)
p[i] = i;
for (; m; --m)
{
int op, x, y;
fin >> op >> x >> y;
if (op == 1)
unite(x, y);
else if (sameset(x, y))
fout << "DA\n";
else
fout << "NU\n";
}
fout.close();
return 0;
}