Pagini recente » Clasament remediere | Cod sursa (job #2005057) | Profil mihaipriboi | Monitorul de evaluare | Cod sursa (job #2841640)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int NMAX = 100001;
int n, m, ROOT[NMAX], GR[NMAX];
int root(int x);
void unite (int x, int y);
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; ++i)
{
ROOT[i] = i;
GR[i] = 1;
}
int x,y,q;
for (int i = 1; i <= m; ++i)
{
fin >> q >> x >> y;
if (q == 1) unite(x,y);
else {
if (root(x) == root(y)) fout << "DA" << ' ';
else fout << "NU" << ' ';
}
}
return 0;
}
int root(int x)
{
if (x == ROOT[x]) return x;
ROOT[x] = root(ROOT[x]);
}
void unite(int x, int y)
{
if (GR[x] > GR[y])
{
ROOT[y] = ROOT[x];
GR[x] += GR[y];
}
else {
ROOT[x] = ROOT[y];
GR[y] += GR[x];
}
}