Pagini recente » Monitorul de evaluare | Cod sursa (job #2421172) | Cod sursa (job #1550063) | Cod sursa (job #1433848) | Cod sursa (job #2091795)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int MAX_N = 100000;
int N, M, r[1 + MAX_N];
int reprezentant(int k) {
if (r[k] != k)
r[k] = reprezentant(r[k]);
return r[k];
}
bool verif(int x, int y) {
return reprezentant(x) == reprezentant(y);
}
void adauga(int x, int y) {
x = reprezentant(x);
y = reprezentant(y);
r[x] = y;
}
int main() {
fin >> N >> M;
for (int i = 1; i <= N; i++)
r[i] = i;
for (int h = 1; h <= M; h++) {
int cod, x, y;
fin >> cod >> x >> y;
if (cod == 1)
adauga(x, y);
if (cod == 2) {
if (verif(x, y))
fout << "DA\n";
else fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}