Pagini recente » Cod sursa (job #1046092) | Cod sursa (job #2540139) | Cod sursa (job #157559) | Cod sursa (job #3217371) | Cod sursa (job #2730296)
#include <fstream>
using namespace std;
int Find(int nod, int Father[]) {
int Rep = nod, temp;
while(Rep != Father[Rep])
Rep = Father[Rep];
while(nod != Father[nod]) {
temp = Father[nod];
Father[nod] = Rep;
nod = temp;
}
return Rep;
}
int main() {
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, q, x, y, Father[100001], Rank[100001] {}, i, RepX, RepY;
fin >> n >> m;
for(i = 1; i <= n; ++i)
Father[i] = i;
for(i = 1; i <= m; ++i) {
fin >> q >> x >> y;
if(q == 1) {
RepX = Find(x, Father);
RepY = Find(y, Father);
if(Rank[RepX] > Rank[RepY]) {
++Rank[RepX];
Father[RepY] = RepX;
}
else {
++Rank[RepY];
Father[RepX] = RepY;
}
}
else
fout << (Find(x, Father) == Find(y, Father) ? "DA\n" : "NU\n");
}
}