Pagini recente » Cod sursa (job #2153462) | Cod sursa (job #1690272) | Cod sursa (job #1073300) | Cod sursa (job #2402014) | Cod sursa (job #1249781)
#include <fstream>
#define Nmax 100100
using namespace std;
class Forest {
private:
int Father[Nmax];
public:
void init(int N) {
for(int i = 1; i <= N; i++)
Father[i] = i;
}
int root(int Node) {
if(Node != Father[Node])
Father[Node] = root(Father[Node]);
return Father[Node];
}
bool same(int A, int B) {
return (root(A) == root(B));
}
void unite(int A, int B) {
A = root(A);
B = root(B);
if(A == B)
return;
Father[A] = B;
}
};
Forest F;
int main() {
int type, x, y, N, M;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
in >> N >> M;
F.init(N);
while(M--) {
in >> type >> x >> y;
if(type == 1)
F.unite(x, y);
else if(F.same(x, y))
out << "DA\n";
else
out << "NU\n";
}
in.close();
out.close();
return 0;
}