Pagini recente » Cod sursa (job #863016) | Cod sursa (job #1734539) | Cod sursa (job #1275285) | Cod sursa (job #1249464) | Cod sursa (job #2944071)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n, m, colors[100001], mult1, mult2, time[300001], cnt = 1;
int check(int nod){
if(colors[nod] == nod){
return nod;
}
colors[nod] = check(colors[nod]);
return colors[nod];
}
int main() {
in >> n >> m;
for(int i = 1; i <= n; i++){
colors[i] = i;
}
int nr, a, b;
while(m--){
in >> nr >> a >> b;
if(nr == 1){
if(time[min(a, b)] == 0)
time[min(a, b)] = cnt, cnt++;
if(time[max(a, b)] == 0)
time[max(a, b)] = cnt, cnt++;
if(time[a] < time[b])
colors[check(b)] = colors[check(a)];
else
colors[check(a)] = colors[check(b)];
}
else
{
mult1 = check(a);
mult2 = check(b);
out << (mult1 == mult2 ? "DA" : "NU") << '\n';
}
}
return 0;
}