bingxuan9112

BCC簡單路徑判斷

Jan 29th, 2021
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef local
  3. #define debug(args...) qqbx(#args, args)
  4. template <typename ...T> void qqbx(const char *s, T ...args) {
  5.     int cnt = sizeof...(T);
  6.     ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << args << (--cnt ? ", " : ")\033[0m\n")));
  7. }
  8. #define TAK(args...) std::ostream& operator<<(std::ostream &O, args)
  9. #define NIE(STL, BEG, END, OUT) template <typename ...T> TAK(std::STL<T...> v) \
  10.     { O << BEG; int f=0; for(auto e: v) O << (f++ ? ", " : "") << OUT; return O << END; }
  11. NIE(vector, "[", "]", e)
  12. #else
  13. #define debug(...) ((void)0)
  14. #endif // local
  15. #define pb emplace_back
  16. #define all(v) begin(v),end(v)
  17. #define sort_uni(v) sort(all(v)), v.erase(unique(all(v)),v.end())
  18. #define get_pos(v,x) int(lower_bound(all(v),x)-v.begin())
  19.  
  20. using namespace std;
  21. const int N = 300001;
  22.  
  23. vector<int> g[N], stk, bccv[N];
  24. int vis[N], low[N], tot;
  25. int bccn, bcc[N];
  26. bool ap[N];
  27. void dfs(int i, int p) {
  28.     int child = 0;
  29.     vis[i] = low[i] = ++tot;
  30.     stk.pb(i);
  31.     for(int j: g[i]) {
  32.         if(j == p) continue;
  33.         if(vis[j]) {
  34.             low[i] = min(low[i], vis[j]);
  35.         } else {
  36.             ++child;
  37.             dfs(j, i);
  38.             if(low[j] >= vis[i]) {
  39.                 int x;
  40.                 do {
  41.                     x = stk.back(), stk.pop_back();
  42.                     bcc[x] = bccn;
  43.                     bccv[bccn].pb(x);
  44.                 } while(x != j);
  45.                 bcc[i] = bccn;
  46.                 bccv[bccn].pb(i);
  47.                 ++bccn;
  48.                 ap[i] = true;
  49.             }
  50.             low[i] = min(low[i], low[j]);
  51.         }
  52.     }
  53.     if(p == -1 && child == 1)
  54.         ap[i] = false;
  55. }
  56. vector<int> tr[N*2];
  57. int in[N*2], ou[N*2], cnt;
  58. int pa[20][N*2], dep[N*2];
  59. bool isap[N*2];
  60. bool isAnc(int p, int i) {
  61.     return in[p] <= in[i] && in[i] < ou[p];
  62. }
  63. void DFS(int i, int p) {
  64.     in[i] = cnt++;
  65.     for(int j: tr[i]) {
  66.         if(j != p) {
  67.             pa[0][j] = i;
  68.             dep[j] = dep[i] + 1;
  69.             DFS(j, i);
  70.         }
  71.     }
  72.     ou[i] = cnt;
  73. }
  74. int LCA(int a, int b) {
  75.     if(dep[a] > dep[b]) swap(a, b);
  76.     int d = dep[b] - dep[a];
  77.     for(int i = 18; i >= 0; i--) if(d >> i & 1) b = pa[i][b];
  78.     if(a == b) return 0;
  79.     for(int i = 18; i >= 0; i--) if(pa[i][a] != pa[i][b]) a = pa[i][a], b = pa[i][b];
  80.     return pa[0][a];
  81. }
  82. bool ok(int a, int b, int c) {
  83.     int lca = LCA(a, b);
  84.     if(!isap[lca] && c==pa[0][lca]) return true;
  85.     if(isap[c] && ok(a, b, pa[0][c])) return true;
  86.     if(isAnc(lca, c) && (isAnc(c, a) || isAnc(c, b))) return true;
  87.     if(isAnc(c, lca) && (isAnc(a, b) || isAnc(b, a))) return true;
  88.     return false;
  89. }
  90. signed main() {
  91.     ios_base::sync_with_stdio(0), cin.tie(0);
  92.     int n, m, q;
  93.     cin >> n >> m >> q;
  94.     for(int i = 0; i < m; i++) {
  95.         int a, b;
  96.         cin >> a >> b;
  97.         --a, --b;
  98.         g[a].pb(b), g[b].pb(a);
  99.     }
  100.     dfs(0, -1);
  101.     for(int i = 0; i < n; i++) if(ap[i]) {
  102.         bcc[i] = bccn++;
  103.         isap[bcc[i]] = true;
  104.     }
  105.     debug(bccn);
  106.     for(int i = 0; i < bccn && i < N; i++)
  107.         for(int j: bccv[i])
  108.             if(ap[j])
  109.                 tr[i].pb(bcc[j]), tr[bcc[j]].pb(i);
  110.     for(int i = 0; i < bccn; i++)
  111.         sort_uni(tr[i]), debug(i, tr[i]);
  112.     DFS(0, -1);
  113.     // return 0;
  114.     for(int L = 1; L < 20; L++)
  115.         for(int i = 0; i < bccn; i++)
  116.             pa[L][i] = pa[L-1][pa[L-1][i]];
  117.     while(q--) {
  118.         int a, b, c;
  119.         cin >> a >> b >> c;
  120.         --a, --b, --c;
  121.         a = bcc[a], b = bcc[b], c = bcc[c];
  122.         if(ok(a, b, c) || ok(c, a, b) || ok(b, c, a))
  123.             cout << "Yes\n";
  124.         else
  125.             cout << "No\n";
  126.     }
  127. }
  128.  
  129. /*
  130. 6 7 0
  131. 1 2
  132. 2 3
  133. 3 1
  134. 4 5
  135. 5 6
  136. 6 4
  137. 1 4
  138.  
  139. 6 6 0
  140. 1 2
  141. 2 3
  142. 3 1
  143. 1 4
  144. 2 5
  145. 3 6
  146.  */
  147.    
Advertisement
Add Comment
Please, Sign In to add comment