Submission #413843

#TimeUsernameProblemLanguageResultExecution timeMemory
413843TruaShamuTorrent (COI16_torrent)C++14
100 / 100
499 ms27724 KiB
#include <bits/stdc++.h> using namespace std; struct Edge { int to; int index; Edge(int to, int index): to(to), index(index) {} }; const int N = 3e5 + 5; vector<int> path; vector<int> temp; vector<Edge> a[N]; int n, X, Y; int cutEdge; void dfsFindPath(int u, int p = 0) { if (!path.empty()) return; if (u == Y) { path = temp; return; } for (auto e : a[u]) { int v = e.to; if (v == p) continue; temp.push_back(e.index); dfsFindPath(v, u); temp.pop_back(); } } int solve(int u, int p = 0) { int ans = 0; vector<int> V; for (auto e : a[u]) { int v = e.to; if (v == p) continue; if (e.index == cutEdge) continue; V.push_back(solve(v, u)); } sort(V.begin(), V.end(), greater<int>()); for (int i = 0; i < V.size(); ++i) ans = max(ans, V[i] + i + 1); return ans; } int main() { ios::sync_with_stdio(false); cin >> n >> X >> Y; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; a[u].push_back(Edge(v, i)); a[v].push_back(Edge(u, i)); } dfsFindPath(X); int l = 0, r = (int)path.size() - 1, last = 0; while (l <= r) { int mid = l + r >> 1; cutEdge = path[mid]; int ansX = solve(X); int ansY = solve(Y); if (ansX <= ansY) last = mid, l = mid + 1; else r = mid - 1; } cutEdge = path[last]; int ans = max(solve(X), solve(Y)); if (last + 1 < path.size()) { cutEdge = path[last + 1]; ans = min(ans, max(solve(X), solve(Y))); } cout << ans << endl; return 0; }

Compilation message (stderr)

torrent.cpp: In function 'int solve(int, int)':
torrent.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i = 0; i < V.size(); ++i)
      |                     ~~^~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:62:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   62 |         int mid = l + r >> 1;
      |                   ~~^~~
torrent.cpp:73:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     if (last + 1 < path.size()) {
      |         ~~~~~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...