Submission #650505

#TimeUsernameProblemLanguageResultExecution timeMemory
650505Alex_tz307Torrent (COI16_torrent)C++17
100 / 100
127 ms28388 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(); 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...