Pagini recente » Cod sursa (job #196765) | Cod sursa (job #3275801) | Cod sursa (job #1820295) | Cod sursa (job #258608) | Cod sursa (job #1804957)
#include <stdio.h>
#include <ctype.h>
#define BUF_SIZE 4096
int depth[100001];
int boss[100001];
char buf[BUF_SIZE],bufferw[BUF_SIZE];
int pos = BUF_SIZE, pozw;
inline char getChar(FILE *f) {
if (pos == BUF_SIZE) {
fread(buf, 1, BUF_SIZE, f);
pos = 0;
}
return buf[pos++];
}
inline int read(FILE *f) {
int result = 0;
char c;
do {
c = getChar(f);
} while (!isdigit(c));
do {
result = 10 * result + c - '0';
c = getChar(f);
} while (isdigit(c));
return result;
}
inline void putch( char ch ){
bufferw[ pozw++ ] = ch;
if( pozw == BUF_SIZE ){
fwrite( bufferw, BUF_SIZE, 1, stdout );
pozw = 0;
}
}
int find(int u)
{
if(u == boss[u])
return u;
return boss[u] = find(boss[u]);
}
void unify(int u1, int u2)
{
int x = find(u1);
int y = find(u2);
if(x != y)
{
if(depth[x] > depth[y])
{
boss[x] = boss[y];
depth[y] += depth[x];
}
else
{
boss[y] = boss[x];
depth[x] += depth[y];
}
}
}
int main()
{
FILE *f = fopen("disjoint.in", "r");
FILE *g=freopen("disjoint.out","w",stdout);
int n,m,i,type,i1,i2;
n = read(f);
m = read(f);
for(i = 1; i <= 100000; ++i)
{
depth[i] = 1;
boss[i] = i;
}
for(i = 1; i <= m; ++i)
{
type = read(f); i1 = read(f); i2 = read(f);
if(type == 1)
unify(i1,i2);
else
{
if( find(i1) == find(i2) )
{
putch( 'D' );
putch( 'A' );
putch( '\n' );
}
else
{
putch( 'N' );
putch( 'U' );
putch( '\n' );
}
}
}
if( pozw < BUF_SIZE ) fwrite( bufferw, pozw, 1, stdout);
return 0;
}