Pagini recente » Cod sursa (job #2808923) | Borderou de evaluare (job #3098530) | Cod sursa (job #2802571) | Monitorul de evaluare | Cod sursa (job #2801980)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax = 100005;
struct repRang{
int rep;
int rang;
};
int findRep(repRang* info, int x){
if(x == info[x].rep)
return x;
return (info[x].rep = findRep(info, info[x].rep));
}
void reunion(repRang* info, int x, int y){
int repX = findRep(info, x);
int repY = findRep(info, y);
if(info[repX].rang > info[repY].rang)
info[repX].rep = repY;
else
if(info[repX].rang < info[repY].rang)
info[repY].rep = repX;
else
{
info[repX].rang++;
info[repY].rep = repX;
}
}
int main(){
repRang* info = new repRang[nmax];
int N, M;
fin >> N >> M;
for(int i = 1; i <= N; ++i)
info[i].rep = i;
for(int i = 1; i <= M; ++i){
int cod, x, y;
fin >> cod >> x >> y;
if(cod == 2)
if(findRep(info, x) == findRep(info, y))
fout << "DA\n";
else
fout << "NU\n";
else
reunion(info, x, y);
}
return 0;
}