Pagini recente » Cod sursa (job #1560443) | Cod sursa (job #3228680) | Cod sursa (job #2695998) | Cod sursa (job #2248150) | Cod sursa (job #1538955)
#include<fstream>
#include<vector>
using namespace std;
vector<int> t, rg;
int find(int x)
{
int rez = x, c;
while(rez != t[rez])
rez = t[rez];
while(t[x] != x)
{
c = t[x];
t[x] = rez;
x = c;
}
return rez;
}
void unite(int x, int y)
{
int _x = find(x), _y = find(y);
if(rg[_x] < rg[_y])
t[_x] = _y;
else
t[_y] = _x;
if(rg[_x] == rg[_y])
rg[_y]++;
}
int main()
{
fstream in("disjoint.in", ios::in), out("disjoint.out", ios::out);
int n, i, op, x, y;
in>>n;
for(i = 0; i<n; i++)
{
t.push_back(i);
rg.push_back(1);
}
in>>n;
for(i=0; i < n; i++)
{
in>>op>>x>>y;
x--;
y--;
if(op == 1)
unite(x,y);
else
if(find(x) == find(y))
out<<"DA\n";
else
out<<"NU\n";
}
in.close();
out.close();
return 0;
}