Submission #923540

#TimeUsernameProblemLanguageResultExecution timeMemory
923540codefoxTorrent (COI16_torrent)C++14
31 / 100
1133 ms23548 KiB
#include <bits/stdc++.h> using namespace std; vector<vector<int>> graph; vector<int> opt; vector<int> path; int f, s; bool pos = 1; int border = -1; int sol = 0; void dfs(int i, int p, int limit) { opt[i] = 0; vector<int> subs; int alt = -1; for (int ele:graph[i]) { if (ele==p) continue; if (!path[ele]) { dfs(ele, i, limit); subs.push_back(opt[ele]); } else alt = ele; } sort(subs.begin(), subs.end(), greater<int>()); int c = limit-1; for (int j = 0; j < subs.size(); j++) { if (subs[j]+j+1==limit) c = subs[j]; opt[i] = max(opt[i], subs[j]+j+1); } if (opt[i]>limit) { if (path[i]) border = p; if (opt[i]>sol) pos = 0; } else if (alt != -1) dfs(alt, i, c); } void check(int i, int p) { opt[i] = 0; vector<int> subs; for (int ele:graph[i]) { if (ele == p) continue; if (ele == border) continue; check(ele, i); subs.push_back(opt[ele]); } sort(subs.begin(), subs.end(), greater<int>()); for (int j = 0; j < subs.size(); j++) { opt[i] = max(opt[i], subs[j]+j+1); } if (opt[i]>sol) pos = 0; } int findpath(int i, int p) { if (i==f) return 1; for (int ele:graph[i]) { if (ele==p) continue; if (findpath(ele, i)) path[i] = 1; } return path[i]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n; cin >> n >> f >> s; f--; s--; graph = vector<vector<int>>(n); path = vector<int>(n, 0); opt = vector<int>(n, 0); for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } findpath(s, -1); for (int i = 29; i >= 0; i--) { pos = 1; border = f; sol += 1<<i; dfs(f, -1, sol); check(s, -1); if (pos) sol -= 1<<i; } cout << sol+1; return 0; }

Compilation message (stderr)

torrent.cpp: In function 'void dfs(int, int, int)':
torrent.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |     for (int j = 0; j < subs.size(); j++)
      |                     ~~^~~~~~~~~~~~~
torrent.cpp: In function 'void check(int, int)':
torrent.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for (int j = 0; j < subs.size(); j++)
      |                     ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...