Pagini recente » Cod sursa (job #1734797) | Cod sursa (job #660380) | Cod sursa (job #1326699) | Cod sursa (job #3238414) | Cod sursa (job #2527573)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[100005], n, m;
inline void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int rad, y;
/// gasim radacina x
rad = x;
while(t[rad] != 0)
rad = t[rad];
/// compactam drumul
while(x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int i, op, x, y;
fin >> n >> m;
for(i = 1; i <= m; i++)
{
fin >> op >> x >> y;
x = Find(x);
y = Find(y);
if(op == 1)
{
if(x != y) Union(x, y);
}
else
{
if(x == y) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}