Pagini recente » Cod sursa (job #1311477) | Cod sursa (job #2529088) | Cod sursa (job #3291559) | Cod sursa (job #1065902) | Cod sursa (job #1360563)
// disjoint
#include <iostream>
#include <fstream>
#define NMax 100005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n,m;
int FA[NMax], GR[NMax];
int fatherOf(int x) {
int e;
for (e=x;FA[e]!=e;e=FA[e]);
for (;FA[x]!=x;) {
int aux = FA[x];
FA[x] = e;
x = aux;
}
return e;
}
void join(int i, int j) {
i = fatherOf(i);
j = fatherOf(j);
if (i == j)
return;
if (GR[i] > GR[j])
FA[j] = i;
else if (GR[i] < GR[j])
FA[i] = j;
else {
FA[i] = j;
GR[j]++;
}
}
void read() {
f>>n>>m;
for (int i=1;i<=n;i++) {
FA[i] = i;
GR[i] = 1;
}
for (int i=1;i<=m;i++) {
int type; f>>type;
int x,y; f>>x>>y;
if (type == 1) {
join(x,y);
} else if (fatherOf(x) != fatherOf(y)) {
g<<"NU\n";
} else
g<<"DA\n";
}
}
int main() {
read();
f.close(); g.close();
return 0;
}