Pagini recente » Cod sursa (job #324387) | Cod sursa (job #2075671) | Cod sursa (job #2935505) | Cod sursa (job #268622) | Cod sursa (job #949193)
Cod sursa(job #949193)
#include <fstream>
#include <vector>
using namespace std;
#define in "disjoint.in"
#define out "disjoint.out"
#define N 100005
vector <int> tata (N, 0);
int n, m, x, y, type;
int find (int x) {
if (tata[x] != x)
tata[x] = find (tata[x]);
return tata[x];
}
void unite (int x, int y) {
tata[x] = y;
}
int main () {
ifstream fin (in);
ofstream fout (out);
fin >> n >> m;
for (int i = 1; i <= n; ++i)
tata[i] = i;
for (int i = 0; i < m; ++i) {
fin >> type >> x >> y;
if (type == 1)
unite (find(x), find(y));
else
fout << (find(x) == find(y) ? "DA" : "NU") << "\n";
}
fcloseall();
}