Pagini recente » Statistici Justin Andrei (Justinelu1998) | Cod sursa (job #1081373) | Cod sursa (job #2512985) | Cod sursa (job #2376795) | Cod sursa (job #2815019)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define N 100001
using namespace std;
int T[N]; //vectorul de tati
int S[N]; //vector de sizes la apm, aici e vector de rang, echivalent
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int find(int x){
while (x != T[x]) {
x = T[x];
}
return x;
}
void connect(int x, int y)
{
int T_X = find(x), T_Y = find(y);
if (S[T_X] >= S[T_Y]) {
T[T_Y] = T_X;
S[T_X] += S[T_Y];
} else {
S[T_Y] += S[T_X];
T[T_X] = T_Y;
}
}
int main()
{
int n, m, cod, x, y;
fin>>n>>m;
for (int i=1; i<=n; i++) {
T[i] = i;
S[i] = 1;
}
for (int i=1; i<=m; i++)
{
fin >> cod >> x >> y;
if (cod == 1)
{
connect(x,y);
}
if(cod == 2)
{
if (find(x) == find(y))
{
fout<<"DA"<<endl;
}
else
{
fout<<"NU"<<endl;
}
}
}
return 0;
}