Pagini recente » Cod sursa (job #1814100) | Cod sursa (job #786206) | Cod sursa (job #1561309) | Cod sursa (job #441939) | Cod sursa (job #1122985)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
#include <cassert>
using namespace std;
const char infile[] = "disjoint.in";
const char outfile[] = "disjoint.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
template <class T>
class DisjointSet {
public:
DisjointSet(const int &N) {
this -> N = N;
this -> father.resize(N);
for(int i = 0 ; i < N ; ++ i)
this -> father[i] = i;
}
void Join(const int &x, const int &y) {
int Tx = Find(x);
int Ty = Find(y);
assert(Tx != Ty);
father[Tx] = Ty;
}
int Find(const int &Node) {
if(father[Node] != Node)
father[Node] = Find(father[Node]);
return father[Node];
}
string Query(const int &x, const int &y) {
int Tx = Find(x);
int Ty = Find(y);
if(Tx == Ty)
return "DA";
return "NU";
}
private:
vector <int> father;
int N;
};
int main() {
int N, M;
fin >> N >> M;
DisjointSet <int> dSet(N);
for(int i = 1 ; i <= M ; ++ i) {
int tip, x, y;
fin >> tip >> x >> y;
if(tip == 1)
dSet.Join(--x, --y);
else fout << dSet.Query(--x, --y) << '\n';
}
fin.close();
fout.close();
return 0;
}