Advertisement
erek1e

A Splash of Milk - BIO 2023 Round 2

Apr 11th, 2023
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     freopen("input.txt", "r", stdin);
  7.     freopen("output.txt", "w", stdout);
  8.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  9.     int n; cin >> n;
  10.     vector<vector<int>> g(n);
  11.     int g1, g2;
  12.     while (cin >> g1 >> g2 && g1 != -1) {
  13.         --g1, --g2;
  14.         g[g1].push_back(g2);
  15.         g[g2].push_back(g1);
  16.     }
  17.  
  18.     if (g[0].size() < 2) {
  19.         cout << 1 << endl;
  20.         return 0;
  21.     }
  22.  
  23.     vector<bool> notThree(n);
  24.     for (int i = 0; i < n; ++i) {
  25.         if (g[i].size() < 3) notThree[i] = true;
  26.     }
  27.  
  28.  
  29.     // bfs from root
  30.     vector<int> distFromRoot(n, -1);
  31.     distFromRoot[0] = 0;
  32.     queue<int> q;
  33.     q.push(0);
  34.     while (!q.empty()) {
  35.         int v = q.front();
  36.         q.pop();
  37.         for (int u : g[v]) {
  38.             if (distFromRoot[u] == -1) {
  39.                 distFromRoot[u] = distFromRoot[v] + 1;
  40.                 q.push(u);
  41.             }
  42.         }
  43.     }
  44.  
  45.     int minBad = n;
  46.  
  47.     // case 1: get to node with degree < 3
  48.     for (int v = 1; v < n; ++v) {
  49.         if (notThree[v] && distFromRoot[v] != -1) minBad = min(minBad, distFromRoot[v] + 1);
  50.     }
  51.  
  52.     // case 2: reach and traverse a cycle
  53.     for (int pivot = 0; pivot < n; ++pivot) { // reach pivot which is on a cycle
  54.         if (distFromRoot[pivot] == -1) continue;
  55.  
  56.         // optimisation to ignore pivots that can never help
  57.         //  cycleLen >= 3, so total cost will be at least distFromRoot[pivot] + 3 - 1 = distFromRoot[pivot] + 2
  58.         if (distFromRoot[pivot] + 2 >= minBad) continue;
  59.  
  60.         // max useful cycle:
  61.         //  distFromRoot[pivot] + cycleLen - 1 < minBad
  62.         //  cycleLen - 1 < minBad - distFromRoot[pivot]
  63.         //  dist[i] + dist[j] < minBad - distFromRoot[pivot]
  64.  
  65.         // bfs from pivot
  66.         while (!q.empty()) q.pop();
  67.         vector<int> dist(n, -1), previous(n, -1);
  68.         dist[pivot] = 0;
  69.         q.push(pivot);
  70.         while (!q.empty()) {
  71.             int v = q.front();
  72.             if (dist[v] + dist[v] - 1 >= minBad - distFromRoot[pivot]) break; // optimisation
  73.             q.pop();
  74.             for (int u : g[v]) {
  75.                 if (dist[u] == -1) {
  76.                     dist[u] = dist[v] + 1;
  77.                     previous[u] = v;
  78.                     q.push(u);
  79.                 } else if (u != previous[v] && v != previous[u]) {
  80.                     minBad = min(minBad, distFromRoot[pivot] + dist[u] + dist[v]);
  81.                 }
  82.             }
  83.         }
  84.  
  85. //      for (int i = 0; i < n; ++i) {
  86. //          if (distFromRoot[i] == -1) continue;
  87. //          for (int j : g[i]) {
  88. //              if (previous[i] != j && previous[j] != i) {
  89. //                  int cycleLen = dist[i] + dist[j] + 1;
  90. //                  minBad = min(minBad, distFromRoot[pivot] + cycleLen - 1);
  91. //              }
  92. //          }
  93. //      }
  94.     }
  95.  
  96.     cout << minBad << endl;
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement