Pagini recente » Cod sursa (job #1536636) | Cod sursa (job #1894518) | Cod sursa (job #1818446) | Atasamentele paginii oji_bv_1112 | Cod sursa (job #3285847)
//Code by Patcas Csaba aka SleepyOverlord
#include <vector>
#include <array>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <bitset>
#include <stack>
#include <list>
#include <numeric>
#include <algorithm>
#include <random>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <climits>
#include <cctype>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;
#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define PLL pair <LL, LL>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVLL vector < VLL >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define MX(x, y) x = max(x, y)
#define MN(x, y) x = min(x, y)
#define SZ size()
#define BG begin()
#define EN end()
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count
template <class T> ostream& operator <<(ostream & os, const vector<T> &vec)
{
for (int i = 0; i < vec.size() - 1; ++i) os << vec[i] << ' ';
return os << vec[vec.size() - 1];
}
template <class T1, class T2> ostream& operator <<(ostream & os, const pair<T1, T2> &p)
{
return os << p.X << " " << p.Y;
}
template <typename T>
void pr(T var1)
{
cout << var1 << '\n';
}
template <typename T, typename... Types>
void pr(T var1, Types... var2)
{
cout << var1;
pr(var2...);
}
int n, q;
VI parent, sz;
int find_set(int v)
{
if (v == parent[v]) return v;
return parent[v] = find_set(parent[v]);
}
void union_set(int a, int b)
{
a = find_set(a);
b = find_set(b);
if (sz[a] < sz[b]) swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
int main()
{
#ifdef AT_HOME
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q;
parent.RS(n + 1), sz.RS(n + 1, 1);
FOR(i, 1, n) parent[i] = i;
FOR(qq, 1, q)
{
int op, x, y;
cin >> op >> x >> y;
if (op == 1)
{
assert(find_set(x) != find_set(y));
union_set(x, y);
}
else cout << (find_set(x) == find_set(y) ? "DA" : "NU") << "\n";
}
return 0;
}