Pagini recente » Cod sursa (job #3004212) | Cod sursa (job #2540593) | Cod sursa (job #2831822) | Cod sursa (job #3168859) | Cod sursa (job #1024250)
#include <fstream>
using namespace std;
int N,M;
int H[100005];
int Tata[100005];
void Formare()
{
for(int i=1;i<=N;++i)
Tata[i]=i;
}
int FindFather(int x)
{
int Root,aux;
Root=aux=x;
while(Root!=Tata[Root])
Root=Tata[Root];
while(x!=Tata[x])
{
aux=Tata[x];
Tata[x]=Root;
x=aux;
}
return Root;
}
void Unite(int x,int y)
{
int Rx=FindFather(x);
int Ry=FindFather(y);
if(H[x]>H[y])
Tata[Ry]=Rx;
else
{
Tata[Rx]=Ry;
if(H[x]==H[y])
H[y]++;
}
}
int main()
{
ifstream f("disjoint.in");
ofstream fout("disjoint.out");
f>>N>>M;
Formare();
int x,y,Op;
for(int i=1;i<=M;++i)
{
f>>Op>>x>>y;
if(Op==1)
{
Unite(x,y);
continue;
}
if(Op==2)
{
if(FindFather(x)==FindFather(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
}
f.close();
fout.close();
return 0;
}