Pagini recente » Cod sursa (job #1256207) | Cod sursa (job #2153552) | Cod sursa (job #1729268) | Cod sursa (job #1817751) | Cod sursa (job #2866643)
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <unordered_map>
#define NMAX 100003
using namespace std;
int n, m;
int tata[NMAX];
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int cRad(int k)
{
int rad = k;
while (tata[rad] > 0)
{
rad = tata[rad];
}
int nr = k;
while (tata[nr] > 0)
{
int aux = nr;
nr = tata[nr];
tata[aux] = rad;
}
return rad;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
{
tata[i] = -1;
}
for (int i = 1; i <= m; i++)
{
int op, x, y;
fin >> op >> x >> y;
int rad1 = cRad(x);
int rad2 = cRad(y);
if (op == 1)
{
//reuniune
if (rad1 != rad2)
{
if (tata[rad1] <= tata[rad2])
{
tata[rad1] += tata[rad2];
tata[rad2] = rad1;
}
else {
tata[rad2] += tata[rad1];
tata[rad1] = rad2;
}
}
}
else {
if (rad1 == rad2)
{
fout << "DA\n";
}
else {
fout << "NU\n";
}
}
}
return 0;
}