Pagini recente » Cod sursa (job #217314) | Cod sursa (job #66178) | Cod sursa (job #1270266) | Cod sursa (job #54834) | Cod sursa (job #1313244)
#include <fstream>
#define nmax 100005
//cu optimizare
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m;
int t[nmax];
int rad(int x)
{
int y=x;
int aux;
while(t[x]>0) {
x=t[x];
}
while (t[y]>0) {
aux=t[y];
t[y]=x;
y=aux;
}
return x;
}
void leaga(int a,int b)
{
t[a]+=t[b];
t[b]=a;
}
int main()
{
int i,j,x,y;
int rx,ry,op;
f>>n>>m;
for (i=1;i<=n;i++) t[i]=-1;
for (i=1;i<=m;i++) {
f>>op>>x>>y;
rx=rad(x);
ry=rad(y);
if (op==2) {
if (rx==ry)
g<<"DA\n";
else
g<<"NU\n";
}
else {
if (rx==ry) continue;
if (t[rx]<t[ry])
leaga(rx,ry);
else
leaga(ry,rx);
}
}
return 0;
}