Pagini recente » Cod sursa (job #158197) | Cod sursa (job #2444079) | Cod sursa (job #446539) | Cod sursa (job #2562664) | Cod sursa (job #1640050)
#include <fstream>
#include <vector>
class SetGroup
{
private:
std::vector<int> v;
inline int get(int x) {
while(v[x] != 0) x = v[x];
return x;
}
inline void mark(int x, int mark) {
int aux;
while(v[x] != 0) {
aux = v[x];
v[x] = mark;
x = aux;
}
}
public:
SetGroup() {
}
SetGroup(int n_sets) : v(n_sets) {
}
bool areJoint(int x, int y) {
int a = get(x);
int b = get(y);
mark(x, a);
mark(y, b);
return a == b;
}
void uniteSets(int x, int y) {
int a = get(x);
int b = get(y);
if(a != b) v[a] = b;
}
};
int main()
{
std::ifstream in("disjoint.in");
std::ofstream out("disjoint.out");
int n, m;
in>>n>>m;
SetGroup forest(n);
for(int a, b, c, i = 1; i <= m; ++i) {
in>>c>>a>>b;
switch(c) {
case 1:
forest.uniteSets(a, b);
break;
case 2:
if(forest.areJoint(a, b)) out<<"DA\n";
else out<<"NU\n";
}
}
}