Pagini recente » Cod sursa (job #124445) | Cod sursa (job #1764964) | Cod sursa (job #1004826) | Cod sursa (job #2425557) | Cod sursa (job #902950)
Cod sursa(job #902950)
#include <cstdio>
#include <cstring>
#include <cassert>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
typedef vector<int>::iterator it;
const int oo = 0x3f3f3f3f;
const int MAX_N = 100005;
const int NIL = -1;
int N, Father[MAX_N];
int Find(int X) {
if (Father[X] == NIL)
return X;
Father[X] = Find(Father[X]);
return Father[X];
}
inline bool Merge(int X, int Y) {
X = Find(X); Y = Find(Y);
if (X == Y)
return false;
Father[Y] = X;
return true;
}
void InitForest() {
memset(Father, NIL, sizeof(Father));
}
int main() {
assert(freopen("disjoint.in", "r", stdin));
assert(freopen("disjoint.out", "w", stdout));
int NQ; assert(scanf("%d %d", &N, &NQ) == 2);
InitForest();
for (; NQ > 0; --NQ) {
int Type, X, Y; assert(scanf("%d %d %d", &Type, &X, &Y) == 3);
if (Type == 1)
assert(Merge(X, Y));
else
printf("%s\n", (Find(X) == Find(Y) ? "DA" : "NU"));
}
return 0;
}