Pagini recente » Cod sursa (job #609382) | Cod sursa (job #3143636) | Cod sursa (job #2156781) | Cod sursa (job #1892067) | Cod sursa (job #2447918)
#include <bits/stdc++.h>
using namespace std;
ifstream f ( "disjoint.in" );
ofstream g ( "disjoint.out" );
int tati[100005],rang[100005];
int reprezentant(int nod)
{ if(tati[nod]==nod) return nod;
return(tati[nod]=reprezentant(tati[nod]));
}
void reuniune(int i,int j)
{ int ri=reprezentant(i);
int rj=reprezentant(j);
if(ri!=rj)
{ if(rang[ri]>rang[rj])
{ rang[ri]+=rang[rj];
tati[rj]=ri;
}
else
{ rang[rj]+=rang[ri];
tati[ri]=rj;
}
}
}
int main()
{ int n,m;
f>>n>>m;
for(int i=1;i<=n;i++)
{ tati[i]=i;
rang[i]=1;
}
while(m--)
{ int tip,i,j;
f>>tip>>i>>j;
if(tip==2)
{ if(reprezentant(i)!=reprezentant(j)) g<< "NU\n";
else g<< "DA\n";
}
else reuniune(i,j);
}
return 0;
}