Pagini recente » Cod sursa (job #3294999) | Cod sursa (job #517803) | Cod sursa (job #518234) | Cod sursa (job #3235159) | Cod sursa (job #2518671)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax=100005;
int tt[nmax],rg[nmax];
int findset(int x)
{
while (x!=tt[x])
x=tt[x];
return x;
}
void unionset(int x,int y)
{
if (rg[x]==rg[y])
{
tt[y]=x;
rg[x]++;
}
else if (rg[x]>rg[y]) tt[y]=x;
else tt[x]=y;
}
int main()
{
int n,m,t,a,b,x,y;
fin >> n >> m;
for (int i=1;i<=n;i++)
{
tt[i]=i;
rg[i]=1;
}
for (int i=1;i<=m;i++)
{
fin >> t >> a >> b;
x=findset(a);
y=findset(b);
if (t==1) unionset(x,y);
else
{
if (x==y) fout << "DA";
else fout << "NU";
fout << '\n';
}
}
}