Submission #650494

#TimeUsernameProblemLanguageResultExecution timeMemory
650494Alex_tz307Torrent (COI16_torrent)C++17
69 / 100
150 ms32548 KiB
#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(); maxSelf(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]); } } sort(vals.rbegin(), vals.rend()); for (int i = 0; i < (int)vals.size(); ++i) { maxSelf(dp[u], vals[i] + i + 1); } } } cout << max(dp[a], dp[b]) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...