Pagini recente » Cod sursa (job #226008) | Cod sursa (job #2279747) | Cod sursa (job #1448433) | Cod sursa (job #2801698) | Cod sursa (job #1435206)
#include<fstream> // Paduri de multimi disjuncte (DSU) Disjoint O(log star n) aprox O(1).
#define NMAX 100020
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int root[NMAX];
int rang[NMAX];
int i,n,t;
int find(int x)
{
int r, aux;
for(r=x; root[r]!=r; r=root[r]);
while(root[x]!=x) // compresia drumurilor // euristica(II)
{
aux=root[x]; // the parent of the current node
root[x]=r; // set the parent of the current node equal to the root found
x=aux; // move up and set parents equal to the root found till the root is reached
}
return r;
}
void unite(int x, int y)
{
if(rang[x] < rang[y]) // radacina cu rang mai mic lipim la cea mai mare (euristica I)// rang mai mic la rang mai mare
root[x] = y;
else
root[y] = x;
if(rang[x] == rang[y]) rang[y]++;
}
int main()
{
cin>>n>>t;
for(i=1; i<=n; ++i)
{
rang[i]=1;
root[i]=i;
}
while(t--)
{
int c,x,y;
cin>>c>>x>>y;
if(c==1)
unite(find(x), find(y));
else
if(find(x)==find(y))
cout<<"DA"<<endl;
else
cout<<"NU"<<endl;
}
return 0;
}