Pagini recente » Cod sursa (job #452455) | Cod sursa (job #2253525) | Cod sursa (job #289692) | Cod sursa (job #2813067) | Cod sursa (job #2424294)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int paduri[100005], rang[100005];
int n, m;
int Find(int x){
if(paduri[x] == x)
return x;
int aux = Find(paduri[x]);
paduri[x] = aux;
return aux;
}
void unio(int x, int y){
if(rang[x] < rang[y]){
paduri[x] = y;
rang[y] += rang[x];
}
else
{
paduri[y] = x;
rang[x] += rang[y];
}
}
int main()
{
f>>n>>m;
for(int i = 1; i <= n; i ++)
{
rang[i] = 1;
paduri[i] = i;
}
for(int i = 0; i < m; i ++)
{
int cod, x, y;
f>>cod>>x>>y;
if(cod == 2){
if(Find(x) == Find(y))
g<<"DA\n";
else g<<"NU\n";
}
else
unio(Find(x), Find(y));
}
return 0;
}