Pagini recente » Cod sursa (job #2712273) | Cod sursa (job #1677709) | Cod sursa (job #466401) | Cod sursa (job #2961202) | Cod sursa (job #1024245)
#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)
{
if(H[x]>H[y])
Tata[y]=x;
else
{
Tata[x]=y;
if(H[x]==H[y])
H[x]++;
}
}
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;
}