Pagini recente » Cod sursa (job #2301505) | Cod sursa (job #2366363) | Cod sursa (job #167756) | Cod sursa (job #2390443) | Cod sursa (job #1831099)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
void AtaseazaArbore(int n, vector<int> &tata, int x, int y)
{
while (tata[x])
x = tata[x];
while (tata[y])
y = tata[y];
tata[y] = x;
}
bool VerificaArbori(int n, vector<int> &tata, int x, int y)
{
while (tata[x])
x = tata[x];
int tata_y = (tata[y] != 0) ? tata[y] : y;
while (tata[tata_y])
tata_y = tata[tata_y];
if (x != tata_y)
return false;
while (tata[y])
{
tata_y = tata[y];
tata[y] = x;
y = tata_y;
}
return true;
}
int main()
{
int n, m;
f >> n >> m;
vector<int> tata;
tata.resize(n + 1);
for (int i = 1; i <= n; ++i)
tata[i] = 0;
for (int i = 1; i <= m; ++i)
{
int cod, x, y;
f >> cod >> x >> y;
if (cod == 1)
AtaseazaArbore(n, tata, x, y);
else
{
if (VerificaArbori(n, tata, x, y))
g << "DA\n";
else
g << "NU\n";
}
}
}