Pagini recente » Rating Zagrean Mircea (big-s) | Cod sursa (job #1314408) | Cod sursa (job #2447875) | Cod sursa (job #2431242) | Cod sursa (job #719897)
Cod sursa(job #719897)
#include <fstream>
#define nmax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, t[nmax];
void citeste(){
f >> n >> m;
}
int radacina(int x){
int i;
for(i=x; t[i]; i=t[i]);
int j = x;
while(j!=i){
int aux = j;
t[j] = i;
j = t[aux];
}
return i;
}
void uneste(int x, int y){
x = radacina(x);
y = radacina(y);
t[x] = y;
}
int multime(int x, int y){
x = radacina(x);
y = radacina(y);
if (x == y) return 1;
return 0;
}
void rezolva(){
for(int i=1; i<=m; i++){
int tip, x, y;
f >> tip >> x >> y;
if (tip==1){
uneste(x,y);
}
else if(tip == 2){
if (multime(x,y) > 0) g << "DA" << "\n";
else g << "NU" <<"\n";
}
}
}
int main(){
citeste();
rezolva();
f.close();
g.close();
return 0;
}