Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iostream>
  7. #include <map>
  8. #include <set>
  9. using namespace std;
  10.  
  11. typedef long long llint;
  12.  
  13. #define pii pair< int, int >
  14. #define mp make_pair
  15. #define pb push_back
  16. #define lu lower_bound
  17.  
  18. const int N = 200100;
  19. const int INF = 0x3f3f3f3f;
  20. const int LG = 19;
  21. const int MOD = 1000000007;
  22.  
  23. int n;
  24. int q;
  25. int m;
  26. int bio[ N ];
  27. //0 -> nikad nije bio
  28. //1 -> gotov
  29. //2 -> aktivan
  30. int cycle[ N ];
  31. int depth[ N ];
  32. bool is[ N ];
  33. int pot2[ N ];
  34.  
  35.  
  36. vector< int > graf[ N ], veze[ N ];
  37. int dp[ LG ][ N ];
  38. int sum[ N ];
  39.  
  40. vector< int > V;
  41. int which_cycle;
  42.  
  43. inline void load( ) {
  44. scanf( "%d %d", &n, &m );
  45. for( int i = 0; i < m; i++ ) {
  46. int x, y;
  47. scanf( "%d %d", &x, &y );
  48. --x;
  49. --y;
  50. veze[ x ].pb( y );
  51. veze[ y ].pb( x );
  52. }
  53. }
  54.  
  55. void find( int node, int par = -1 ) {
  56. if( bio[ node ] == 1 ) return;
  57. if( bio[ node ] == 2 ) {
  58. for( int i = ( int ) V.size( ) - 1; i >= 0; i-- ) {
  59. cycle[ V[ i ] ] = which_cycle;
  60. if( V[ i ] == node ) break;
  61. }
  62. which_cycle++;
  63. return;
  64. }
  65. bio[ node ] = 2;
  66. V.pb( node );
  67. for( int i = 0; i < veze[ node ].size( ); i++ ) {
  68. if( veze[ node ][ i ] != par ) find( veze[ node ][ i ], node );
  69. }
  70. V.pop_back( );
  71. bio[ node ] = 1;
  72. }
  73.  
  74. inline void DEBUG1( ) {
  75. printf( "CIKLUSI\n" );
  76. for( int i = 0; i < n; i++ ) {
  77. printf( "node == %d -> %d\n", i + 1, cycle[ i ] );
  78. }
  79. }
  80.  
  81. inline void find_cycles( ) {
  82. which_cycle = n;
  83. memset( cycle, -1, sizeof( cycle ) );
  84. find( 0 );
  85. //DEBUG1( );
  86. }
  87.  
  88. inline void rebuild_graph( ) {
  89. for( int i = 0; i < n; i++ ) {
  90. if( cycle[ i ] == -1 ) cycle[ i ] = i, is[ i ] = true;
  91. }
  92. for( int i = 0; i < n; i++ ) {
  93. for( int j = 0; j < veze[ i ].size( ); j++ ) {
  94. int x = cycle[ i ];
  95. int y = cycle[ veze[ i ][ j ] ];
  96. if( x == y ) continue;
  97. graf[ x ].pb( y );
  98. graf[ y ].pb( x );
  99. }
  100. }
  101. }
  102.  
  103. void dfs( int node, int par = -1, int dub = 0 ) {
  104. depth[ node ] = dub;
  105. if( !is[ node ] ) sum[ node ]++;
  106. if( par != -1 ) sum[ node ] += sum[ par ];
  107. dp[ 0 ][ node ] = par;
  108. bio[ node ] = 1;
  109. for( int i = 0; i < graf[ node ].size( ); i++ ) {
  110. if( bio[ graf[ node ][ i ] ] ) continue;
  111. dfs( graf[ node ][ i ], node, dub + 1 );
  112. }
  113. }
  114.  
  115. inline void precomputation_lca( ) {
  116. memset( bio, 0, sizeof( bio ) );
  117. memset( dp, -1, sizeof( dp ) );
  118. dfs( cycle[ 0 ] );
  119. for( int j = 1; j < LG; j++ ) {
  120. for( int i = 0; i < n; i++ ) {
  121. if( dp[ j - 1 ][ i ] == -1 ) continue;
  122. dp[ j ][ i ] = dp[ j - 1 ][ dp[ j - 1 ][ i ] ];
  123. }
  124. }
  125. }
  126.  
  127. inline int lca( int x, int y ) {
  128. if( depth[ x ] < depth[ y ] ) swap( x, y );
  129. // printf( "aa == %d %d\n", x, y );
  130. // for( int i = 0; i < 5; i++ ) printf( "%d ", dp[ i ][ x ] );
  131. // system( "pause" );
  132. for( int i = LG - 1; i >= 0; i-- ) {
  133. if( depth[ x ] - ( 1 << i ) >= depth[ y ] ) x = dp[ i ][ x ];
  134. }
  135. if( x == y ) return x;
  136. for( int i = LG - 1; i >= 0; i-- ) {
  137. if( dp[ i ][ x ] != dp[ i ][ y ] ) {
  138. x = dp[ i ][ x ];
  139. y = dp[ i ][ y ];
  140. }
  141. }
  142. return dp[ 0 ][ x ];
  143. }
  144.  
  145. inline void precompute_pot2( ) {
  146. pot2[ 0 ] = 1;
  147. for( int i = 1; i <= n; i++ ) {
  148. pot2[ i ] = pot2[ i - 1 ] * 2 % MOD;
  149. }
  150. }
  151.  
  152. inline void get_ansewer( int x, int y ) {
  153. int LCA = lca( x, y );
  154. // printf( "x, y %d %d\n", x, y );
  155. // printf( "LCA == %d\n", LCA );
  156. int ans = sum[ x ] + sum[ y ] - 2 * sum[ LCA ] + ( is[ LCA ] == 0 );
  157. printf( "%d\n", pot2[ ans ] );
  158. }
  159.  
  160.  
  161.  
  162. inline void solve( ) {
  163. // printf( "SOLVE\n" );
  164. // system( "pause" );
  165. find_cycles( );
  166. // system( "pause" );
  167. rebuild_graph( );
  168. // system( "pause" );
  169. n *= 2;
  170. precomputation_lca( );
  171. // system( "pause" );
  172. precompute_pot2( );
  173. scanf( "%d", &q );
  174. // printf( "q == %d\n", q );
  175. for( ; q; q-- ) {
  176. int x, y;
  177. scanf( "%d %d", &x, &y );
  178. --x;
  179. --y;
  180. x = cycle[ x ];
  181. y = cycle[ y ];
  182. get_ansewer( x, y );
  183. }
  184. }
  185.  
  186.  
  187. int main( void ) {
  188. load( );
  189. solve( );
  190. // system( "pause" );
  191. return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement