Pagini recente » Cod sursa (job #2404662) | Cod sursa (job #2070722) | Cod sursa (job #1642494) | Cod sursa (job #258103) | Cod sursa (job #2936787)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
vector<int>tata(100001);
int n, m, operatie, x, y;
int reprezentant(int nod){ //reprezinta radacina unei componente conexe
while(tata[nod] != nod){
nod=tata[nod];
}
return nod;
}
void reuniune(int x, int y){
int reprezentant_x = reprezentant(x), reprezentant_y = reprezentant(y);
//leg reprezentantul lui y direct de reprezentantul lui x(radacina componentei conexe care-l contine pe x)
//tata[reprezentant_y] = reprezentant_x;
tata[reprezentant_x] = reprezentant_y;
}
int main() {
fin>>n>>m;
for(int i=1; i<=n; i++)
tata[i]=i;
for(int i=0; i<m; i++){
fin>>operatie>>x>>y;
if(operatie==1)
reuniune(x, y);
else{
if(reprezentant(x) == reprezentant(y)) fout << "DA\n";
else fout<<"NU\n";
}
}
//cout<<reprezentant(1)<<" "<<reprezentant(6);
//cout<<rang[1]<<" "<<rang[6];
return 0;
}