Pagini recente » Cod sursa (job #2302874) | Romanian IOI Medalists: Careers | Cod sursa (job #1964074) | Cod sursa (job #2592687) | Cod sursa (job #2488743)
#include <fstream>
using namespace std;
int t[100005],h[100005];
int findSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
void unionSet(int x,int y)
{
if(h[x]>h[y])
t[y]=x;
else if (h[x]<h[y])
t[x]=y;
else
{
t[y]=x;
h[x]++;
}
}
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int main() {
int n,m,i,x,y,tx,ty,a;
in>>n>>m;
for(i=1;i<=n;i++)
{
h[i]=1;
t[i]=i;
}
for(i=1;i<=m;i++)
{
in>>a>>x>>y;
tx=findSet(x);
ty=findSet(y);
if(a==1){
if(tx!=ty)
unionSet(tx,ty);
}
else
{
if(tx==ty)
out<<"DA\n";
else
out<<"NU\n";
}
}
}