Pagini recente » Cod sursa (job #1932588) | Cod sursa (job #1643024) | Cod sursa (job #2653075) | Cod sursa (job #286436) | Cod sursa (job #1786167)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=100000;
int t[NMAX+5],h[NMAX+5];
int findset(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void unionset(int x,int y)
{
int tx,ty;
tx=findset(x);
ty=findset(y);
if(h[tx]==h[ty])
{
t[ty]=tx;
h[tx]++;
}
else
if(h[tx]>h[ty])
t[ty]=tx;
else
t[tx]=ty;
}
int main()
{
int n,m,i,cod,x,y;
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)
{
if(findset(x)!=findset(y))
unionset(x,y);
}
else
if(findset(x)==findset(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
fin.close();
fout.close();
return 0;
}