Pagini recente » Cod sursa (job #993323) | Cod sursa (job #403516) | Cod sursa (job #1471248) | Cod sursa (job #115151) | Cod sursa (job #1430268)
#include <fstream>
using namespace std;
ifstream f1("disjoint.in");
ofstream f2("disjoint.out");
#define MX 100100
int n, m, tata[MX];
void join(int x, int y)
{
while ( tata[x] )
x= tata[x];
while ( tata[y] )
y= tata[y];
tata[y]= x;
}
bool same_set(int x, int y)
{
while ( tata[x] )
x= tata[x];
while ( tata[y] )
y= tata[y];
return x == y;
}
int main()
{
int i, cod, x, y;
f1>>n>>m;
for (i=1; i<=m; i++)
{
f1>>cod>>x>>y;
if ( cod == 1 )
join(x,y);
else
if ( same_set(x,y) )
f2<<"DA\n";
else f2<<"NU\n";
}
f2.close();
return 0;
}