Pagini recente » Cod sursa (job #2209179) | Cod sursa (job #413130) | Cod sursa (job #2887559) | Cod sursa (job #1646778) | Cod sursa (job #2932723)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// Global variables
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector<int> tata;
vector<int> inaltime;
int n, m;
void citireInitializare()
{
fin >> n >> m;
tata= vector<int>(n + 1, -1);
inaltime = vector<int>(n + 1, 0);
}
int Reprezentant(int nod1)
{
if (tata[nod1] == -1)
{
return nod1;
}
tata[nod1] = Reprezentant(tata[nod1]);
return tata[nod1];
}
void rezolvareCerinte()
{
int cerinta, nod1, nod2;
for (int i = 0; i < m; i++)
{
fin >> cerinta >> nod1 >> nod2;
if (cerinta == 1)
{
int rep1 = Reprezentant(nod1);
int rep2 = Reprezentant(nod2);
if (rep1!=rep2)
{
if (inaltime[rep1] > inaltime[rep2])
{
tata[rep2] = rep1;
}
else
{
tata[rep1] = rep2;
if (inaltime[rep1] == inaltime[rep2])
{
++inaltime[rep2];
}
}
}
}
else
{
if (Reprezentant(nod1)==Reprezentant(nod2))
{
fout << "DA\n";
}
else
{
fout << "NU\n";
}
}
}
}
int main()
{
citireInitializare();
rezolvareCerinte();
}