Pagini recente » Cod sursa (job #80717) | Cod sursa (job #3279073) | Cod sursa (job #2946756) | Cod sursa (job #2565942) | Cod sursa (job #3273557)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, op;
int tata[100002], inaltime[100002];
int getRoot(int x)
{
int root=x;
while(root!=tata[root])
{
root=tata[root];
}
while(x!=tata[x])
{
int tx=tata[x];
tata[x]=root;
x=tx;
}
return root;
}
void unite(int x, int y)
{
x=getRoot(x);
y=getRoot(y);
if(inaltime[x]>inaltime[y])
{
tata[y]=x;
}
else
if(inaltime[x]>inaltime[y])
{
tata[x]=y;
}
else
{
tata[x]=y;
inaltime[y]++;
}
}
int main()
{
fin>>n>>m;
for(int i=1; i<=n; i++)
{
tata[i]=i;
inaltime[i]=1;
}
for(int i=1; i<=m; i++)
{
int x, y;
fin>>op>>x>>y;
if(op==1)
{
unite(x, y);
}
else
{
if(getRoot(x)==getRoot(y))
{
fout<<"DA";
}
else
{
cout<<"NU";
}
}
}
return 0;
}