Pagini recente » Cod sursa (job #1284806) | Cod sursa (job #2200087) | Cod sursa (job #2786695) | Cod sursa (job #42503) | Cod sursa (job #2285839)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int n,m;
vector<int> p;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int father(int nod)
{
if(nod == p[nod])
return nod;
return p[nod] = father(p[nod]);
}
void reuniune(int x, int y)
{
p[father(y)] = p[father(x)];
}
void rezolvare2(int x, int y)
{
if(father(x) == father(y))
g<<"DA\n";
else g<<"NU\n";
}
void solve()
{
int cod,x,y;
for(int i = 1; i <= m; i++)
{
f>>cod>>x>>y;
if(cod == 1)
reuniune(x, y);
else
rezolvare2(x, y);
}
}
int main()
{
f>>n>>m;
p.push_back(0);
for(int i = 1; i <= n; i++)
p.push_back(i);
solve();
return 0;
}