Pagini recente » Cod sursa (job #2914212) | Borderou de evaluare (job #2279968) | Cod sursa (job #2223831) | Cod sursa (job #53080) | Cod sursa (job #2112306)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
struct nod{
int val;
nod* next;
};
nod* v[100001];
int u1[100001];
int u2[100001];
int k(int a){
nod *p;
for(p=v[a];p->next!=NULL;p=p->next);
return p->val;
}
void update(int a, nod *t){
nod *p,*q;
p = v[a];
q = p;
while(p->next!=NULL){
q = p->next;
p->next = t;
v[p->val] = t;
p = q;
}
v[a] = t;
}
void connect(int a,int b){
nod *p,*q,*t;
int l2 = 0;
int l1 = 0;
for(p=v[a];p->next!=NULL;p=p->next)
++l1;
for(q=v[b];q->next!=NULL;q=q->next)
++l2;
if(l1 > l2)
t = q->next = p,
update(a,t);
else
t = p->next = q,
update(b,t);
//update(b,t);
}
int main()
{
int i,n,m;
int x,y,z;
fin >> n >> m;
for(i=1;i<=n;++i){
v[i] = new nod;
v[i]->val = i;
v[i]->next = NULL;
}
for(i=1;i<=m;++i){
fin >> x >> y >> z;
if(x == 1)
connect(y,z);
else
if( k(y) == k(z) )
fout << "DA\n";
else
fout << "NU\n";
}
return 0;
}