Pagini recente » Cod sursa (job #390750) | Cod sursa (job #676111) | Cod sursa (job #88169) | Cod sursa (job #1435734) | Cod sursa (job #2039027)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m;
int p[100000];
int parinte(int nod)
{
if (p[nod]==nod)
return nod;
return p[nod]=parinte(p[nod]);
}
void unite(int x,int y)
{
p[parinte(x)]=parinte(y);
}
int main()
{
f>>n>>m;
for (int i=1; i<=m; ++i)
{
int x1,x2,x3;
f>>x1>>x2>>x3;
if (!p[x2])
p[x2]=x2;
if (!p[x3])
p[x3]=x3;
if (x1==1)
unite(x2,x3);
else
{
if (parinte(x2)==parinte(x3))
g<<"DA\n";
else
g<<"NU\n";
}
}
return 0;
}