Pagini recente » Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #149764) | Cod sursa (job #2019460)
#include <fstream>
#define DIM 100010
using namespace std;
int n, m;
int c[DIM], t[DIM];
inline void Union(int x, int y)
{
t[y] = x;
c[x] += c[y];
}
int Find(int x)
{
int r = x, y;
while (t[r] != 0)
{
r = t[r];
}
while (x != r)
{
y = t[x];
t[x] = r;
x = y;
}
return r;
}
int main()
{
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f >> n >> m;
for (int i = 1;i <= n;++i)
c[i] = 1;
for (int i = 1;i <= m;++i)
{
int cod, x, y;
f >> cod >> x >> y;
switch (cod)
{
case 1:
{
x = Find(x);
y = Find(y);
if (x != y)
Union(x, y);
break;
}
case 2:
{
x = Find(x);
y = Find(y);
if (x == y)
g << "DA\n";
else
g << "NU\n";
}
}
}
f.close();
g.close();
return 0;
}