Pagini recente » Cod sursa (job #1102737) | Cod sursa (job #1563488) | Cod sursa (job #2924777) | Cod sursa (job #2256432) | Cod sursa (job #2931971)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define d double
#define in int
#define si(x) scanf('%d', &x)
#define sl(x) scanf('%lld', &x)
#define ss(s) scanf('%s', s)
#define pi(x) printf('%d', x)
#define pl(x) printf('%lld', x)
#define ps(s) printf('%s', s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
#define modulo 1000000007
#define FOR(i,a,b) for(int i=a;i<=b;i++)
typedef vector<pii> vpii;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int dim = 100005;
vector<int> father(dim + 1);
vector<int> height(dim + 1);
int n,k, operatie, x, y;
class Union_Find{
public:
void Init(int x){
father[x] = height[x] = 0;
}
int Find(int x){
if(father[x] == 0)
return x;
father[x] = Find(father[x]);
return father[x];
}
void Union(int x, int y){
int rx = Find(x);
int ry = Find(y);
if(height[rx] < height[ry]){
father[rx] = ry;
}
else{
if(height[rx] > height[ry])
father[ry] = rx;
else{
father[ry] = rx;
height[rx]+=1;
}
}
}
void display(){
for(int i=1;i<=n;i++){
cout<<father[i]<<" ";
}
cout<<endl;
}
void menu(){
fin>>n>>k;
for(int i=1;i<=n;i++)
Init(i);
for(int i=1;i<=k;i++){
fin>>operatie>>x>>y;
if(operatie == 1){
Union(x,y);
}
else{
int rx = Find(x);
int ry = Find(y);
if(rx == ry)
fout<<"DA"<<endl;
else
fout<<"NU"<<endl;
}
}
}
};
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
Union_Find x;
x.menu();
return 0;
}