Pagini recente » Cod sursa (job #1158611) | Cod sursa (job #2813790) | Cod sursa (job #1732995) | Cod sursa (job #2480210) | Cod sursa (job #2429565)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int NMAX=100000;
int t[NMAX+2],h[NMAX+2];
void Raspuns(int tx,int ty){
if(tx==ty) fout<<"DA\n";
else fout<<"NU\n";
}
void UnionSet(int x,int y){
if(h[x]>h[y]) t[y]=x;
else
if(h[x]<h[y]) t[x]=y;
else { t[y]=x; h[x]++; }
}
int FindSet(int x){
while(t[x]!=x) x=t[x];
return x;
}
int main()
{
int n,m,i,caz,x,y,tx,ty;
fin>>n>>m;
for(i=1;i<=n;i++){
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++){
fin>>caz>>x>>y;
tx=FindSet(x); ty=FindSet(y);
if(caz==1) UnionSet(tx,ty);
else Raspuns(tx,ty);
}
return 0;
}