Pagini recente » Cod sursa (job #1400175) | Cod sursa (job #2374409) | Cod sursa (job #1989022) | Cod sursa (job #2447896) | Cod sursa (job #2166707)
#include <fstream>
#define BufferSize 100000
#define Nmax 100002
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int p,n,m,T[Nmax],t,a,b;
char buffer[BufferSize];
bool digit(int a){return (a<='9' && a>='0');}
void read(int &x){
x = 0;
while (!digit(buffer[p])){
p++;
if (p==BufferSize){
p = 0;
f.read(buffer,BufferSize);
}
}
while (digit(buffer[p])){
x = x * 10 + buffer[p] - '0';
p++;
if (p==BufferSize){
p = 0;
f.read(buffer, BufferSize);
}
}
}
int root(int x){
int rx = x,sav;
while (T[rx]!=rx) rx = T[rx];
while (T[x]!=x) sav = T[x],T[x] = rx,x = sav;
return x;
}
void _union(int x,int y){
int rx,ry;
rx = root(x);
ry = root(y);
if (rx==ry) return;
T[rx] = ry;
}
bool _find(int x,int y){
int rx,ry;
rx = root(x);
ry = root(y);
return rx==ry;
}
int main()
{
read(n);read(m);
for(int i=1;i<=n;i++) T[i] = i;
for (int i=1;i<=m;i++){
read(t);read(a);read(b);
if (t==1) _union(a,b);
else g<<(_find(a,b) ? "DA\n" : "NU\n");
}
return 0;
}