Pagini recente » Cod sursa (job #1165897) | Cod sursa (job #1768239) | Cod sursa (job #2010653) | Cod sursa (job #2816997) | Cod sursa (job #2799321)
#include <stdio.h>
#include <ctype.h>
// Program de Mircea Rebengiuc
// inceput pe 2021.11.12
#define MAXN 100000
#define MAXM 100000
#define NIL -1
int dsu[MAXN];
int find( int x ){
if( x == dsu[x] )
return x;
return dsu[x] = find(dsu[x]);
}
static inline void _union( int i, int j ){
dsu[find(i)] = find(j);
}
FILE *fin, *fout;
static inline int fgetint(){
int n = 0, ch;
while( !isdigit(ch = fgetc(fin)) );
do
n = n * 10 + ch - '0';
while( isdigit(ch = fgetc(fin)) );
return n;
}
char *yesno[2] = { "NU\n", "DA\n" };
int main(){
fin = fopen("disjoint.in", "r");
fout = fopen("disjoint.out", "w");
int n, m, type, a, b;
for( n = fgetint() ; n-- ; )
dsu[n] = n;
for( m = fgetint() ; m-- ; ){
type = fgetint();
a = fgetint() - 1;
b = fgetint() - 1;
if( type == 1 )
_union(a, b);
else
fputs(yesno[find(a) == find(b)], fout);
}
fclose(fin);
fclose(fout);
return 0;
}