Pagini recente » Cod sursa (job #1462857) | Cod sursa (job #2546585) | Cod sursa (job #1222780) | Cod sursa (job #1201717) | Cod sursa (job #2290259)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
struct elem{
int info = 0,
fatherNode = 0;
} v[100002];
int rootFinder(int a)
{
elem x = v[a];
while(x.fatherNode != 0) x = v[x.fatherNode];
return x.info;
}
void unite(int x, int y)
{
int a, b;
a = rootFinder(x);
b = rootFinder(y);
v[b].fatherNode = a;
}
bool checkMemb(int x, int y)
{
int a, b; bool sem;
a = rootFinder(x);
b = rootFinder(y);
return (a == b);
}
int main()
{
int n, m, t, x, y, i;
fin >> n >> m;
for(i = 1; i <= n; i++) v[i].info = i;
for(i = 1; i <= m; i++){
fin >> t >> x >> y;
if(t == 1) unite(x, y);
else{
if(checkMemb(x, y)) fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}