Pagini recente » Cod sursa (job #26419) | Cod sursa (job #1402994) | Cod sursa (job #1128010) | Cod sursa (job #1417679) | Cod sursa (job #2285837)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int n,m;
int p[10001];
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;
for(int i = 1; i <= n; i++)
p[i] = i;
solve();
return 0;
}