Pagini recente » Cod sursa (job #34877) | Cod sursa (job #1416598) | Cod sursa (job #544500) | Cod sursa (job #3234666) | Cod sursa (job #1168260)
#include <fstream>
using namespace std;
int n,m;
int *v;
inline void adauga(int x,int y)
{
int c;
int d;
if(x < y)
{
if(v[x] == 0)
{
v[x] = x;
c = x;
}
else
{
c = v[y];
d = v[x];
}
v[y] = v[x];
}
else
{
if(v[y] == 0)
{
v[y] = y;
c = y;
}
else
{
c = v[y];
d = v[x];
}
v[x] = v[y];
}
int i;
for(i = 1; i <= n; i++)
{
if(v[i] == c)
v[i] = d;
}
}
bool gaseste(int x,int y)
{
return (v[x] == v[y]);
}
inline void citeste()
{
ifstream in("disjoint.in");
ofstream out("disjoint.out");
in>>n>>m;
v = new int[n+1];
int i;
int a,b,c;
for(i = 0; i < m; i++)
{
in>>a>>b>>c;
if(a == 1)
{
adauga(b,c);
}
else
{
if(gaseste(b,c))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
in.close();
out.close();
}
int main()
{
citeste();
return 0;
}