Pagini recente » Cod sursa (job #495633) | Cod sursa (job #2547529) | Cod sursa (job #1982270) | Cod sursa (job #758202) | Cod sursa (job #2930621)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, cod, x, y;
vector< pair<int, int> > tata; //tata, rank
int find(int nod) {
if( tata[nod].first != nod )
tata[nod].first = find(tata[nod].first);
return tata[nod].first;
}
void unionn(int x, int y) {
if( tata[x].second < tata[y].second )
tata[x].first = y;
else if( tata[x].second > tata[y].second )
tata[y].first = x;
else {
tata[y].first = x;
tata[x].second++;
}
}
int main() {
fin >> n >> m;
tata.reserve(n + 3);
for(int i = 1; i <= n; i++)
tata[i] = make_pair(i, 0);
for(int i = 0; i < m; i++) {
fin >> cod >> x >> y;
int tataX = find(x);
int tataY = find(y);
if(cod == 1) {
unionn(x, y);
} else if(cod == 2) {
if( tataX == tataY )
fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}