Pagini recente » Cod sursa (job #2173062) | Cod sursa (job #2346750) | Cod sursa (job #578089) | Cod sursa (job #2947219) | Cod sursa (job #2932720)
#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)
{
while(tata[nod1] != -1)
{
nod1 = tata[nod1];
}
return 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();
}