Pagini recente » Cod sursa (job #14712) | Cod sursa (job #1210533) | Cod sursa (job #2218470) | Cod sursa (job #2109726) | Cod sursa (job #2390675)
#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(a < b)
{
for(auto i : noduri_comp[a])
{
comp[i] = b;
noduri_comp[b].push_back(i);
}
noduri_comp[a].clear();
}
else
{
for(auto i : noduri_comp[b])
{
comp[i] = 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;
if(optiune == 1)
{
f >> x >> y;
if(comp[x]!=comp[y])
reuneste(lista_adiacenta, comp[x], comp[y], n);
}
if(optiune == 2)
{
f >> x >> y;
if(comp[x] == comp[y]) cout << "DA\n";
else cout <<"NU\n";
}
}
return 0;
}