Pagini recente » Cod sursa (job #3213930) | Cod sursa (job #272086) | Cod sursa (job #660698) | Cod sursa (job #930483) | Cod sursa (job #1688397)
#include<fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int NMAX = 100005;
int T[NMAX],R[NMAX],N,Q;
int tata(int nod)
{
int t;
for(t = nod ; t != T[t] ; t = T[t]);
while(nod != T[nod]){
int y = T[nod];
T[nod] = t;
nod = y;
}
return t;
}
void unire(int x,int y)
{
if(R[x] > R[y])
T[y] = x;
else
T[x] = y;
if(R[x] == R[y])
++R[y];
}
int main()
{
in>>N>>Q;
for(int i = 1 ; i <= N ; ++i)
R[i] = 1,T[i] = i;
int cod,x,y;
for( ; Q ; --Q){
in>>cod>>x>>y;
if(cod == 1)
unire(x,y);
else{
if(tata(x) == tata(y))
out<<"DA\n";
else
out<<"NU\n";
}
}
in.close();
out.close();
return 0;
}