Pagini recente » Cod sursa (job #2033049) | Rating Tudor Cebere (tudorcebere) | Statistici Gociu Maria Anastasia (Maryy_1369) | Cod sursa (job #439085) | Cod sursa (job #2935441)
#include <vector>
#include <fstream>
#include <cstdlib>
#define MAX_N 100011
using namespace std;
vector< int > G[MAX_N];
int f[MAX_N], r[MAX_N], cc[MAX_N];
int Find( int x )
{
int y, z;
for( y=f[x]; y != f[y]; y=f[y] );
while( x != f[x] )
{
z=f[x];
f[x]=y;
x=z;
}
return y;
}
int Union( int x, int y )
{
if( r[x] <= r[y] )
f[x]=y;
else f[y]=x;
if( r[x] == r[y] )
++r[y];
}
inline void DFS( int x, int father )
{
f[x]=father;
r[x]=2;
vector < int >::iterator it=G[x].begin(), iend=G[x].end();
for( ; it < iend; ++it )
if( !f[*it] )
DFS( *it);
}
int main( void )
{
int N, M, Q, x, y, nr_cc=0;
ifstream in( "disjoint.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for( x=1; x <= N; ++x )
if( !f[x] )
{
cc[x]=++nr_cc;
DFS(x);
}
for( in>>Q; Q; --Q )
{
in>>i>>x>>y;
if( 1 == x )
{
cout<<( find(x) == find(y) ? "DA\n" : "NU\n" );
}
else {
x=find(x); y=find(y);
if( x != y )
Union( x, y );
}
}
return 0;
}