Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. ///services.msc / информация... / автоматически
  2. #include<bits/stdc++.h>
  3.  
  4. #define pb push_back
  5. #define ll long long
  6. #define ld long double
  7. #define F first
  8. #define S second
  9.  
  10. using namespace std;
  11.  
  12. mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
  13.  
  14. const ll M = 1e9 + 7;
  15. const ll N = 2e6 + 7;
  16.  
  17. int n, t, m, x, y, f[N], d[N], p[N];
  18. vector<int> g[N], a, F[N];
  19. bool fl[N];
  20.  
  21. void bfs(int v){
  22. queue<int> q;
  23. q.push(v);
  24. p[v] = v;
  25. while (!q.empty()){
  26. int u = q.front();
  27. q.pop();
  28. for (auto to : g[u])
  29. if (!p[to]){
  30. p[to] = u;
  31. q.push(to);
  32. }
  33. }
  34. return;
  35. }
  36.  
  37. void dfs(int v, int p){
  38. if (g[v].size() == 1){
  39. f[v] = 0;
  40. return;
  41. }
  42. vector<int> all;
  43. for (auto to : g[v]){
  44. if (to == p) continue;
  45. dfs(to, v);
  46. all.pb(f[to]);
  47. }
  48. sort(all.rbegin(), all.rend());
  49. if (all.size() == 1) f[v] = 1;
  50. else f[v] = all[1] + all.size();
  51. return;
  52. }
  53.  
  54. bool can(int x){
  55. int cur = 1, y = x;
  56. for (int i = a.size() - 1; i >= 0; i--){
  57. int p = 0;
  58. while (p < F[i].size() && F[i][p] > y) p++;
  59. cur -= p;
  60. if (cur < 0) return 0;
  61. cur++;
  62. y -= p;
  63. if (y < 0) return 0;
  64. }
  65. return 1;
  66. }
  67.  
  68. int32_t main(){
  69. ios_base::sync_with_stdio(0);
  70. #ifdef LOCAL
  71. freopen("input.txt", "r", stdin);
  72. freopen("output.txt", "w", stdout);
  73. #endif
  74. cin >> n >> t >> m;
  75. for (int i = 1; i < n; i++){
  76. cin >> x >> y;
  77. g[x].pb(y);
  78. g[y].pb(x);
  79. }
  80. bfs(t);
  81. int v = m;
  82. fl[v] = 1;
  83. while (v != t){
  84. a.pb(v);
  85. v = p[v];
  86. fl[v] = 1;
  87. }
  88. reverse(a.begin(), a.end());
  89. for (int i = 0; i < a.size(); i++){
  90. d[i] = (i ? d[i - 1] : 0);
  91. for (auto to : g[a[i]])
  92. if (!fl[to]) dfs(to, a[i]), d[i]++;
  93. for (auto to : g[a[i]])
  94. if (!fl[to]){
  95. f[to] += d[i];
  96. F[i].pb(f[to]);
  97. }
  98. sort(F[i].rbegin(), F[i].rend());
  99. }
  100. int l = 0, r = 1e9, ans = 1e9;
  101. while (l <= r){
  102. int mid = (l + r)/2;
  103. if (can(mid)){
  104. ans = min(ans, mid);
  105. r = mid - 1;
  106. }
  107. else l = mid + 1;
  108. }
  109. cout << ans;
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement