Pagini recente » Cod sursa (job #1568055) | Cod sursa (job #1145427) | Cod sursa (job #1487731) | Cod sursa (job #1815712) | Cod sursa (job #609634)
Cod sursa(job #609634)
# include <fstream>
using namespace std;
int n, m, tata[100100], grad[100100];
int find (int x)
{
int r = x;
while (tata[r] > 0)
r = tata[r];
/*int nr = x;
while (nr != r)
{
int aux = tata[nr];
tata[nr] = r;
nr = aux;
}*/
return r;
}
void update (int x, int y)
{
if (grad[x] > grad[y])
tata[x] = y, ++grad[x];
else
tata[y] = x, ++grad[y];
}
int main ()
{
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
f >> n >> m;
for (int i = 1; i <= m; ++i)
{
int o, x, y;
f >> o >> x >> y;
if (o == 1)
update (find (x), find (y));
else
if (find (x) == find (y)) g << "DA\n";
else g << "NU\n";
}
g.close ();
return 0;
}