Pagini recente » Cod sursa (job #2324650) | Cod sursa (job #2084620) | Cod sursa (job #153396) | Rating Teofil (Teofilos) | Cod sursa (job #1413680)
#include <fstream>
const int NMAX = 100005;
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N,M;
int type,x,y;
int TT[NMAX];
void Update(int x, int y)
{
if (x > y)
swap(x,y);
TT[x] = y;
}
int Query(int x)
{
if (TT[x] == x)
return x;
TT[x] = Query(TT[x]);
return TT[x];
}
int main()
{
f >> N >> M;
for (int i = 1; i <= N; ++i)
{
TT[i] = i;
}
for (int i = 1; i <= M; ++i)
{
f >> type >> x >> y;
if (type == 1)
{
Update(Query(x),Query(y));
}
if (type == 2)
{
if (Query(x) == Query(y))
g << "DA\n";
else g << "NU\n";
}
}
f.close();
g.close();
return 0;
}