fork download
  1. // Shaazzz
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. const int N = 1e6 + 10;
  5. int n, k, H[N];
  6. vector < int > Adj[N];
  7. void DFS(int v, int p)
  8. {
  9. for (int &u : Adj[v])
  10. if (u != p)
  11. H[u] = H[v] + 1,
  12. DFS(u, v);
  13. }
  14. int main()
  15. {
  16. scanf("%d%d", &n, &k);
  17. for (int i = 1; i < n; i++)
  18. {
  19. int v, u;
  20. scanf("%d%d", &v, &u);
  21. Adj[v].push_back(u);
  22. Adj[u].push_back(v);
  23. }
  24. DFS(1, 0);
  25. int v = 0, d = 0;
  26. for (int i = 1; i <= n; i++)
  27. if (H[i] >= H[v])
  28. v = i;
  29. H[v] = 0; DFS(v, 0);
  30. for (int i = 1; i <= n; i++)
  31. d = max(d, H[i]);
  32. if (k <= d) return !printf("%d", k + 1);
  33. return !printf("%d", min((k + d + 2) / 2, n));
  34. }
Success #stdin #stdout 0s 42584KB
stdin
Standard input is empty
stdout
1