Pagini recente » Cod sursa (job #1868894) | Cod sursa (job #1957683) | Cod sursa (job #2700203) | Cod sursa (job #1616834) | Cod sursa (job #2930622)
#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;
if(cod == 1) {
unionn(find(x), find(y));
} else if(cod == 2) {
if( find(x) == find(y) )
fout << "DA\n";
else fout << "NU\n";
}
}
return 0;
}