Pagini recente » Monitorul de evaluare | Cod sursa (job #1634469) | Cod sursa (job #1328066) | Cod sursa (job #1154079) | Cod sursa (job #1374604)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int kNMax = 100010;
int n, tata[kNMax], inaltime[kNMax];
void Initializare() {
for (int i = 1; i <= n; ++i) {
tata[i] = i;
inaltime[i] = 1;
}
}
void Unire(int nod1, int nod2) {
if (inaltime[nod1] == inaltime[nod2]) {
inaltime[nod1]++;
tata[nod2] = nod1;
}
if (inaltime[nod1] > inaltime[nod2])
tata[nod2] = nod1;
if (inaltime[nod1] < inaltime[nod2])
tata[nod1] = nod2;
}
int Dfs(int nod) {
if (tata[nod] != nod)
tata[nod] = Dfs(tata[nod]);
return tata[nod];
}
int main () {
int m, operatie, x, y;
in >> n >> m;
Initializare();
while (m--) {
in >> operatie >> x >> y;
if (operatie == 1)
Unire(Dfs(x), Dfs(y));
if (operatie == 2)
if(Dfs(x) == Dfs(y))
out << "DA" << '\n';
else
out << "NU" << '\n';
}
in.close();
out.close();
return 0;
}