Pagini recente » Cod sursa (job #3330880) | Cod sursa (job #3351669) | Borderou de evaluare (job #2624298) | Cod sursa (job #3344049) | Cod sursa (job #3337504)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream o("disjoint.out");
int n,m;
int owner[100001];
int findsef(int x){
if(owner[x]!=x){
return findsef(owner[x]);
}
else
return x;
}
void unire(int x,int y){
int xsef=findsef(x);
int ysef=findsef(y);
owner[ysef]=xsef;
}
void afis(int x, int y){
if(findsef(x)==findsef(y))
o<<"DA\n";
else
o<<"NU\n";
}
int main()
{
f>>n>>m;
for(int i=1;i<=n;i++)
owner[i]=i;
for(int i=1;i<=m;i++){
int cod, x, y;
f>>cod>>x>>y;
if(cod==1){
unire(x,y);
}
else
afis(x,y);
}
return 0;
}