Pagini recente » Cei mai harnici utilizatori infoarena | Cod sursa (job #1976201) | Cod sursa (job #646322) | Cod sursa (job #1705187) | Cod sursa (job #1804954)
#include <stdio.h>
#define buff_size 200000
using namespace std;
int depth[100001],boss[100001],n,m,t,x,y,aux;
char buff[buff_size],bufferw[buff_size];int pos,pozw;
FILE*f=freopen("disjoint.in","r",stdin);
FILE*g=freopen("disjoint.out","w",stdout);
inline void read(int &nr)
{
while(buff[pos] < '0' || buff[pos] > '9') if(++pos == buff_size) fread(buff, buff_size,1, stdin), pos = 0;
nr = 0;
while('0' <= buff[pos] && buff[pos] <= '9')
{
nr = nr * 10 + buff[pos] - '0';
if(++pos == buff_size) fread(buff, 1, buff_size, stdin), pos = 0;
}
}
inline void putch( char ch ){
bufferw[ pozw++ ] = ch;
if( pozw == buff_size ){
fwrite( bufferw, buff_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()
{ int n,m,i,type,i1,i2;
FILE *f = fopen("disjoint.in", "r");
FILE *g = fopen("disjoint.out","w");
fread(buff,buff_size,1,stdin);
read(n);read(m);
for(i = 1; i <= 100000; ++i)
{
depth[i] = 1;
boss[i] = i;
}
for(i = 1; i <= m; ++i)
{
read(type); read(i1);read(i2);
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<buff_size ) fwrite( bufferw, pozw, 1, stdout);
return 0;
}