Pagini recente » Cod sursa (job #341407) | Cod sursa (job #901647) | Cod sursa (job #623240) | Cod sursa (job #2491903) | Cod sursa (job #1631808)
#include <fstream>
#include <vector>
using namespace std;
const int N_MAX = 1e5;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int root[N_MAX + 5];
vector<int> ls[N_MAX + 5];
void Join(int x, int y) {
if (ls[x].size() < ls[y].size())
swap(x, y);
for (int i : ls[y]) {
root[i] = x;
ls[x].push_back(i);
}
}
int main() {
fin >> N >> M;
for (int i = 1; i <= N; ++i) {
ls[i].assign(1, i);
root[i] = i;
}
while (M--) {
int c, x, y;
fin >> c >> x >> y;
if (c == 1)
Join(root[x], root[y]);
else
fout << (root[x] == root[y] ? "DA\n" : "NU\n");
}
return 0;
}