Pagini recente » Cod sursa (job #912114) | Cod sursa (job #2474415) | Cod sursa (job #1008912) | Cod sursa (job #1235007) | Cod sursa (job #3189607)
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int Nmax = 100005;
int father[Nmax];
int find_root(int x){
if(father[x]<0)
return x;
int root = find_root(father[x]);
father[x] = root;
return root;
}
void Union(int x, int y){
int rx = find_root(x);
int ry = find_root(y);
if(rx == ry)
return;
if(father[rx]<father[ry])
swap(rx,ry);
father[ry]+=father[rx];
father[rx] = ry;
}
int main()
{
int n,m,i;
fin >> n >> m;
for(i=1;i<=n;i++)
father[i] = -1;
for(i=1;i<=m;i++){
int type,x,y;
fin >> type >> x >> y;
if(type == 1)
Union(x,y);
if(type == 2){
if(find_root(x) == find_root(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}