Pagini recente » Cod sursa (job #290913) | Cod sursa (job #212797) | Cod sursa (job #2439634) | Cod sursa (job #2152939) | Cod sursa (job #2390704)
#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)
{
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]) cout << "DA\n";
else cout <<"NU\n";
}
}
return 0;
}