Submission #111157

#TimeUsernameProblemLanguageResultExecution timeMemory
111157luciocfTorrent (COI16_torrent)C++14
100 / 100
708 ms26040 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5+10; int n; int A, B; vector<int> grafo[maxn]; vector<int> path; bool comp(int a, int b) {return a>b;} bool dfs(int u, int p) { if (u == B) { path.push_back(B); return 1; } for (auto v: grafo[u]) { if (v == p) continue; if (!dfs(v, u)) continue; path.push_back(u); return 1; } return 0; } int solve(int u, int p, int block) { vector<int> child; int tt = 0; for (auto v: grafo[u]) if (v != p && v != block) child.push_back(solve(v, u, block)); sort(child.begin(), child.end(), comp); for (int i = 0; i < child.size(); i++) tt = max(tt, child[i]+i+1); return tt; } int busca(void) { int ini = 1, fim = path.size()-1, ans = -1; while (ini <= fim) { int mid = (ini+fim)>>1; int ans1 = solve(A, 0, path[mid]); int ans2 = solve(B, 0, path[mid-1]); if (ans2 >= ans1) ans = mid-1, ini = mid+1; else fim = mid-1; } return ans; } int main(void) { scanf("%d %d %d", &n, &A, &B); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); grafo[u].push_back(v); grafo[v].push_back(u); } dfs(A, 0); reverse(path.begin(), path.end()); int opt = busca(); int ans; if (opt == -1) ans = solve(A, 0, path[1]); else { ans = solve(B, 0, path[opt]); if (opt < path.size()-2) ans = min(ans, solve(A, 0, path[opt+2])); } printf("%d\n", ans); }

Compilation message (stderr)

torrent.cpp: In function 'int solve(int, int, int)':
torrent.cpp:47:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < child.size(); i++)
                  ~~^~~~~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:96:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (opt < path.size()-2)
       ~~~~^~~~~~~~~~~~~~~
torrent.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &A, &B);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...