Pagini recente » Cod sursa (job #22775) | Cod sursa (job #2758356) | Cod sursa (job #1590386) | Cod sursa (job #2152799) | Cod sursa (job #2778966)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
#define cin f
#define cout g
#define int long long
const int Max = 1e5 + 1;
void nos()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
class DSU{
private:
vector < int > parent;
vector < int > rang;
public:
void init_empty(int n)
{
parent.resize(n + 1);
rang.resize(n + 1);
for(int i=1;i<=n;i++)
parent[i] = i,rang[i] = 1;
}
int find(int x)
{
int nod ,y;
for(nod = x;parent[nod] != nod;nod = parent[nod]);
/*
while(parent[x] != x)
{
y = parent[x];
parent[x] = nod;
x = y;
}
*/
return nod;
}
void merge(int x, int y)
{
if(find(x) == find(y))
{
cout<<"esti prost: sunt deja in aceiasi multime";
assert(false);
}
if(rang[x] > rang[y])
parent[y] = x;
else
parent[x] = y;
if(rang[x] == rang[y])
rang[y] ++;
}
};
int n,m;
void read()
{
f>>n>>m;
}
void solve()
{
DSU dsu;
dsu.init_empty(n);
while(m --)
{
int task, x,y;
f>>task>>x>>y;
if(task == 1)
dsu.merge(x,y);
else
{
if(dsu.find(x) == dsu.find(y))
cout<<"DA\n";
else
cout<<"NU\n";
}
}
}
void restart()
{
}
int32_t main()
{
nos();
read();
solve();
restart();
return 0;
}