Pagini recente » Cod sursa (job #1689221) | Cod sursa (job #1143999) | Cod sursa (job #462965) | Cod sursa (job #1262086) | Cod sursa (job #2512527)
#define NMAX 100005
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, c, x, y;
int tati[NMAX];
int rad_act;
void rad(int x)
{
if(tati[x] == x)
{
rad_act = x;
return;
}
rad(tati[x]);
tati[x] = rad_act;
}
void findW(int x, int y)
{
rad(x);
int radOfX = rad_act;
rad(y);
int radOfY = rad_act;
if(radOfX == radOfY)
g<<"DA\n";
else g<<"NU\n";
}
void init()
{
for(int i=1; i<=n; ++i)
tati[i] = i;
}
void unionOf(int x, int y)
{
rad(x);
int aux = rad_act;
rad(y);
tati[aux] = rad_act;
}
int main()
{
f>>n>>m;
init();
for(int i=0; i<m; ++i)
{
f>>c>>x>>y;
if(c == 1)
unionOf(x, y);
else findW(x, y);
}
return 0;
}