fork download
  1. /*
  2.  * J1K7_7
  3.  *
  4.  * You can use my contents on a Creative Commons - Attribution 4.0 International - CC BY 4.0 License. XD
  5.  * - JASKAMAL KAINTH
  6.  */
  7. #include <iostream>
  8. #include <sstream>
  9. #include <fstream>
  10. #include <string>
  11. #include <vector>
  12. #include <deque>
  13. #include <queue>
  14. #include <stack>
  15. #include <set>
  16. #include <cstring>
  17. #include <cassert>
  18. #include <list>
  19. #include <map>
  20. #include <unordered_map>
  21. #include <iomanip>
  22. #include <algorithm>
  23. #include <functional>
  24. #include <utility>
  25. #include <bitset>
  26. #include <cmath>
  27. #include <cstdlib>
  28. #include <ctime>
  29. #include <cstdio>
  30. #include <limits>
  31. using namespace std;
  32. typedef long long ll;
  33. typedef unsigned long long ull;
  34. typedef long double ld;
  35. typedef pair<int,int> pii;
  36. typedef pair<ll,ll> pll;
  37. typedef vector<int> vi;
  38. typedef vector<long long> vll;
  39. #define left(x) (x << 1)
  40. #define right(x) (x << 1) + 1
  41. #define mid(l, r) ((l + r) >> 1)
  42. #define mp make_pair
  43. #define pb push_back
  44. #define all(a) a.begin(),a.end()
  45. #define ff first
  46. #define ss second
  47. const int maxn = 3e5+7;
  48. int n; // number of nodes
  49.  
  50. vector<int> G[maxn];
  51. int low[maxn], disc[maxn] , parent[maxn] , vis[maxn];
  52. vector<pair<int,int> > bedge; // all bridge edges
  53. int timer;
  54. vector<int> Btree[maxn];
  55.  
  56. int rankN[maxn];
  57. int parentN[maxn];
  58. inline void make_set()
  59. {
  60. for(int i = 0; i < n; i++)
  61. parentN[i] = rankN[i] = i;
  62. }
  63. inline int find_set(int u)
  64. {
  65. if(parentN[u] != u )
  66. parentN[u] = find_set(parentN[u]);
  67. return parentN[u];
  68. }
  69. inline void union_set(int u,int v)
  70. {
  71. int pu = find_set(u);
  72. int pv = find_set(v);
  73. if(pu == pv)
  74. return ;
  75. if(rankN[pu] > rankN[pv])
  76. {
  77. parentN[pv] = pu;
  78. }
  79. else
  80. {
  81. parentN[pu] = pv;
  82. if(rankN[pu] == rankN[pv])
  83. rankN[pv]++;
  84. }
  85. }
  86. inline void init_bridge_tree()
  87. {
  88. make_set();
  89. timer = 0;
  90. for(int i = 0; i < maxn; i++)
  91. {
  92. low[i] = disc[i] = vis[i] = 0;
  93. parent[i] = -1;
  94. }
  95. }
  96. /* Mark all the bridges */
  97. inline void mark_bridges(int u)
  98. {
  99. vis[u] = 1;
  100. disc[u] = low[u] = timer++;
  101. for(int &v: G[u])
  102. {
  103. if(!vis[v])
  104. {
  105. parent[v] = u;
  106. mark_bridges(v);
  107. low[u] = min(low[u],low[v]);
  108. if(low[v] > disc[u])
  109. {
  110. bedge.push_back(mp(u,v));
  111. }
  112. else
  113. {
  114. union_set(u,v);
  115. }
  116. }
  117. else if(parent[u] != v)
  118. {
  119. low[u] = min(low[u],disc[v]);
  120. }
  121. }
  122. }
  123. inline void make_tree()
  124. {
  125. init_bridge_tree();
  126. mark_bridges(0); // mark all the bridges
  127. for(auto &i: bedge) // atmose n-1 bridge edges.
  128. {
  129. int pu = find_set(i.ff);
  130. int pv = find_set(i.ss);
  131. if(pu != pv)
  132. {
  133. Btree[pu].push_back(pv);
  134. Btree[pv].push_back(pu);
  135. }
  136. }
  137. }
  138. const int MAXN = 3e5+7;
  139. const int MAXLOG = 20;
  140.  
  141. int P[MAXN][MAXLOG]; // initially all -1
  142. int d[MAXN];
  143. void dfs1(int v,int p = -1){
  144. P[v][0] = p;
  145. if(p + 1)
  146. d[v] = d[p] + 1;
  147. for(int i = 1;i < MAXLOG;i ++)
  148. if(P[v][i-1] + 1)
  149. P[v][i] = P[P[v][i-1]][i-1];
  150. for(auto u : Btree[v])
  151. if(p - u)
  152. dfs1(u,v);
  153. }
  154. void process3(int N = n)
  155. {
  156. int i, j;
  157. for (i = 0; i < N; i++)
  158. for (j = 0; 1 << j < N; j++)
  159. P[i][j] = -1;
  160. for (j = 1; 1 << j < N; j++)
  161. for (i = 0; i < N; i++)
  162. if (P[i][j - 1] != -1)
  163. P[i][j] = P[P[i][j - 1]][j - 1];
  164. }
  165.  
  166. int query(int p, int q)
  167. {
  168. int tmp, log, i;
  169. if (d[p] < d[q])
  170. tmp = p, p = q, q = tmp;
  171. for (log = 1; 1 << log <= d[p]; log++);
  172. log--;
  173. for (i = log; i >= 0; i--)
  174. if (d[p] - (1 << i) >= d[q])
  175. p = P[p][i];
  176. if (p == q)
  177. return p;
  178. for (i = log; i >= 0; i--)
  179. if (P[p][i] != -1 && P[p][i] != P[q][i])
  180. p = P[p][i], q = P[q][i];
  181. return P[p][0];
  182. }
  183.  
  184. int dist(int u , int v)
  185. {
  186. int l =query( u , v);
  187. return ( d[u] + d[v] - 2 * d[l] );
  188. }
  189. int main()
  190. {
  191. ios_base::sync_with_stdio(false); cin.tie(0);
  192.  
  193. int m , q; cin >> n >> m >> q;
  194.  
  195. init_bridge_tree();
  196. for(int i = 0; i < m ; i++)
  197. {
  198. int u , v; cin >> u >> v;
  199. u--; v--;
  200. G[u].pb(v);
  201. G[v].pb(u);
  202. }
  203.  
  204. make_set();
  205. make_tree();
  206. process3();
  207. dfs1(find_set(0));
  208.  
  209. while(q--)
  210. {
  211. int a , b; cin >> a >> b;
  212. a--; b--;
  213. int pi = find_set(a);
  214. int pj = find_set(b);
  215. if(pi == pj )
  216. {
  217. cout << "0" << "\n";
  218. }
  219. else
  220. {
  221. cout << dist(pi,pj) << "\n";
  222. }
  223. }
  224. return 0;
  225. }
  226.  
  227.  
Success #stdin #stdout 0s 42144KB
stdin
8 9 4
1 2
1 3
2 3
2 4
4 5
4 7
4 8
7 8
8 6
4 6
1 6
2 5
7 5
stdout
1
2
2
1