Pagini recente » Borderou de evaluare (job #2735589) | Cod sursa (job #3155142) | Diferente pentru problema/deque intre reviziile 7 si 8 | Borderou de evaluare (job #1518958) | Cod sursa (job #2807124)
#include <bits/stdc++.h>
struct repRang{
int rep;
int rang;
};
const int nmax = 100005;
using namespace std;
class Graf{
public:
static int findRep(repRang*, int);
static void reunion(repRang*, int, int);
static void disjoint();
};
int Graf::findRep(repRang* info, int x){
if(x == info[x].rep)
return x;
return (info[x].rep = findRep(info, info[x].rep));
}
void Graf::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;
}
}
void Graf::disjoint(){
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
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);
}
}
int main()
{
Graf::disjoint();
return 0;
}