Pagini recente » Cod sursa (job #624141) | Cod sursa (job #1973552) | Cod sursa (job #1121248) | Cod sursa (job #1371240) | Cod sursa (job #1921854)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int N, V[100001], NRV[100001];
int stramos(int x){
if(V[x] == x)
return x;
V[x] = stramos(V[x]);
return V[x];
}
int main()
{
int m, i, v, x, y, nx, ny;
f >> N >> m;
for(i = 1; i <= N; i++){
V[i] = i;
NRV[i] = 1;
}
for(i = 1; i <= m; i++){
f >> v >> x >> y;
if(v == 1){
nx = stramos(x);
ny = stramos(y);
if(NRV[nx] > NRV[ny])
V[nx] = V[ny];
else
V[ny] = V[nx];
}
else{
if(stramos(x) == stramos(y))
g << "DA";
else
g << "NU";
g << '\n';
}
}
return 0;
}