Pagini recente » Cod sursa (job #2961676) | Cod sursa (job #52201) | Cod sursa (job #2631966) | Cod sursa (job #2533401) | Cod sursa (job #2556471)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tati[100005], n, m;
int getRoot(int x)
{
if(tati[x] == 0)
return x;
int tata = getRoot(tati[x]);
tati[x] = tata;
return tata;
}
void mergeTogether(int x, int y)
{
int xRoot = getRoot(x);
int yRoot = getRoot(y);
tati[xRoot] = yRoot;
}
void checkTogether(int x, int y)
{
int xRoot = getRoot(x);
int yRoot = getRoot(y);
if(xRoot == yRoot)
g<<"DA\n";
else
g<<"NU\n";
}
void solve()
{
f>>n>>m;
int command, x, y;
for(int i = 0; i<m; ++i)
{
f>>command>>x>>y;
if(command == 1)
mergeTogether(x, y);
else
checkTogether(x, y);
}
}
int main()
{
solve();
return 0;
}