Pagini recente » Cod sursa (job #1003349) | Cod sursa (job #2012799) | Cod sursa (job #181091) | Cod sursa (job #3207218) | Cod sursa (job #3154089)
#include <iostream>
#include <fstream>
#include <unordered_map>
using namespace std;
unordered_map <int, int> mapa;
int findsef (int a);
int main()
{
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int n, m, op, a, b;
cin >> n >> m;
for (int i=1; i<=n; i++)
{
mapa[i] = i;
}
for (int i=0; i<m; i++)
{
cin >> op;
if (op == 1)
{
cin >> a >> b;
mapa[b] = mapa[a];
}
else if (op == 2)
{
cin >> a >> b;
if (findsef(a) == findsef(b))
{
cout << "DA" << endl;
}
else cout << "NU" << endl;
}
}
return 0;
}
int findsef (int a)
{
while (mapa[a] != a)
{
a = mapa[a];
}
return a;
}