Pagini recente » Cod sursa (job #1148778) | Cod sursa (job #2779689) | Cod sursa (job #1817493) | Cod sursa (job #696411) | Cod sursa (job #1442180)
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
int *sp;
int findsp(int x) {
if (sp[x] == x) return x;
else return sp[x] = findsp(sp[x]);
}
void unite(int x, int y) {
int tx,ty;
tx = findsp(x);
ty = findsp(y);
sp[tx] = ty;
}
int main()
{
int n,m,o,x,y,i;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n;
f>>m;
sp = new int[n+1];
for (i=1;i<=n;i++) sp[i] = i;
while (f>>o>>x>>y) {
if (o == 1) {
unite(x,y);
}
if (o == 2) {
int tx = findsp(x);
int ty = findsp(y);
if (tx == ty) g<<"DA";
else g<<"NU";
g<<"\n";
}
}
return 0;
}