Pagini recente » Cod sursa (job #2130632) | Cod sursa (job #1148319) | Cod sursa (job #3262524) | Cod sursa (job #1242359) | Cod sursa (job #2512553)
#include <fstream>
#include <set>
using namespace std;
int n, m;
int mult[100005];
int multimi[100005];
struct lista{
int el;
lista *next = nullptr, *last = this;
}liste[100005];
void reunite(int x, int y){
int mult1 = mult[x], mult2 = mult[y];
lista *p = &liste[mult2];
while(p != nullptr){
mult[p->el] = mult1;
p = p->next;
}
liste[mult1].last->next = &liste[mult2];
liste[mult1].last = liste[mult2].last;
}
int main() {
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for (int i = 1; i <= n; ++i) {
mult[i] = i;
liste[i].el = i;
}
for (int i = 0; i < m; ++i) {
int tip, x, y;
f>>tip>>x>>y;
if(tip == 2){
if(mult[x] == mult[y])
g<<"DA\n";
else g<<"NU\n";
}else{
reunite(x, y);
}
}
return 0;
}