Pagini recente » Cod sursa (job #587726) | Cod sursa (job #2550461) | Cod sursa (job #1271154) | Cod sursa (job #1639016) | Cod sursa (job #2479044)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[100005],rang[100005],n,m,x,y,i,c;
int reprezentant(int x)
{
if(t[x]!=x)
t[x]=reprezentant(t[x]);
return t[x];
}
void uneste(int x,int y)
{
x=reprezentant(x);
y=reprezentant(y);
if(rang[x]<rang[y])
{
t[x]=t[y];
rang[y]+=rang[x];
}
else
{
t[y]=t[x];
rang[x]+=rang[y];
}
}
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
t[i]=i,rang[i]++;
for(i=1;i<=m;i++)
{
f>>c>>x>>y;
if(c==1)
{
uneste(x,y);
}
else
{
if(reprezentant(x)==reprezentant(y))
g<<"DA"<<'\n';
else g<<"NU"<<'\n';
}
}
return 0;
}