Pagini recente » Cod sursa (job #710171) | Cod sursa (job #690213) | Cod sursa (job #141055) | Cod sursa (job #2860721) | Cod sursa (job #2674209)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,x,y,cod,tt[100005],sz[100005];
int find_radacina(int x)
{
if(x==tt[x]) return x;
int radacina = find_radacina(tt[x]);
tt[x]=radacina;
return tt[x];
}
void unite(int x, int y)
{
x = find_radacina(x);
y = find_radacina(y);
if(x == y) return;
if(sz[x]<sz[y]) swap(x,y);
tt[y]=tt[x];
sz[x]+=sz[y];
}
int main()
{
fin >> n >> m;
for(int i=1;i<=n;i++)
{
tt[i]=i;
sz[i]=1;
}
for(int i=1;i<=m;i++)
{
fin >> cod >> x >> y;
if(cod==1)
{
unite(find_radacina(x),find_radacina(y));
}
else
{
if(find_radacina(x)==find_radacina(y)) fout << "DA \n";
else fout << "NU \n";
}
}
return 0;
}