Pagini recente » Cod sursa (job #2582123) | Cod sursa (job #770473) | Cod sursa (job #2867167) | Cod sursa (job #2525581) | Cod sursa (job #1350298)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[100005], h[100005];
int FindSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void UnionSet(int x, int y)
{
//x,y radacinile arborilor
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
int main() {
int n,m,op,x,y;
in>>n>>m;
for(int i=1;i<=n;i++)
t[i]=i;
for(int i=1;i<=m;i++)
{
in>>op>>x>>y;
int tx=FindSet(x),ty=FindSet(y);
if(op==1)
{
UnionSet(tx,ty);
}
else
{
if(tx==ty)
out<<"DA\n";
else
out<<"NU\n";
}
}
return 0;
}