Pagini recente » Cod sursa (job #1076546) | Cod sursa (job #957059) | Statistici Attila Bodo (attilalord) | Cod sursa (job #1080847) | Cod sursa (job #2421646)
#if 1
#include <fstream>
using namespace std;
int tata[200005], dist[200005];
fstream f("disjoint.in");
ofstream g("disjoint.out");
int rad(int x) {
if (tata[x] == x)
return x;
tata[x] = rad(tata[x]);
return tata[x];
}
void unire(int x, int y) {
if (dist[x] < dist[y])
tata[x] = tata[y];
else tata[y] = tata[x];
if (dist[x] == dist[y])
dist[y]++;
}
int main()
{
int n, m, k, i, x, y;
f >> n >> m;
for (i = 1; i <= n; i++)
{
tata[i] = i;
dist[i] = 1;
}
for (i = 0; i < m; i++)
{
f >> k >> x >> y;
if (k == 1)
{
unire(rad(x), rad(y));
}
else if (rad(x) == rad(y))g << "DA" <<"\n";
else g << "NU" <<"\n";
}
return 0;
}
#endif