Pagini recente » Cod sursa (job #1773478) | Cod sursa (job #1824764) | Cod sursa (job #1497657) | Cod sursa (job #1606572) | Cod sursa (job #2009647)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
class Graph {
public:
Graph(int n) {
this -> n = n;
_node.resize(n + 1);
_root.resize(n + 1);
for (int i = 1; i <= n; i ++) {
_node[i].push_back(i);
_root[i] = i;
}
}
void link(int x, int y) {
_link(x, y);
}
void query(int x, int y) {
_root[x] == _root[y] ? cout << "DA" << '\n' : cout << "NU" << '\n';
}
private:
int n;
vector < vector < int > > _node;
vector < int > _root;
void ch_root(int x, int y) {
for (int el : _node[y]) {
_root[el] = _root[x];
}
}
void _link(int x, int y) {
if (_node[x].size() >= _node[y].size()) {
ch_root(x, y);
_node[x].push_back(y);
}
else {
ch_root(y, x);
_node[y].push_back(x);
}
}
};
int main() {
int n, m;
cin >> n >> m;
Graph *graph = new Graph(n);
for (int i = 1; i <= m; i ++) {
int type;
cin >> type;
if (type == 1) {
int x, y;
cin >> x >> y;
graph -> link(x, y);
}
else {
int x, y;
cin >> x >> y;
graph -> query(x, y);
}
}
}