Pagini recente » Cod sursa (job #1634135) | Cod sursa (job #1836862) | Borderou de evaluare (job #1566706) | Cod sursa (job #594388) | Cod sursa (job #2390710)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int comp[100001];
void reuneste(vector <vector <int > > &noduri_comp, int a, int b, int n)
{
a = comp[a];
b = comp[b];
if(noduri_comp[comp[a]].size() < noduri_comp[comp[b]].size())
{
int ca = comp[a];
for(auto i : noduri_comp[ca])
{
comp[i] = comp[b];
noduri_comp[b].push_back(i);
}
noduri_comp[a].clear();
}
else
{
int cb = comp[b];
for(auto i : noduri_comp[cb])
{
comp[i] = comp[a];
noduri_comp[a].push_back(i);
}
noduri_comp[b].clear();
}
}
int main()
{
int n, m, x, y;
f >> n >> m;
vector< vector< int > > lista_adiacenta(n+1);
for(int i = 1; i <= n; i++)
{
comp[i] = i;
lista_adiacenta[i].push_back(i);
}
for(int i = 0; i < m; i++)
{
int optiune;
f >> optiune >> x >> y;
if(optiune == 1)
{
if(comp[x]!=comp[y])
reuneste(lista_adiacenta, x, y, n);
}
if(optiune == 2)
{
if(comp[x] == comp[y]) g << "DA\n";
else g <<"NU\n";
}
}
return 0;
}