Pagini recente » Cod sursa (job #3157573) | Cod sursa (job #1635530) | Cod sursa (job #2982280) | Cod sursa (job #1175105) | Cod sursa (job #2956864)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 2e5 + 1;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
struct nod
{
struct nod* next;
int value;
} *list[NMAX];
int n, m, op, x, y;
nod* createNewNode(int num)
{
nod* nou = new nod;
nou -> value = num;
nou -> next = NULL;
return nou;
}
void reuniune(int x, int y)
{
nod* curr = list[x];
while (curr -> next != NULL)
{
curr = curr -> next;
}
nod *curr2 = list[y];
while (curr2 != NULL)
{
curr -> next = createNewNode(curr2 -> value);
curr = curr -> next;
curr2 = curr2 -> next;
}
list[y] = list[x];
}
int reprezentant(int x)
{
nod* curr = list[x];
while (curr -> value != x)
{
x = curr -> value;
curr = list[curr -> value];
}
return curr -> value;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++)
{
list[i] = createNewNode(i);
}
while (m--)
{
fin >> op >> x >> y;
if (x > y)
{
swap(x, y);
}
if (op == 1)
{
//cout << list[x] -> value << ' ' << list[y] -> value << '\n';
reuniune(x, y);
//cout << list[x] -> value << ' ' << list[y] -> value << '\n';
}
else
{
if (reprezentant(x) == reprezentant(y))
{
fout << "DA\n";
}
else
{
fout << "NU\n";
}
}
}
}