Pagini recente » Arhiva de probleme | Cod sursa (job #703219) | Cod sursa (job #532925) | Cod sursa (job #1430944) | Cod sursa (job #2277469)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int T[100005], t1, t2;
int sef(int x)
{
if(x!=T[x])
T[x] = sef(T[x]);
return T[x];
}
void join(int x, int y)
{
t1 = sef(x);
t2 = sef(y);
T[t2]=t1;
}
int main()
{
int n, m, cod, x, y, i;
in>>n>>m;
for(i = 1; i<=n; i++)
T[i]=i;
for(i = 1; i<=m; i++)
{
in>>cod>>x>>y;
if(cod==1)
join(x, y);
else
{
if(sef(x)==sef(y))
out<<"DA"<<endl;
else out<<"NU"<<endl;
}
}
return 0;
}