Submission #846433

#TimeUsernameProblemLanguageResultExecution timeMemory
846433voiTorrent (COI16_torrent)C++17
0 / 100
78 ms22100 KiB
#include <bits/stdc++.h> #define pll pair <long long, long long> #define fi first #define se second using namespace std; #define task "code" #define all(s) s.begin(), s.end() typedef long long ll; const int ar = 3e5+2; const ll mod = 998244353; const int oo = 1e9; int n, a, b, child[ar], dp[ar], pre[ar], suf[ar]; vector <int> g[ar], trace; void caltrace(int u, int p) { if(trace.empty() || trace.back() != b) { if(trace.size()) child[trace.back()] = u; trace.push_back(u); } for(auto v : g[u]) if(v != p) caltrace(v, u); if(trace.back() != b) trace.pop_back(), child[trace.back()] = 0; } void dfs(int u, int p) { vector <int> tg; for(auto v : g[u]) if(v != p) { dfs(v, u); // if(u == 3) cout << v << " 3333\n"; if(v != child[u]) { tg.push_back(dp[v]); } } sort(all(tg), greater<int>()); // if(u == 3) cout << tg.size() << '\n'; for(int i = 0; i < tg.size(); ++i) dp[u] = max(dp[u], tg[i] + i + 1); // cout << u << ' ' << dp[u] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } cin >> n >> a >> b; for(int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } caltrace(a, 0); dfs(a, 0); int m = trace.size(); // for(auto x : trace) cout << x << ' ' << dp[x] << '\n'; if(m == 2) return cout << max(dp[a], dp[b]), 0; pre[0] = dp[trace[0]]; for(int i = 1; i < m - 1; ++i) pre[i] = max(pre[i - 1], dp[trace[i]] + i + 1); suf[m - 1] = dp[trace.back()]; for(int i = m - 2; i > 0; --i) suf[i] = max(suf[i + 1], dp[trace[i]] + m - i); int res = oo; for(int i = 1; i < m; ++i) res = min(res, max(pre[i - 1], suf[i])); cout << res; return 0; }

Compilation message (stderr)

torrent.cpp: In function 'void dfs(int, int)':
torrent.cpp:41:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for(int i = 0; i < tg.size(); ++i)
      |                    ~~^~~~~~~~~~~
torrent.cpp: In function 'int main()':
torrent.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
torrent.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...