fork download
  1. /* AUTHOR: TUAN ANH - BUI */
  2. // ~~ icebear ~~
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. typedef pair<int, int> ii;
  8. typedef pair<int, ii> iii;
  9.  
  10. template<class X, class Y>
  11. bool minimize(X &x, const Y &y) {
  12. if (x > y) return x = y, true;
  13. return false;
  14. }
  15.  
  16. template<class X, class Y>
  17. bool maximize(X &x, const Y &y) {
  18. if (x < y) return x = y, true;
  19. return false;
  20. }
  21.  
  22. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  23. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  24. #define REP(i, n) for(int i=0; i<(n); ++i)
  25. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  26. #define MASK(i) (1LL << (i))
  27. #define BIT(S, i) (((S) >> (i)) & 1)
  28. #define mp make_pair
  29. #define pb push_back
  30. #define fi first
  31. #define se second
  32. #define all(x) x.begin(), x.end()
  33. #define task "icebear"
  34. /*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN TST26 */
  35.  
  36. const int MOD = 1e9 + 7;
  37. const int inf = (int)1e9 + 27092008;
  38. const ll INF = (ll)1e18 + 27092008;
  39. const int N = 2e5 + 5;
  40. int numNode, limitSize;
  41. vector<int> G[N];
  42. int high[N], tour[N << 1], pos[N], minHigh[N << 1][20], timer = 0, curSize = 0;
  43. set<int> nodes;
  44.  
  45. void dfs(int u, int par) {
  46. pos[u] = ++timer;
  47. tour[timer] = u;
  48. for(int v : G[u]) if (v != par) {
  49. high[v] = high[u] + 1;
  50. dfs(v, u);
  51. tour[++timer] = u;
  52. }
  53. }
  54.  
  55. #define MIN_HIGH(x, y) (high[x] < high[y] ? x : y)
  56. int LCA(int u, int v) {
  57. int l = pos[u], r = pos[v];
  58. if (l > r) swap(l, r);
  59. int k = __lg(r - l + 1);
  60. return MIN_HIGH(minHigh[l][k], minHigh[r - MASK(k) + 1][k]);
  61. }
  62.  
  63. void update(int u, int sign) {
  64. nodes.insert(pos[u]);
  65. auto it = nodes.find(pos[u]);
  66. int prv = (it == nodes.begin() ? tour[*prev(nodes.end())] : tour[*prev(it)]);
  67. int nxt = (next(it) == nodes.end() ? tour[*nodes.begin()] : tour[*next(it)]);
  68.  
  69. curSize -= sign * (high[LCA(u, nxt)] + high[LCA(u, prv)] - high[LCA(prv, nxt)]);
  70. curSize += sign * high[u];
  71.  
  72. if (sign == -1) nodes.erase(it);
  73. }
  74.  
  75. void init(void) {
  76. cin >> numNode >> limitSize;
  77. FOR(i, 2, numNode) {
  78. int u, v;
  79. cin >> u >> v;
  80. G[u].pb(v);
  81. G[v].pb(u);
  82. }
  83. }
  84.  
  85. void process(void) {
  86. dfs(1, -1);
  87. FOR(i, 1, timer) minHigh[i][0] = tour[i];
  88. FOR(j, 1, 19) FOR(i, 1, timer - MASK(j) + 1)
  89. minHigh[i][j] = MIN_HIGH(minHigh[i][j - 1], minHigh[i + MASK(j - 1)][j - 1]);
  90. int l = 1, ans = 0;
  91. FOR(r, 1, numNode) {
  92. update(r, +1);
  93. while(curSize >= limitSize) update(l++, -1);
  94. maximize(ans, r - l + 1);
  95. }
  96. cout << ans;
  97. }
  98.  
  99. int main() {
  100. ios_base::sync_with_stdio(0);
  101. cin.tie(0); cout.tie(0);
  102. if (fopen(task".inp", "r")) {
  103. freopen(task".inp", "r", stdin);
  104. freopen(task".out", "w", stdout);
  105. }
  106. int tc = 1;
  107. // cin >> tc;
  108. while(tc--) {
  109. init();
  110. process();
  111. }
  112. return 0;
  113. }
  114.  
  115.  
Success #stdin #stdout 0.01s 11892KB
stdin
Standard input is empty
stdout
Standard output is empty