Pagini recente » Cod sursa (job #2754509) | Cod sursa (job #2638344)
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("disjoint.in");
ofstream o("disjoint.out");
short type;
int u, v;
int g[100005];
int find(int x)
{
int y2 = x, t;
while (y2 != g[y2])
{
y2 = g[y2];
}
while (x != y2)
{
t = g[x];
g[x] = y2;
x = t;
}
return x;
}
int main()
{
f >> u >> v;
for (size_t i = 1; i <= u; i++)
{
g[i] = i;
}
int x, y;
while (v--)
{
f >> type >> x >> y;
if (type == 1) //adauga
{
if (find(x) != find(y))
{
int sefx = find(x);
int sefy = find(y);
g[sefy] = sefx;
}
}
else // nu / da
{
if (find(x) == find(y))
{
o << "DA" << "\n";
}
else
{
o<< "NU" << "\n";
}
}
}
}