Pagini recente » Cod sursa (job #3235518) | Cod sursa (job #972097) | Cod sursa (job #3281723) | Cod sursa (job #1956503) | Cod sursa (job #2390652)
#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(b > a) swap(b, a);
for(int i = 1; i <= n; i++)
{
if(comp[i] == comp[b])
{
comp[i] = comp[a];
noduri_comp[i].push_back(i);
}
}
}
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;
if(optiune == 1)
{
f >> x >> y;
reuneste(lista_adiacenta, x, y, n);
}
if(optiune == 2)
{
f >> x >> y;
if(comp[x] == comp[y]) cout << "DA\n";
else cout <<"NU\n";
}
}
return 0;
}