Pagini recente » Cod sursa (job #2505927) | Cod sursa (job #2545157) | Cod sursa (job #2896696) | Cod sursa (job #2084250) | Cod sursa (job #2329156)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, father[100005], from, to, operatie;
void reunire(int m1, int m2)
{
if (father[m2]==m2)
{
father[m2]=m1;
return;
}
reunire(m1,father[m2]);
father[m2]=father[father[m2]];
}
void cautare(int m1, int m2)
{
while (father[m1]!=m1)
m1=father[m1];
while (father[m2]!=m2)
m2=father[m2];
if (m2==m1)
g << "DA\n";
else
g << "NU\n";
}
int main() {
f >> n >> m;
for (int i=1; i<=n; i++)
father[i]=i;
for (int i=1; i<=m; i++)
{
f >> operatie >> from >> to;
if (operatie==1)
{
reunire(from,to);
}
else
{
cautare(from,to);
}
}
return 0;
}