Pagini recente » Cod sursa (job #1497383) | Cod sursa (job #50189) | Cod sursa (job #1882648) | Cod sursa (job #1375946) | Cod sursa (job #1413897)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N, M;
int T[100005];
void update(int x, int y)
{
if (x > y)
swap(x, y);
T[x] = y;
}
int query(int x)
{
if (T[x] == x)
return x;
T[x] = query(T[x]);
return T[x];
}
int main()
{
fin >> N >> M;
for (int i = 1; i <= N; ++i)
T[i] = i;
int type, x, y;
for (int i = 1; i <= M; ++i)
{
fin >> type >> x >> y;
int rad1 = query(x);
int rad2 = query(y);
if (type == 1)
update(rad1, rad2); // legam radacinile, nu nodurile
else
{
if (rad1 == rad2)
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}