Pagini recente » Cod sursa (job #1411992) | Cod sursa (job #1037234) | Cod sursa (job #2331178) | Cod sursa (job #1780954) | Cod sursa (job #1179436)
#include <fstream>
#include <iostream>
using namespace std;
int n,m;
int *v, *d;
//d retine dimensiunea temporara a subarborilor
int gaseste_tata(int x)
{
while(x != v[x])
{
v[x] = v[v[x]];
d[x] = d[d[x]];
x = v[x];
}
return x;
}
inline void adauga(int x,int y)
{
int tatax = gaseste_tata(x);
int tatay = gaseste_tata(y);
if(d[tatax] < d[tatay]) //il adaug pe x la y
{
v[x] = tatay;
d[tatay] += d[tatax];
}
else
{
v[y] = tatax;
d[tatax] += d[tatay];
}
}
bool gaseste(int x,int y)
{
return (v[x] == v[y]);
}
void afis()
{
for(int i = 1; i <= n; i++)
cout<<v[i]<<" ";
cout<<'\n';
}
inline void citeste()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
in>>n>>m;
v = new int[n+1];
int i;
for(i = 1; i <= n; i++)
{
v[i] = i;
d[i] = 1;
}
//afis();
//cout<<endl;
int a,b,c;
for(i = 0; i < m; i++)
{
in>>a>>b>>c;
if(a == 1)
{
adauga(b,c);
//afis();
//cin.get();
}
else
{
if(gaseste(b,c))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
in.close();
out.close();
}
int main()
{
citeste();
return 0;
}