Pagini recente » Autentificare | Cod sursa (job #2685269) | Cod sursa (job #2567875) | Cod sursa (job #1975332) | Cod sursa (job #2658791)
#include <fstream>
#include <vector>
using namespace std;
ifstream f ("disjoint.in");
ofstream g ("disjoint.out");
int n, m;
int poz[100005];
vector < int > v[100005];
void Unire (int x, int y)
{
if (poz[x] == poz[y])
return;
if (v[poz[x]].size() <= v[poz[y]].size())
{
int aux = poz[y];
for (int i=0; i<v[aux].size(); i++)
{
v[poz[x]].push_back(v[aux][i]);
poz[v[aux][i]] = poz[x];
}
}
}
void Querry (int x, int y)
{
if (poz[x] == poz[y])
g << "DA" << "\n";
else g << "NU" << "\n";
}
int main()
{
f >> n >> m;
for (int i=1; i<=n; i++)
{
v[i].push_back(i);
poz[i] = i;
}
while (m --)
{
int type, x, y;
f >> type >> x >> y;
if (type == 1) Unire(x, y);
else Querry(x, y);
}
return 0;
}