Pagini recente » Cod sursa (job #1346314) | Cod sursa (job #3040339) | Cod sursa (job #2641114) | Cod sursa (job #1508546) | Cod sursa (job #1542470)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int root[100005], n, m, x, y, tx, ty, i, cod;
int find(int a)
{
int t, i;
for(t=a;t!=root[t];t=root[t]);
i=a;
while(i!=t)
{
x=root[i];
root[i]=t;
i=x;
}
return t;
}
void unite(int a, int b)
{
int ta, tb;
ta=find(a);
tb=find(b);
root[tb]=ta;
}
int main()
{
fin>>n>>m;
for(i=1;i<=n;i++)
root[i]=i;
for(i=1;i<=m;i++)
{
fin>>cod;
if(cod==1)
{
fin>>x>>y;
unite(x,y);
}
else
{
fin>>x>>y;
tx=find(x);
ty=find(y);
if(tx==ty)
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}