Advertisement
trungore10

problem_cezar

Sep 29th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void Read(int &val) {
  5.     val = 0; char c;
  6.     do { c = getchar(); } while (!isdigit(c));
  7.     while (isdigit(c)) { val = val * 10 + c - '0'; c = getchar(); }
  8. }
  9.  
  10. const int N = 1e5 + 4;
  11. int n, k;
  12. vector<int> adj[N];
  13.  
  14. int numChild[N];
  15.  
  16. void DFS_CountChild(int dad, int u) {
  17.     numChild[u] = 1;
  18.     for (int v : adj[u]) if (v != dad) {
  19.         DFS_CountChild(u, v);
  20.         numChild[u] += numChild[v];
  21.     }
  22. }
  23.  
  24. struct Binary_Indexed_Tree {
  25.     int node[N], cost[N];
  26.     Binary_Indexed_Tree() {
  27.         memset(node, 0, sizeof(node));
  28.         memset(cost, 0, sizeof(cost));
  29.     }
  30.  
  31.     void update(int pos, int val) {
  32.         for (int i = pos; i < N; i += i & (-i)) node[i] += val;
  33.         for (int i = pos; i < N; i += i & (-i)) cost[i] += val * pos;
  34.     }
  35.     int get(int pos) {
  36.         int ans = 0;
  37.         for (int i = pos; i > 0; i -= i & (-i)) ans += node[i];
  38.         return ans;
  39.     }
  40.     int getCost(int pos) {
  41.         int ans = 0;
  42.         for (int i = pos; i > 0; i -= i & (-i)) ans += cost[i];
  43.         return ans;
  44.     }
  45.  
  46. } BIT;
  47.  
  48. int res = -1;
  49.  
  50. void process(int root) {
  51.     int l = 1, r = n, pos = -1;
  52.     while (l <= r) {
  53.         int mid = (l + r) / 2, Count = BIT.get(n) - BIT.get(mid-1);
  54.         if (Count >= k) { pos = mid; l = mid + 1; }
  55.         else r = mid - 1;
  56.     }
  57.    
  58.     assert(pos != -1);
  59.  
  60.     int tmp = BIT.get(n) - BIT.get(pos-1), remain = tmp - k;
  61.  
  62.     int tmpRes = pos * remain;
  63.     tmpRes += BIT.getCost(pos-1);
  64.  
  65.     res = (res == -1) ? tmpRes : min(tmpRes, res);
  66. }
  67.  
  68. void DFS_getRes(int dad, int u) {
  69.     int prevDad = numChild[dad], prevU = numChild[u];
  70.     if (u != 1) {
  71.         numChild[dad] = n - numChild[u]; numChild[u] = n;
  72.         BIT.update(prevDad, -1); BIT.update(prevU, -1);
  73.         BIT.update(numChild[dad], 1); BIT.update(numChild[u], 1);
  74.     }
  75.  
  76.     process(u);
  77.  
  78.     for (int v : adj[u]) if (v != dad) DFS_getRes(u, v);
  79.  
  80.     if (u != 1) {
  81.         BIT.update(prevDad, 1); BIT.update(prevU, 1);
  82.         BIT.update(numChild[dad], -1); BIT.update(numChild[u], -1);
  83.         numChild[dad] = prevDad; numChild[u] = prevU;
  84.     }
  85. }
  86.  
  87. void sol() {
  88.     DFS_CountChild(0, 1);
  89.     for (int i = 1; i <= n; ++i) BIT.update(numChild[i], 1);
  90.  
  91.     DFS_getRes(0, 1);
  92.     cout << res << '\n';   
  93. }
  94.  
  95. int main() {
  96.     freopen("input.txt", "r", stdin);
  97.     // freopen("cezar.inp", "r", stdin);
  98.     // freopen("cezar.out", "w", stdout);
  99.  
  100.     Read(n); Read(k); ++k;
  101.     int u, v;
  102.     for (int i = 1; i < n; ++i) {
  103.         Read(u); Read(v);
  104.         adj[u].push_back(v); adj[v].push_back(u);
  105.     }
  106.  
  107.     sol();
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement