Pagini recente » Cod sursa (job #936027) | Cod sursa (job #2711288) | Infoarena Monthly 2014 - Clasament | Cod sursa (job #2250550) | Cod sursa (job #1469100)
#include <fstream>
#define Nmax 100005
using namespace std;
ofstream fout("disjoint.out");
ifstream fin("disjoint.in");
int v[Nmax],n,m;
void reuniune(int x, int y)
{
int y1=0,x1=0;
if(v[x]!=x)
{
if(v[y]==y)
{
v[y]=v[x];
}
else
{
y1=v[y];
while(v[y1]!=y1)
{
y1=v[y1];
}
v[y1]=v[x];
}
}
else if(v[y]!=y)
{
if(v[x]==x)
{
v[x]=v[y];
}
else
{
x1=v[x];
while(v[x1]!=x1)
{
x1=v[x1];
}
v[x1]=v[y];
}
}
else
{
v[x]=y;
}
}
void verif(int x, int y)
{
if(v[x]==v[y])
{
fout<<"DA"<<'\n';
return;
}
fout<<"NU"<<'\n';
}
int main()
{
int i=0,x=0,y=0,o=0;
fin>>n;
fin>>m;
for(i=1;i<=n;i++)
{
v[i]=i;
}
for(i=1;i<=m;i++)
{
fin>>o>>x>>y;
if(o==1)
{
reuniune(x,y);
}
if(o==2)
{
verif(x,y);
}
}
return 0;
}