Pagini recente » Cod sursa (job #1271828) | Cod sursa (job #2026355) | Cod sursa (job #2274389) | Cod sursa (job #758370) | Cod sursa (job #2147730)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=100000;
int t[NMAX+5];
int h[NMAX+5];
int FindSet(int x)
{
while(x!=t[x])
{
x=t[x];
}
return x;
}
void UnionSet(int x, int y)
{
//x si y sunt radacinile celor 2 arbori
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, i, tu, tv, a, u, v;
fin>>n>>m;
for(i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
{
fin>>a>>u>>v;
if(a==1)
{
tu=FindSet(u);
tv=FindSet(v);
if(tu!=tv)
UnionSet(tu, tv);
}
else
{
if(FindSet(u)==FindSet(v))
fout<<"DA"<<"\n";
else
fout<<"NU"<<"\n";
}
}
return 0;
}