Pagini recente » Cod sursa (job #3194054) | Cod sursa (job #2392847) | Cod sursa (job #2466250) | Cod sursa (job #315961) | Cod sursa (job #2736406)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int repr[100004];
int height[100004];
void R(int n)
{
for(int i=1;i<=n;i++)
repr[i]=i;
for(int i=1;i<=n;i++)
height[i]=0;
}
int reprezentant(int x) {
int r = x;
while (r != repr[r]) {
r = repr[r];
}
while (x != repr[x]) {
int next = repr[x];
repr[x] = r;
x = next;
}
return r;
}
void joinHeight(int x, int y) {
int rx = reprezentant(x);
int ry = reprezentant(y);
if (rx == ry) return;
if (height[rx] > height[ry]) {
repr[ry] = rx;
} else if(height[rx] < height[ry]) {
repr[rx] = ry;
} else {
repr[rx] = ry;
height[ry] += 1;
}
}
bool inSameSet(int x, int y) {
if (reprezentant(x) == reprezentant(y)) return true;
else return false;
}
int main()
{
int n;
fin>>n;
int m;
fin>>m;
R(n);
while(m)
{
int cod,x,y;
fin>>cod>>x>>y;
if(cod==1)
{
joinHeight(x,y);
}
if(cod==2)
{
if(inSameSet(x,y))
fout<<"DA"<<"\n";
else
fout<<"NU"<<"\n";
}
m--;
}
}