Pagini recente » Cod sursa (job #2499023) | Cod sursa (job #641302) | Cod sursa (job #132101) | Cod sursa (job #1646936) | Cod sursa (job #900177)
Cod sursa(job #900177)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
#define nmax 100005
#define ll long long
int n, m, t[nmax];
void citeste(){
f >> n >> m;
}
int radacina(int x){
int x2 = x;
for(; t[x]!=0; x=t[x]);// caut radacina din care face parte
while(x2 != x){// acum marchez multiimile de pe drum
int aux = t[x2];
t[x2] =x;
x2 = aux;
}
return x;
}
void uneste(int x, int y){
t[x] = y;
}
void rezolva(){
int tip, x, y;
for(int i=1; i<=m; ++i){
f >> tip >> x >> y;
if (tip == 1){
uneste( radacina(x), radacina(y) );
}else {
if (radacina(x) == radacina(y)){
g << "DA" << "\n";
}else g << "NU" << "\n";
}
}
}
int main(){
citeste();
rezolva();
f.close();
g.close();
return 0;
}