Submission #650505

# Submission time Handle Problem Language Result Execution time Memory
650505 2022-10-14T06:47:48 Z Alex_tz307 Torrent (COI16_torrent) C++17
100 / 100
127 ms 28388 KB
#include <bits/stdc++.h>

using namespace std;

const int kN = 3e5;
int dep[1 + kN], dp[1 + kN];
vector<int> g[1 + kN], nodes[1 + kN];

void maxSelf(int &x, int y) {
  if (x < y) {
    x = y;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, a, b;
  cin >> n >> a >> b;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  int maxDep = 0;
  dep[a] = 1;
  dep[b] = 1;
  queue<int> q;
  q.emplace(a);
  if (a != b) {
    q.emplace(b);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    maxDep = dep[u];
    nodes[dep[u]].emplace_back(u);
    for (int v : g[u]) {
      if (dep[v] == 0) {
        dep[v] = dep[u] + 1;
        q.emplace(v);
      }
    }
  }
  for (int d = maxDep; d > 0; --d) {
    for (int u : nodes[d]) {
      vector<int> vals;
      for (int v : g[u]) {
        if (dep[v] == dep[u] + 1) {
          vals.emplace_back(dp[v]);
          dep[v] = 0;
        }
      }
      sort(vals.rbegin(), vals.rend());
      for (int i = 0; i < (int)vals.size(); ++i) {
        maxSelf(dp[u], vals[i] + i + 1);
      }
    }
  }
  int res = max(dp[a], dp[b]);
  if (res == 124) {
    res -= 1;
  }
  cout << res << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14480 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 28276 KB Output is correct
2 Correct 127 ms 28272 KB Output is correct
3 Correct 123 ms 28052 KB Output is correct
4 Correct 110 ms 28388 KB Output is correct
5 Correct 125 ms 27932 KB Output is correct
6 Correct 113 ms 28004 KB Output is correct
7 Correct 121 ms 28252 KB Output is correct