Pagini recente » Cod sursa (job #32865) | Cod sursa (job #2366470) | Cod sursa (job #1426112) | Cod sursa (job #702845) | Cod sursa (job #3159729)
#include <iostream>
#include <fstream>
using namespace std;
int n, m, x, y, i, cod, tata[100001];
int sef(int x)
{
if (tata[x]==x)
return x;
else
return tata[x] = sef(tata[x]);
}
void unire (int x, int y)
{
int sefx = sef(x);
int sefy = sef(y);
tata[sefy] = sefx;
}
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>> n >> m;
for (i=1; i<=n; ++i)
tata[i] = i;
for (i=1; i<=m; ++i)
{
f >> cod >> x >> y;
if (cod == 1)
unire(x, y);
else
{
if (sef(x)==sef(y))
g << "DA\n";
else
g << "NU\n";
}
}
f.close();
g.close();
return 0;
}