Pagini recente » Cod sursa (job #449382) | Cod sursa (job #1975485) | Cod sursa (job #1432601) | Cod sursa (job #818383) | Cod sursa (job #1003815)
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
char da[]="DA";
char nu[]="NU";
class DisjointSets
{
private:
vector<int> t,h;
int Dad(int u)
{
return u==t[u]?u:t[u]=Dad(t[u]);
}
public:
DisjointSets(int n)
{
t.push_back(0);h.push_back(0);
for(int i=1;i<=n;i++)
{
h.push_back(1);
t.push_back(i);
}
}
void update(int x,int y)
{
x=Dad(x);y=Dad(y);
if(x==y) return;
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
char* query(int x,int y)
{
return Dad(x)==Dad(y)?da:nu;
}
};
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
DisjointSets S(n);
for(int tip,x,y,i=1;i<=m;i++)
{
scanf("%d%d%d",&tip,&x,&y);
if(tip==1)
S.update(x,y);
else
puts(S.query(x,y));
}
return 0;
}