Advertisement
Guest User

Untitled

a guest
Oct 27th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int upper(int a, int b)
  6. {
  7.     return (a + b - 1) / b;
  8. }
  9.  
  10. struct Element
  11. {
  12.     bool isCycle;
  13.     int length;
  14.     Element() {}
  15.     Element(bool b, int l) : isCycle(b), length(l) {}
  16. };
  17.  
  18. void dfs(vector<vector<int>> & G, vector<bool> & was, int now, int & meetSuper, int & total, int super)
  19. {
  20.     was[now] = true;
  21.     total++;
  22.     for (auto x : G[now])
  23.     {
  24.         if (!was[x])
  25.             dfs(G, was, x, meetSuper, total, super);
  26.         else if (x == super)
  27.             meetSuper++;
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     ios::sync_with_stdio(false);
  34.     cin.tie(nullptr);
  35.     int n, m ,k, x, y;
  36.     cin >> n >> m >> k;
  37.     vector<vector<int>> G(n + 1);
  38.     for (int i = 0; i < m; i++)
  39.     {
  40.         cin >> x >> y;
  41.         G[x].push_back(y);
  42.         G[y].push_back(x);
  43.     }
  44.     int super = -1;
  45.     for (int i = 1; i <= n; i++)
  46.         if (G[i].size() > 2)
  47.             super = i;
  48.     if (super == -1)        /// No root, trivial case: chain or loop
  49.     {
  50.         cout << upper(n - k, 2 * k);
  51.         return 0;
  52.     }
  53.     vector<Element> elements;
  54.     vector<bool> was(n + 1, false);
  55.     was[super] = true;
  56.     for (auto x : G[super])
  57.         if (!was[x])
  58.         {
  59.             int meetSuper = 0, total = 0;
  60.             dfs(G, was, x, meetSuper, total, super);
  61.             elements.emplace_back(meetSuper == 2, total);
  62.         }
  63.     int R = n, L = -1;
  64.     while (R - L > 1)
  65.     {
  66.         int M = (R + L) / 2, lenM = 2 * M + 1;
  67.         int overlapse = -1, ans = 0;
  68.         for (auto x : elements)
  69.             if (!x.isCycle)
  70.                 overlapse = max(overlapse, ((x.length + M) / lenM) * lenM - x.length - 1);
  71.         if (overlapse == -1)        /// Making hole in super
  72.         {
  73.             ans++;
  74.             overlapse = M;
  75.         }
  76.         for (auto x : elements)
  77.         {
  78.             if (x.isCycle)
  79.                 ans += upper(max(0, x.length - 2 * overlapse), lenM);
  80.             else
  81.                 ans += upper(max(0, x.length - overlapse), lenM);
  82.         }
  83.         if (ans > k)
  84.             L = M;
  85.         else
  86.             R = M;
  87.     }
  88.     cout << R;
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement