Pagini recente » Cod sursa (job #443792) | Cod sursa (job #820721) | Cod sursa (job #2300911) | Cod sursa (job #2389054) | Cod sursa (job #2686422)
#define NMAX 100005
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream gout("disjoint.out");
struct graph{
int t;
int d;
}g[NMAX];
int n, m, c, x, y;
int tati[NMAX];
int rad_act;
void rad(int x)
{
if(g[x].t == x)
{
rad_act = x;
return;
}
rad(g[x].t);
g[x].t = rad_act;
}
void findW(int x, int y)
{
rad(x);
int radOfX = rad_act;
rad(y);
int radOfY = rad_act;
if(radOfX == radOfY)
gout<<"DA\n";
else gout<<"NU\n";
}
void init()
{
for(int i=1; i<=n; ++i)
g[i].t = i;
}
void unionOf(int x, int y)
{
rad(x);
int aux = rad_act;
rad(y);
if(g[aux].d == g[rad_act].d)
{
g[aux].t = rad_act;
g[aux].d++;
}
else if(g[aux].d < g[rad_act].d)
g[rad_act].t = aux;
else{
g[aux].t = 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;
}