Pagini recente » Cod sursa (job #1972489) | Cod sursa (job #3158733) | Cod sursa (job #2451041) | Cod sursa (job #3164806) | Cod sursa (job #2173358)
#include <iostream>
#include <fstream>
using namespace std;
#define nmax 100005
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m,p[nmax];
int parinte(int nod)
{
if (nod==p[nod])
return nod;
return p[nod]=parinte(p[nod]);
}
void unire(int x1,int x2)
{
p[parinte(x1)]=parinte(x2);
}
void checkparents(int x1,int x2)
{
if (parinte(x1)==parinte(x2))
g<<"DA\n";
else
g<<"NU\n";
return;
}
void read()
{
f>>n>>m;
for (int i=1;i<=n;++i)
p[i]=i;
for (int i=1;i<=m;++i)
{
int op,e2,e3;
f>>op>>e2>>e3;
if (op==1)
unire(e2,e3);
else
checkparents(e2,e3);
}
}
int main()
{
read();
return 0;
}