Pagini recente » Cod sursa (job #384376) | Cod sursa (job #1386864) | Cod sursa (job #2746959) | Cod sursa (job #2474680) | Cod sursa (job #2833920)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int n, m;
int parent[100001];
int Find(int x)
{
if (x != parent[x])
{
return Find(parent[x]);
}
return x;
}
void Union(int x, int y)
{
int rootX = Find(x);
int rootY = Find(y);
if (rootX != rootY)
{
parent[rootX] = rootY;
}
}
int main()
{
f >> m >> n;
for (int i = 1; i <= m; i++)
{
parent[i] = i;
}
for (int i = 1; i <= n; i++)
{
int option, x, y;
f >> option;
switch (option)
{
case 1:
f >> x >> y;
Union(x, y);
break;
case 2:
f >> x >> y;
if (Find(x) == Find(y))
{
g << "DA\n";
}
else
{
g << "NU\n";
}
break;
}
}
// for (int i = 1; i <= m; i++)
// {
// cout << parent[i] << " ";
// }
}