Pagini recente » Cod sursa (job #2966808) | Cod sursa (job #1313902) | Cod sursa (job #1842686) | Cod sursa (job #483290) | Cod sursa (job #1024190)
#include<fstream>
#include<cstring>
#define DIM 10000
using namespace std;
//Varianta 1 : fara euristice
int t[100001];
int p[100001];
int h[100001];
int r[100001];
char buff[10000];
int n,m,x,y,tip,poz;
ifstream in("disjoint.in"); ofstream out("disjoint.out");
void cit(int &nr){
nr=0;
while('0'>buff[poz] || buff[poz]>'9'){
if(++poz==DIM) in.read(buff,DIM),poz=0;
}
while('0'<=buff[poz] && buff[poz]<='9'){
nr=nr*10+buff[poz]-'0';
if(++poz==DIM) in.read(buff,DIM),poz=0;
}
}
int radacina(int x){
if(t[p[x]]==0) return p[x];
int nr=0;
while(t[x]!=0){
r[++nr]=x;
x=t[x];
}
for(int i=1;i<=nr;++i) p[r[i]]=x;
return x;
}
void reunit(int x, int y){
int tx=radacina(x);
int ty=radacina(y);
if(h[tx]>h[ty]){ //cel mai mic se adauga la cel mai mare
h[tx]+=h[ty];
t[ty]=x;
}
else{
h[ty]+=h[tx];
t[tx]=y;
}
}
int main(){
cit(n); cit(m);
for(int i=1;i<=n;++i) h[i]=1,p[i]=i;
for(int i=1;i<=m;++i){
cit(tip); cit(x); cit(y);
if(tip==1) reunit(x,y);
else{
if(radacina(x)==radacina(y)) out<<"DA\n";
else out<<"NU\n";
}
}
out.close();
return 0;
}