Pagini recente » Cod sursa (job #959293) | Cod sursa (job #923702) | Cod sursa (job #2955426) | Cod sursa (job #2639128) | Cod sursa (job #3273559)
#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"<<endl;
}
else
{
fout<<"NU"<<endl;
}
}
}
return 0;
}