Pagini recente » Cod sursa (job #1268077) | Cod sursa (job #664696) | Cod sursa (job #210607) | Cod sursa (job #1403514) | Cod sursa (job #1377653)
#include <fstream>
#define nmax 100001
using namespace std;
ifstream is ("disjoint.in");
ofstream os ("disjoint.out");
int N, M, x, y, type;
int GR[nmax], NR[nmax];
void Read();
int Find(int);
void Union(int,int);
int main()
{
is >> N >> M;
for(int i = 1; i <= N; ++i)
GR[i] = i;
for(int i = 1; i <= M; ++i)
{
is >> type >> x >> y;
if(type == 1) Union(Find(x), Find(y));
else {
if(Find(x) == Find(y)) os << "DA\n";
else os << "NU\n";
}
}
is.close();
os.close();
return 0;
}
void Union(int x, int y)
{
if(NR[x] > NR[y])
GR[y] = x;
if(NR[y] > NR[x])
GR[x] = y;
if(NR[y] == NR[x])
{
GR[y] = x;
NR[x]++;
}
}
int Find(int x)
{
if(GR[x] == x)
return x;
return Find(GR[x]);
}