Pagini recente » Cod sursa (job #2701827) | Cod sursa (job #2410970) | Cod sursa (job #1370885) | Cod sursa (job #673522) | Cod sursa (job #2640448)
#include <iostream>
#include <fstream>
using namespace std;
int tip,x,y;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAXN = 1e5 + 5;
int t[MAXN],n,m,h[MAXN];
int findroot(int nod) {
if(t[nod] == nod)
return nod;
return t[nod] = findroot(t[nod]);
}
void Union(int rad1, int rad2) {
if(h[rad1] > h[rad2]) {
t[rad2] = rad1;
}
else
if(h[rad1] < h[rad2]) {
t[rad1] = rad2;
}
else // daca au aceeasi inaltime
{
t[rad1] = rad2;
h[rad2]++;
}
}
int main() {
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
t[i] = i;
h[i] = 1;
}
for ( int i =1 ;i <= m; ++i) {
fin >> tip >> x >> y;
if(tip == 1) {
if(findroot(x) != findroot(y)){
Union(findroot(x),findroot(y));
}
}
else
{
if(findroot(x) == findroot(y))
fout <<"DA\n";
else
fout <<"NU\n";
}
}
}
/*int main()
{
cin>>n>>m;
for (i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}*/