Pagini recente » Cod sursa (job #1625219) | Cod sursa (job #2491773) | Cod sursa (job #1452738) | Cod sursa (job #1265802) | Cod sursa (job #2932718)
#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;
for (int i = 0; i < n+1; i++)
{
tata.push_back(-1);
inaltime.push_back(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" << endl;
}
else
{
fout << "NU" << endl;
}
}
}
}
int main()
{
citireInitializare();
rezolvareCerinte();
}