Pagini recente » Cod sursa (job #2939660) | Cod sursa (job #2473993) | Cod sursa (job #428093) | Cod sursa (job #2224661) | Cod sursa (job #3246121)
#include <algorithm>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
const int nmax = 1005;
int n, q, t[nmax], nr[nmax];
int multime(int x)
{
if(t[x] == x)
return x;
int aux = multime(t[x]);
t[x] = aux;
return t[x];
}
void reuniune(int x, int y)
{
int rx = multime(x);
int ry = multime(y);
if(rx == ry)
return;
if(nr[rx] < nr[ry])
t[rx] = ry, nr[ry] += nr[rx], nr[rx] = 0;
else
t[ry] = rx, nr[rx] += nr[ry], nr[ry] = 0;
}
bool query(int x, int y){
return (multime(x) == multime(y));
}
int main()
{
f >> n >> q;
for(int i = 1; i <= n; i ++){
t[i] = i;
nr[i] = 1;
}
for(int i = 1; i <= q; i ++)
{
int tip, x, y;
f >> tip >> x >> y;
if(tip == 1)
reuniune(x, y);
else
g << (query(x, y) ? "DA" : "NU") << '\n';
}
return 0;
}