Pagini recente » Cod sursa (job #2334842) | Cod sursa (job #2969882) | Rating Chili Ivona Alexandra (ivona) | Cod sursa (job #2069210) | Cod sursa (job #2488783)
#include <fstream>
#include <algorithm>
using namespace std;
long long t[400010];
long long i,n,m,tip,rx,ry,x,y;
long long Radacina_lui (long long x) {
while (t[x]>0) {
x=t[x];
}
return x;
}
int main() {
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin>>n>>m;
for (i=1;i<=n;i++) {
t[i]=-1;
}
for (i=1;i<=m;i++) {
fin>>tip>>x>>y;
rx=Radacina_lui(x);
ry=Radacina_lui(y);
if (tip==1) {
if (rx!=ry) {
if (t[rx]<t[ry]) {
t[rx]+=t[rx];
t[ry]=rx;
}
else {
t[ry]+=t[rx];
t[rx]=ry;
}
}
}
else {
if (rx==ry) fout<<"DA"<<"\n";
else fout<<"NU"<<"\n";
}
}
return 0;
}