Pagini recente » Cod sursa (job #159179) | Cod sursa (job #3272818) | Rating Badita Marcel (MarcelBadita) | Cod sursa (job #3266348) | Cod sursa (job #229341)
Cod sursa(job #229341)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in ("disjoint.in");
ofstream out("disjoint.out");
vector<int> arbore, rang;
int n, m;
int find (int x)
{
int aux = x;
while (arbore[aux] != aux)
{aux = arbore[aux];
}
while (arbore[x] != x)
{x = arbore[x];
arbore[x] = aux;
}
return aux;
}
void uniune(int x, int y)
{
if (rang[x] < rang[y])
{arbore[y] = x;
}
if (rang[x] > rang[y])
{arbore[x] = y;
}
if (rang[x] == rang[y])
{arbore[x] = y;
rang[x]++;
}
}
int main()
{
in >> n >> m;
for (int i = 0; i <= n; i++)
{arbore.push_back(i);
rang.push_back(1);
}
for (int i = 1; i<= m; i++)
{
int x, y, z;
in >> x >> y >> z;
if (x == 1)
{uniune(find(y), find(z));
}
else
{if (find(y) == find(z))
out << "DA" << "\n";
else
out << "NU" << "\n";
}
}
return 0;
}