Pagini recente » Cod sursa (job #977170) | Cod sursa (job #2057979) | Rating Rusu-Raiciu Andrei-Tiberiu (rusuandrei27) | Cod sursa (job #188963) | Cod sursa (job #1311292)
#include <fstream>
#define lmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,tip,x,y;
int tata[lmax],inalt[lmax];
inline int find(int nod)
{
int rad=nod;
while (tata[rad]!=rad)
rad=tata[rad];
while (tata[nod]!=nod)
{ tata[nod]=rad;
nod=tata[nod];
}
return rad;
}
inline void uneste(int nod1,int nod2)
{
if (inalt[nod1]>inalt[nod2])
tata[nod1]=nod2;
else
tata[nod2]=nod1;
if (inalt[nod1]==inalt[nod2])
inalt[nod2]++;
}
int main()
{
f>>n>>m;
for (int i=1;i<=n;i++)
tata[i]=i;
for (int i=1;i<=m;i++)
{
f>>tip>>x>>y;
if (tip==1)
uneste(find(x),find(y));
else
if (find(x)==find(y))
g<<"DA\n";
else
g<<"NU\n";
}
f.close();
g.close();
}