Advertisement
vlatkovski

(WIP) Sherlock and Queries on the Graph

Apr 2nd, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef pair<int, int> pi;
  5. typedef vector<int> vi;
  6.  
  7. /*
  8. site: HackerRank
  9. problem: Sherlock and Queries on the Graph
  10.  
  11. najdi ja tetratkata so bomba, pisuva "POWER" vo portokalovo i "time" vo belo na crna pozadina
  12. resenieto na list e posle esejot za makedonski
  13. se otkazav od pisuvanje
  14. */
  15.  
  16. const int MAXN = 100005;
  17. const int MAXLOG = 20;
  18.  
  19. int N, M, Q;
  20. vector<vector<int>> G[MAXN];
  21.  
  22. int numcalls;
  23. int num[MAXN], low[MAXN];
  24. vector<pair<int, int>> bridges;
  25. unordered_set<int> blocked[MAXN];
  26.  
  27. void artb(int nd, int prnt) {
  28.     num[nd] = low[nd] = numcalls++;
  29.     for (int to : G[nd]) {
  30.         if (num[to] == -1) {
  31.             artb(to, nd);
  32.             if (low[to] > num[nd]) { // bridge
  33.                 bridges.emplace_back(nd, to);
  34.                 blocked[nd].insert(to);
  35.                 blocked[to].insert(nd);
  36.             }
  37.             low[nd] = min(low[nd], low[to]);
  38.         } else if (to != prnt) {
  39.             low[nd] = min(low[nd], num[to]);
  40.         }
  41.     }
  42. }
  43.  
  44. int numcc;
  45. int ccid[MAXN];
  46. vector<vector<int>> H[MAXN];
  47.  
  48. void makecc(int nd) {
  49.     ccid[nd] = numcc;
  50.     for (int to : G[nd]) {
  51.         if (blocked[nd].count(to) == 0) {
  52.             makecc(to);
  53.         }
  54.     }
  55. }
  56.  
  57. int depth[MAXN];
  58. int P[MAXN][MAXLOG];
  59.  
  60. void getdepth(int u, int p, int d) {
  61.     depth[u] = d;
  62.     for (int v : H[u]) {
  63.         if (v != p) {
  64.             P[v][0] = u;
  65.             getdepth(v, u, d+1);
  66.         }
  67.     }
  68. }
  69.  
  70. struct lcadata {
  71.     int node, int dist1, int dist2;
  72.     lcadata(int a, int b, int c) {
  73.         node = a, dist1 = b, dist2 = c;
  74.     }
  75.     lca() {}
  76. };
  77.  
  78. lcadata LCA(int a, int b) {
  79.     bool swapped = false;
  80.     if (depth[a] < depth[b]) {
  81.         swapped = true;
  82.         swap(a, b);
  83.     }
  84.     lcadata res(0, 0, 0);
  85.     for (int j = MAXLOG-1; j >= 0; --j) {
  86.         if (depth[a] - (1 << j) >= depth[b]) {
  87.             res.dist1 += (1 << j);
  88.             a = P[a][j];
  89.         }
  90.     }
  91.     if (swapped) swap(res.dist1, res.dist2);
  92.     if (a == b) return res;
  93.     for (int j = MAXLOG-1; j >= 0; --j) {
  94.         if (P[a][j] != -1 && P[a][j] != P[b][j]) {
  95.             res.dist1 += (1 << j);
  96.             res.dist2 += (1 << j);
  97.             a = P[a][j];
  98.             b = P[b][j];
  99.         }
  100.     }
  101.     res.dist1 += 1;
  102.     res.dist2 += 1;
  103.     res.node = P[a][0];
  104.     return res;
  105. }
  106.  
  107. int ccnd1, ccnd2, cclca;
  108.  
  109. int firstnode(int a) {
  110.     int b = a, j = 0;
  111.     while (true) {
  112.         if (incc(b)) {
  113.            
  114.         }
  115.     }
  116. }
  117.  
  118. void Main() {
  119.     cin >> N >> M >> Q;
  120.     for (int i = 0; i < M; ++i) {
  121.         int a, b;
  122.         cin >> a >> b;
  123.         G[a].push_back(b);
  124.         G[b].push_back(a);
  125.     }
  126.     memset(num, -1, sizeof(num));
  127.     numcalls = 0;
  128.     for (int i = 1; i <= N; ++i) {
  129.         if (num[i] == -1) {
  130.             artb(i, -1);
  131.         }
  132.     }
  133.     memset(ccid, -1, sizeof(ccid));
  134.     numcc = 0;
  135.     for (auto& edge : bridges) {
  136.         int u = edge.first, v = edge.second;
  137.         if (ccid[u] == -1) {
  138.             makecc(u);
  139.             ++numcc;
  140.         }
  141.         if (ccid[v] == -1) {
  142.             makecc(v);
  143.             ++numcc;
  144.         }
  145.         H[ccid[u]].push_back(ccid[v]);
  146.         H[ccid[v]].push_back(ccid[u]);
  147.     }
  148.     memset(P, -1, sizeof(p));
  149.     getdepth(0, -1, 0);
  150.     for (int j = 1; j < MAXLOG; ++j) {
  151.         for (int i = 0; i < numcc; ++i) {
  152.             if (P[i][j-1] != -1) {
  153.                 P[i][j] = P[ P[i][j-1] ][j-1];
  154.             }
  155.         }
  156.     }
  157.     while (Q--) {
  158.         int a, b, c, d;
  159.         cin >> a >> b >> c >> d;
  160.        
  161.     }
  162. }
  163.  
  164. int main() {
  165.     ios::sync_with_stdio(false);
  166.     cin.tie(0);
  167.     #ifdef _DEBUG
  168. //    freopen("in.txt", "r", stdin);
  169. //    freopen("out.txt", "w", stdout);
  170.     #endif
  171.     Main();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement