Pagini recente » Infoarena Monthly 2014, Clasament Runda 5 | Cod sursa (job #318681) | Cod sursa (job #2723234) | Cod sursa (job #600351) | Cod sursa (job #3232081)
#include <fstream>
using namespace std;
const int N = 1e5;
int t[N+1], h[N+1];
int radacina(int x)
{
if (t[x] == 0)
{
return x;
}
return radacina(t[x]);
}
void reuniune(int x, int y)
{
int rx = radacina(x);
int ry = radacina(y);
if (h[rx] < h[ry])
{
t[rx] = ry;
}
else
{
t[ry] = rx;
if (h[rx] == h[ry])
{
h[rx]++;
}
}
}
bool verif(int x, int y)
{
return (radacina(x) == radacina(y));
}
int main()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, q;
in >> n >> q;
for (int i = 1; i <= q; i++)
{
int tip, x, y;
in >> tip >> x >> y;
if (tip == 1)
{
reuniune(x, y);
}
else
{
if (verif(x, y))
{
out << "DA\n";
}
else
{
out << "NU\n";
}
}
}
in.close();
out.close();
return 0;
}