Pagini recente » Cod sursa (job #1955617) | Cod sursa (job #60342) | Cod sursa (job #1259551) | Cod sursa (job #457937) | Cod sursa (job #674845)
Cod sursa(job #674845)
#include<fstream>
#define lim 100002
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int T[lim],m,n,i,tip,x,y,a,b;
int tata (int x){
int y=x;
while(T[y]>=0){
y=T[y];
}
return y;
}
void lipeste(int a,int b){
if(-T[a]>=-T[b]){
T[a]=T[a]+T[b];
T[b]=a;
}
else{
T[b]=T[a]+T[b];
T[a]=b;
}
}
int main(){
f>>n>>m;
for(i=1;i<=n;i++){
T[i]=-1;
}
for(i=1;i<=m;i++){
f>>tip>>x>>y;
a=tata(x);
b=tata(y);
if(tip==1){
lipeste(a,b);
}
else{
if(a==b)
g<<"DA\n";
else
g<<"NU\n";
}
}
return 0;
}