Pagini recente » Monitorul de evaluare | Autentificare | Cod sursa (job #2654120) | Cod sursa (job #505695) | Cod sursa (job #2626454)
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int n, m;
int arbore[100005];
int Tata (int nod)
{
if (arbore[nod] == 0)
return nod;
return Tata(arbore[nod]);
}
void Unire (int nod1, int nod2)
{
int rad1 = Tata(nod1);
int rad2 = Tata(nod2);
if (rad1 != rad2)
{
while (arbore[nod1])
{
int aux = arbore[nod1];
arbore[nod1] = rad2;
nod1 = aux;
}
arbore[rad1] = rad2;
}
}
void Querry (int nod1, int nod2)
{
if (Tata(nod1) == Tata(nod2))
g << "DA" << "\n";
else g << "NU" << "\n";
}
int main()
{
f >> n >> m;
while (m --)
{
int type, x, y;
f >> type >> x >> y;
if (type == 1)
Unire(x, y);
else Querry(x, y);
}
return 0;
}