Pagini recente » Cod sursa (job #2158123) | Cod sursa (job #2330548) | Rating Resiga Sorana (Sorana132004) | Cod sursa (job #2931289) | Cod sursa (job #2220001)
#include <fstream>
using namespace std;
const int nmax=100000;
int r[nmax],h[nmax];
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int findset(int x) {
while(r[x]!=x) {
x=r[x];
}
return x;
}
void unionset(int x,int y)
{
if(h[x]==h[y])
{
h[x]++;
r[y]=x;
}
else
{
if(h[x]>h[y])
{
r[y]=x;
}
else r[x]=y;
}
}
int main() {
int n,m,i,x,y,rx,ry,cod;
in>>n>>m;
for(i=1; i<=n; i++) {
r[i]=i;
h[i]=1;
}
for(i=1; i<=m; i++) {
in>>cod;
in>>x>>y;
rx=findset(x);
ry=findset(y);
if(cod==2) {
if(rx==ry)out<<"DA\n";
else out<<"NU\n";
} else {
if(rx!=ry) {
unionset(rx,ry);
}
}
}
return 0;
}