Pagini recente » Monitorul de evaluare | Diferente pentru utilizator/andrici_cezar intre reviziile 178 si 171 | Diferente pentru documentatie/borderoul-de-evaluare intre reviziile 15 si 7 | Cufar | Cod sursa (job #2849096)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, m;
vector<int> r;
vector<int> p;
int find(int x)
{
while (x != p[x])
x = p[x];
return x;
}
void make_union(int x, int y)
{
x = find(x);
y = find(y);
if (r[x] == r[y])
r[x]++;
if (r[x] > r[y])
p[y] = x;
else
p[x] = y;
}
int main()
{
in >> n >> m;
int x, y, z;
for (int i = 0; i <= n; i++)
{
p.push_back(i);
r.push_back(0);
}
for (int i = 0; i < m; i++)
{
in >> x >> y >> z;
if (x == 1)
make_union(y, z);
else
{
if (find(y) == find(z))
out << "DA\n";
else
out << "NU\n";
}
}
}