Pagini recente » Cod sursa (job #51813) | Cod sursa (job #719341) | Cod sursa (job #901221) | Cod sursa (job #2132319) | Cod sursa (job #2977027)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[100005], h[100005];
int n, m;
void init() {
for (int i=1; i<=n; i++) {
t[i]=i;
h[i]=1;
}
}
int find(int x) {
int r, y;
for (r=x; t[r]!=r; r=t[r]);
while (t[x]!=x) {
y=t[x];
t[x]=r;
x=y;
}
return r;
}
void unite(int x, int y) {
if (h[x]>h[y])
t[y]=x;
else t[x]=y;
if (h[x]==h[y])
h[y]++;
}
void verif_cod(int cod, int x, int y, int i) {
if (cod==2) {
if (find(x)==find(y))
fout<<"DA\n";
else
fout<<"NU\n";
} else {
if (find(x)==find(y)) {
fout<<i;
exit(0);
}
unite(find(x), find(y));
}
}
void citire() {
int cod, x, y;
for (int i=1; i<=m; i++) {
fin>>cod>>x>>y;
verif_cod(cod, x, y, i);
}
}
int main() {
fin>>n>>m;
init();
citire();
return 0;
}