Pagini recente » Monitorul de evaluare | Statistici Andrei (Andrei_Keri) | Profil Tokumei_no_Kage | Statistici Cretoiu Patricia (CretoiuPatricia) | Cod sursa (job #2000144)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax=100000;
int t[nmax+3],h[nmax+3];
int FindSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void UnionSet(int x, int y)
{
if(h[x]==h[y])
{
t[y]=x;
h[x]++;
}
else if(h[x]>h[y])
t[y]=x;
else t[x]=y;
}
int main()
{
int n,m,cod,x,y,i;
fin>>n>>m;
for(i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
fin>>cod>>x>>y;
if(cod==1)
{
x=FindSet(x);
y=FindSet(y);
if(x!=y)
UnionSet(x,y);
}
else if(FindSet(x)==FindSet(y))
fout<<"DA\n";
else fout<<"NU\n";
}
return 0;
}