Pagini recente » Cod sursa (job #1053466) | Cod sursa (job #2570831) | Cod sursa (job #2322798) | Cod sursa (job #1266479) | Cod sursa (job #1403334)
#include <fstream>
using namespace std;
const int N=100001;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int tata[N],ina[N];
int find(int x)
{
int pas=x;
while(tata[pas]!=pas) pas=tata[pas];
int trec=x,aux;
while(trec!=pas)
{
aux=tata[trec];
tata[trec]=pas;
trec=aux;
}
return pas;
}
void unione(int x,int y)
{
if(ina[x]>ina[y]) tata[y]=x;
else
{
tata[x]=y;
}
if(ina[x]==ina[y]) ina[y]++;
}
int main()
{
int n,m,i,ok,x,y;
fin>>n>>m;
for(i=1;i<=n;i++)
{
tata[i]=i;
ina[i]=1;
}
for(i=1;i<=m;i++)
{
fin>>ok>>x>>y;
if(ok==1) unione(x,y);
else if(find(x)==find(y)) fout<<"DA\n";
else fout<<"NU\n";
}
}